diff --git a/src/Context/JsonContext.php b/src/Context/JsonContext.php index f7c7b8eb..245659b6 100644 --- a/src/Context/JsonContext.php +++ b/src/Context/JsonContext.php @@ -207,9 +207,13 @@ public function theJsonNodeShouldContain($node, $text): void { $json = $this->getJson(); - $actual = $this->inspector->evaluate($json, $node); + $actual = (string) $this->inspector->evaluate($json, $node); - $this->assertContains($text, (string) $actual); + $this->assertContains( + $text, + $actual, + \sprintf("The node '%s' should contain '%s', but it is '%s'.", $node, $text, $actual) + ); } /** @@ -231,9 +235,13 @@ public function theJsonNodeShouldNotContain($node, $text): void { $json = $this->getJson(); - $actual = $this->inspector->evaluate($json, $node); + $actual = (string) $this->inspector->evaluate($json, $node); - $this->assertNotContains($text, (string) $actual); + $this->assertNotContains( + $text, + $actual, + \sprintf("The node '%s' should not contain '%s', but it is '%s'.", $node, $text, $actual) + ); } /** diff --git a/tests/units/Context/JsonContext.php b/tests/units/Context/JsonContext.php index ab04cfaf..e5e3df3a 100644 --- a/tests/units/Context/JsonContext.php +++ b/tests/units/Context/JsonContext.php @@ -3,15 +3,25 @@ namespace Behatch\Tests\Units\Context; use Behat\Gherkin\Node\TableNode; +use Behat\Mink\Driver\BrowserKitDriver; +use Behat\Mink\Exception\ExpectationException; +use Behat\Mink\Mink; +use Behat\Mink\Session; use Behatch\HttpCall\HttpCallResult; use Behatch\HttpCall\HttpCallResultPool; +use Symfony\Component\BrowserKit\HttpBrowser; class JsonContext extends \atoum { private HttpCallResultPool $httpCallResultPool; + private Mink $mink; + public function beforeTestMethod($methodName): void { + $this->mink = new Mink(['default' => new Session(new BrowserKitDriver(new HttpBrowser()))]); + $this->mink->setDefaultSessionName('default'); + $this->httpCallResultPool = new HttpCallResultPool(); $this->httpCallResultPool->store( new HttpCallResult(json_encode([ @@ -164,6 +174,91 @@ public function testTheJsonNodeShouldBeEqualToTheNumber(): void ->hasMessage("The node 'a number node' value is '3', number '2' expected"); } + public function testTheJsonNodeShouldContain(): void + { + $this + ->given($this->newTestedInstance($this->httpCallResultPool)) + ->and($this->testedInstance->setMink($this->mink)) + ->then + + ->if($this->testedInstance->theJsonNodeShouldContain('a string node', 'string')) + ->if($this->testedInstance->theJsonNodeShouldContain('a number node', '3')) + + ->exception(function (): void { + $this->testedInstance->theJsonNodeShouldContain('a string node', 'expected'); + }) + ->isInstanceOf(ExpectationException::class) + ->hasMessage("The node 'a string node' should contain 'expected', but it is 'some string'."); + } + + public function testTheJsonNodesShouldContain(): void + { + $this + ->given($this->newTestedInstance($this->httpCallResultPool)) + ->and($this->testedInstance->setMink($this->mink)) + ->and($validTableNode = new TableNode([ + 1 => ['a string node', 'some'], + 2 => ['another string node', 'other'], + ])) + ->then + + ->if($this->testedInstance->theJsonNodesShouldContain($validTableNode)) + + ->exception(function (): void { + $invalidTableNode = new TableNode([ + 1 => ['a string node', 'some'], + 2 => ['another string node', 'expected'], + ]); + $this->testedInstance->theJsonNodesShouldContain($invalidTableNode); + }) + ->hasMessage("The node 'another string node' should contain 'expected', but it is 'some other string'."); + } + + public function testTheJsonNodeShouldNotContain(): void + { + $this + ->given($this->newTestedInstance($this->httpCallResultPool)) + ->and($this->testedInstance->setMink($this->mink)) + ->then + + ->if($this->testedInstance->theJsonNodeShouldNotContain('a string node', 'expected')) + + ->exception(function (): void { + $this->testedInstance->theJsonNodeShouldNotContain('a string node', 'string'); + }) + ->isInstanceOf(ExpectationException::class) + ->hasMessage("The node 'a string node' should not contain 'string', but it is 'some string'.") + + ->exception(function (): void { + $this->testedInstance->theJsonNodeShouldNotContain('a number node', '3'); + }) + ->isInstanceOf(ExpectationException::class) + ->hasMessage("The node 'a number node' should not contain '3', but it is '3'."); + } + + public function testTheJsonNodesShouldNotContain(): void + { + $this + ->given($this->newTestedInstance($this->httpCallResultPool)) + ->and($this->testedInstance->setMink($this->mink)) + ->and($validTableNode = new TableNode([ + 1 => ['a string node', 'other'], + 2 => ['another string node', 'expected'], + ])) + ->then + + ->if($this->testedInstance->theJsonNodesShouldNotContain($validTableNode)) + + ->exception(function (): void { + $invalidTableNode = new TableNode([ + 1 => ['a string node', 'other'], + 2 => ['another string node', 'other'], + ]); + $this->testedInstance->theJsonNodesShouldNotContain($invalidTableNode); + }) + ->hasMessage("The node 'another string node' should not contain 'other', but it is 'some other string'."); + } + public function testTheJsonNodeShouldExist(): void { $this