Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

increase JSON context DX #11

Merged
merged 6 commits into from
Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 51 additions & 28 deletions src/Context/JsonContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
namespace Behatch\Context;

use Behat\Gherkin\Node\PyStringNode;

use Behat\Gherkin\Node\TableNode;
use Behat\Mink\Exception\ExpectationException;
use Behatch\HttpCall\HttpCallResultPool;
use Behatch\Json\Json;
use Behatch\Json\JsonSchema;
use Behatch\Json\JsonInspector;
use Behatch\HttpCall\HttpCallResultPool;
use Behatch\Json\JsonSchema;

class JsonContext extends BaseContext
{
Expand Down Expand Up @@ -49,17 +47,19 @@ public function theResponseShouldNotBeInJson()
/**
* Checks, that given JSON node is equal to given value
*
* @Then the JSON node :node should be equal to :text
* @Then the JSON node :node should be equal to :expected
*/
public function theJsonNodeShouldBeEqualTo($node, $text)
public function theJsonNodeShouldBeEqualTo($node, $expected)
{
$json = $this->getJson();

$expected = self::reespaceSpecialGherkinValue($expected);

$actual = $this->inspector->evaluate($json, $node);

if ($actual != $text) {
if ($actual != $expected) {
throw new \Exception(
sprintf("The node value is '%s'", json_encode($actual))
sprintf("The node '%s' value is '%s', '%s' expected", $node, json_encode($actual), $expected)
);
}
}
Expand All @@ -71,8 +71,21 @@ public function theJsonNodeShouldBeEqualTo($node, $text)
*/
public function theJsonNodesShouldBeEqualTo(TableNode $nodes)
{
foreach ($nodes->getRowsHash() as $node => $text) {
$this->theJsonNodeShouldBeEqualTo($node, $text);
$json = $this->getJson();

$errors = [];
foreach ($nodes->getRowsHash() as $node => $expected) {
$actual = $this->inspector->evaluate($json, $node);

$expected = self::reespaceSpecialGherkinValue($expected);

if ($actual != $expected) {
$errors[] = sprintf("The node '%s' value is '%s', '%s' expected", $node, json_encode($actual), $expected);
}
}

if (!empty($errors)) {
throw new \Exception(implode("\n", $errors));
}
}

Expand All @@ -89,7 +102,7 @@ public function theJsonNodeShouldMatch($node, $pattern)

if (preg_match($pattern, $actual) === 0) {
throw new \Exception(
sprintf("The node value is '%s'", json_encode($actual))
sprintf("The node '%s' value is '%s', '%s' pattern expected", $node, json_encode($actual), $pattern)
);
}
}
Expand All @@ -107,7 +120,7 @@ public function theJsonNodeShouldBeNull($node)

if (null !== $actual) {
throw new \Exception(
sprintf('The node value is `%s`', json_encode($actual))
sprintf("The node '%s' value is '%s', null expected", $node, json_encode($actual))
);
}
}
Expand All @@ -119,9 +132,15 @@ public function theJsonNodeShouldBeNull($node)
*/
public function theJsonNodeShouldNotBeNull($node)
{
$this->not(function () use ($node) {
return $this->theJsonNodeShouldBeNull($node);
}, sprintf('The node %s should not be null', $node));
$json = $this->getJson();

$actual = $this->inspector->evaluate($json, $node);

if (null === $actual) {
throw new \Exception(
sprintf("The node '%s' value is null, non-null value expected", $node)
);
}
}

/**
Expand All @@ -137,7 +156,7 @@ public function theJsonNodeShouldBeTrue($node)

if (true !== $actual) {
throw new \Exception(
sprintf('The node value is `%s`', json_encode($actual))
sprintf("The node '%s' value is '%s', 'true' expected", $node, json_encode($actual))
);
}
}
Expand All @@ -155,25 +174,25 @@ public function theJsonNodeShouldBeFalse($node)

if (false !== $actual) {
throw new \Exception(
sprintf('The node value is `%s`', json_encode($actual))
sprintf("The node '%s' value is '%s', 'false' expected", $node, json_encode($actual))
);
}
}

/**
* Checks, that given JSON node is equal to the given string
*
* @Then the JSON node :node should be equal to the string :text
* @Then the JSON node :node should be equal to the string :expected
*/
public function theJsonNodeShouldBeEqualToTheString($node, $text)
public function theJsonNodeShouldBeEqualToTheString($node, $expected)
{
$json = $this->getJson();

$actual = $this->inspector->evaluate($json, $node);

if ($actual !== $text) {
if ($actual !== $expected) {
throw new \Exception(
sprintf('The node value is `%s`', json_encode($actual))
sprintf("The node '%s' value is '%s', string '%s' expected", $node, json_encode($actual), $expected)
);
}
}
Expand All @@ -191,7 +210,7 @@ public function theJsonNodeShouldBeEqualToTheNumber($node, $number)

if ($actual !== (float) $number && $actual !== (int) $number) {
throw new \Exception(
sprintf('The node value is `%s`', json_encode($actual))
sprintf("The node '%s' value is '%s', number '%s' expected", $node, json_encode($actual), (string) $number)
);
}
}
Expand All @@ -207,7 +226,7 @@ public function theJsonNodeShouldHaveElements($node, $count)

$actual = $this->inspector->evaluate($json, $node);

$this->assertSame($count, sizeof((array) $actual));
$this->assertSame($count, count((array) $actual));
}

/**
Expand Down Expand Up @@ -276,6 +295,7 @@ public function theJsonNodeShouldExist($name)
} catch (\Exception $e) {
throw new \Exception("The node '$name' does not exist.");
}

return $node;
}

Expand Down Expand Up @@ -337,7 +357,7 @@ public function theJsonShouldBeInvalidAccordingToTheSchema($filename)

$this->not(function () use ($filename) {
return $this->theJsonShouldBeValidAccordingToTheSchema($filename);
}, "The schema was valid");
}, 'The schema was valid');
}

/**
Expand All @@ -356,7 +376,7 @@ public function theJsonShouldBeEqualTo(PyStringNode $content)
$this->assertSame(
(string) $expected,
(string) $actual,
"The json is equal to:\n". $actual->encode()
"The json is equal to:\n" . $actual->encode()
);
}

Expand Down Expand Up @@ -390,8 +410,8 @@ public function theJsonShouldBeValidAccordingToTheSwaggerSchema($dumpPath, $sche
)
);
}

/**
*
* Checks, that response JSON not matches with a swagger dump
*
* @Then the JSON should not be valid according to swagger :dumpPath dump schema :schemaName
Expand All @@ -403,8 +423,6 @@ public function theJsonShouldNotBeValidAccordingToTheSwaggerSchema($dumpPath, $s
}, 'JSON Schema matches but it should not');
}



protected function getJson()
{
return new Json($this->httpCallResultPool->getResult()->getValue());
Expand All @@ -418,4 +436,9 @@ private function checkSchemaFile($filename)
);
}
}

public static function reespaceSpecialGherkinValue(string $value): string
{
return str_replace("\\n", "\n", $value);
}
}
Loading