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

Commit 7191bc6

Browse files
committed
add tests
1 parent c97a84f commit 7191bc6

File tree

2 files changed

+211
-5
lines changed

2 files changed

+211
-5
lines changed

src/Context/JsonContext.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function theJsonNodeShouldBeEqualTo($node, $expected)
5757

5858
if ($actual != $expected) {
5959
throw new \Exception(
60-
sprintf("The node '%s' value is '%s', '%s' expected", $node, json_encode($actual), json_encode($expected))
60+
sprintf("The node '%s' value is '%s', '%s' expected", $node, json_encode($actual), $expected)
6161
);
6262
}
6363
}
@@ -69,8 +69,19 @@ public function theJsonNodeShouldBeEqualTo($node, $expected)
6969
*/
7070
public function theJsonNodesShouldBeEqualTo(TableNode $nodes)
7171
{
72-
foreach ($nodes->getRowsHash() as $node => $text) {
73-
$this->theJsonNodeShouldBeEqualTo($node, $text);
72+
$json = $this->getJson();
73+
74+
$errors = [];
75+
foreach ($nodes->getRowsHash() as $node => $expected) {
76+
$actual = $this->inspector->evaluate($json, $node);
77+
78+
if ($actual != $expected) {
79+
$errors[] = sprintf("The node '%s' value is '%s', '%s' expected", $node, json_encode($actual), $expected);
80+
}
81+
}
82+
83+
if (!empty($errors)) {
84+
throw new \Exception(implode("\n", $errors));
7485
}
7586
}
7687

@@ -123,7 +134,7 @@ public function theJsonNodeShouldNotBeNull($node)
123134

124135
if (null === $actual) {
125136
throw new \Exception(
126-
sprintf("The node '%s' value is null, '%s' expected", $node, json_encode($actual))
137+
sprintf("The node '%s' value is null, non-null value expected", $node)
127138
);
128139
}
129140
}
@@ -195,7 +206,7 @@ public function theJsonNodeShouldBeEqualToTheNumber($node, $number)
195206

196207
if ($actual !== (float) $number && $actual !== (int) $number) {
197208
throw new \Exception(
198-
sprintf("The node '%s' value is '%s', numder '%6' expected", $node, json_encode($actual), (float) $number)
209+
sprintf("The node '%s' value is '%s', number '%s' expected", $node, json_encode($actual), (string) $number)
199210
);
200211
}
201212
}

tests/units/Context/JsonContext.php

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
<?php
2+
3+
namespace Behatch\Tests\Units\Context;
4+
5+
use atoum;
6+
use Behat\Gherkin\Node\TableNode;
7+
use Behatch\HttpCall\HttpCallResult;
8+
use Behatch\HttpCall\HttpCallResultPool;
9+
10+
class JsonContext extends atoum
11+
{
12+
/**
13+
* @var HttpCallResultPool
14+
*/
15+
private $httpCallResultPool;
16+
17+
public function beforeTestMethod($methodName)
18+
{
19+
$this->mockGenerator->orphanize('__construct');
20+
$httpCallResult = $this->newMockInstance(HttpCallResult::class);
21+
$httpCallResult->getMockController()->getValue = json_encode([
22+
'a string node' => 'some string',
23+
'another string node' => 'some other string',
24+
'a null node' => null,
25+
'a true node' => true,
26+
'a false node' => false,
27+
'a number node' => 3,
28+
'an array node' => [
29+
'one',
30+
'two',
31+
'three',
32+
],
33+
]);
34+
35+
$this->httpCallResultPool = $this->newMockInstance(HttpCallResultPool::class);
36+
$this->httpCallResultPool->getMockController()->getResult = $httpCallResult;
37+
}
38+
39+
public function testTheJsonNodeShouldBeEqualTo()
40+
{
41+
$this
42+
->given($this->newTestedInstance($this->httpCallResultPool))
43+
->then
44+
45+
->if($this->testedInstance->theJsonNodeShouldBeEqualTo('a string node', 'some string'))
46+
47+
->exception(function () {
48+
$this->testedInstance->theJsonNodeShouldBeEqualTo('a string node', 'expectedstring');
49+
})
50+
->hasMessage("The node 'a string node' value is '\"some string\"', 'expectedstring' expected")
51+
;
52+
}
53+
54+
public function testTheJsonNodesShouldBeEqualTo()
55+
{
56+
$this
57+
->given($this->newTestedInstance($this->httpCallResultPool))
58+
->and($validTableNode = new TableNode([
59+
1 => ['a string node', 'some string'],
60+
2 => ['another string node', 'some other string'],
61+
]))
62+
->then
63+
64+
->if($this->testedInstance->theJsonNodesShouldBeEqualTo($validTableNode))
65+
66+
->exception(function () {
67+
$invalidTableNode = new TableNode([
68+
1 => ['a string node', 'may the force'],
69+
2 => ['another string node', 'be with you'],
70+
]);
71+
$this->testedInstance->theJsonNodesShouldBeEqualTo($invalidTableNode);
72+
})
73+
->hasMessage("The node 'a string node' value is '\"some string\"', 'may the force' expected\nThe node 'another string node' value is '\"some other string\"', 'be with you' expected")
74+
;
75+
}
76+
public function testTheJsonNodeShouldMatch()
77+
{
78+
$this
79+
->given($this->newTestedInstance($this->httpCallResultPool))
80+
->then
81+
82+
->if($this->testedInstance->theJsonNodeShouldMatch('a string node', '/some/'))
83+
84+
->exception(function () {
85+
$this->testedInstance->theJsonNodeShouldMatch('a string node', '/nomatch/');
86+
})
87+
->hasMessage("The node 'a string node' value is '\"some string\"', '/nomatch/' pattern expected")
88+
;
89+
}
90+
91+
public function testTheJsonNodeShouldBeNull()
92+
{
93+
$this
94+
->given($this->newTestedInstance($this->httpCallResultPool))
95+
->then
96+
97+
->if($this->testedInstance->theJsonNodeShouldBeNull('a null node'))
98+
99+
->exception(function () {
100+
$this->testedInstance->theJsonNodeShouldBeNull('a string node');
101+
})
102+
->hasMessage("The node 'a string node' value is '\"some string\"', null expected")
103+
;
104+
}
105+
106+
public function testTheJsonNodeShouldNotBeNull()
107+
{
108+
$this
109+
->given($this->newTestedInstance($this->httpCallResultPool))
110+
->then
111+
112+
->if($this->testedInstance->theJsonNodeShouldNotBeNull('a string node'))
113+
114+
->exception(function () {
115+
$this->testedInstance->theJsonNodeShouldNotBeNull('a null node');
116+
})
117+
->hasMessage("The node 'a null node' value is null, non-null value expected")
118+
;
119+
}
120+
121+
public function testTheJsonNodeShouldBeTrue()
122+
{
123+
$this
124+
->given($this->newTestedInstance($this->httpCallResultPool))
125+
->then
126+
127+
->if($this->testedInstance->theJsonNodeShouldBeTrue('a true node'))
128+
129+
->exception(function () {
130+
$this->testedInstance->theJsonNodeShouldBeTrue('a false node');
131+
})
132+
->hasMessage("The node 'a false node' value is 'false', 'true' expected")
133+
;
134+
}
135+
136+
public function testTheJsonNodeShouldBeFalse()
137+
{
138+
$this
139+
->given($this->newTestedInstance($this->httpCallResultPool))
140+
->then
141+
142+
->if($this->testedInstance->theJsonNodeShouldBeFalse('a false node'))
143+
144+
->exception(function () {
145+
$this->testedInstance->theJsonNodeShouldBeFalse('a true node');
146+
})
147+
->hasMessage("The node 'a true node' value is 'true', 'false' expected")
148+
;
149+
}
150+
151+
public function testTheJsonNodeShouldBeEqualToTheString()
152+
{
153+
$this
154+
->given($this->newTestedInstance($this->httpCallResultPool))
155+
->then
156+
157+
->if($this->testedInstance->theJsonNodeShouldBeEqualToTheString('a string node', 'some string'))
158+
159+
->exception(function () {
160+
$this->testedInstance->theJsonNodeShouldBeEqualToTheString('a string node', 'expected');
161+
})
162+
->hasMessage("The node 'a string node' value is '\"some string\"', string 'expected' expected")
163+
;
164+
}
165+
166+
public function testTheJsonNodeShouldBeEqualToTheNumber()
167+
{
168+
$this
169+
->given($this->newTestedInstance($this->httpCallResultPool))
170+
->then
171+
172+
->if($this->testedInstance->theJsonNodeShouldBeEqualToTheNumber('a number node', 3))
173+
174+
->exception(function () {
175+
$this->testedInstance->theJsonNodeShouldBeEqualToTheNumber('a number node', 2);
176+
})
177+
->hasMessage("The node 'a number node' value is '3', number '2' expected")
178+
;
179+
}
180+
181+
public function testTheJsonNodeShouldExist()
182+
{
183+
$this
184+
->given($this->newTestedInstance($this->httpCallResultPool))
185+
->then
186+
187+
->if($this->testedInstance->theJsonNodeShouldExist('a string node'))
188+
189+
->exception(function () {
190+
$this->testedInstance->theJsonNodeShouldExist('invalid key');
191+
})
192+
->hasMessage("The node 'invalid key' does not exist.")
193+
;
194+
}
195+
}

0 commit comments

Comments
 (0)