Skip to content
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
16 changes: 12 additions & 4 deletions src/Context/JsonContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

/**
Expand All @@ -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)
);
}

/**
Expand Down
95 changes: 95 additions & 0 deletions tests/units/Context/JsonContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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
Expand Down
Loading