Skip to content
This repository has been archived by the owner on Apr 20, 2021. It is now read-only.

increase JSON context DX #262

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you don’t use the not wrapper here?

Copy link
Contributor Author

@jdeniau jdeniau Sep 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, I could have.

The only "issue" I see is that all those function need to mock or instantiate Mink instance, and I'm not really confident with that for now.

If I change back to the not, I can not easily test the function.

(The error message is Mink instance has not been set on Mink context class. Have you enabled the Mink Extension?)

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);
}
}
6 changes: 3 additions & 3 deletions src/HttpCall/HttpCallResultPoolResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public function resolveArguments(\ReflectionClass $classReflection, array $argum
$parameters = $constructor->getParameters();
foreach ($parameters as $parameter) {
if (
null !== $parameter->getClass()
&& isset($this->dependencies[$parameter->getClass()->name])
null !== $parameter->getType()
&& isset($this->dependencies[$parameter->getType()->getName()])
) {
$arguments[$parameter->name] = $this->dependencies[$parameter->getClass()->name];
$arguments[$parameter->name] = $this->dependencies[$parameter->getType()->getName()];
}
}
}
Expand Down
Loading