33namespace Behatch \Context ;
44
55use Behat \Gherkin \Node \PyStringNode ;
6-
76use Behat \Gherkin \Node \TableNode ;
8- use Behat \ Mink \ Exception \ ExpectationException ;
7+ use Behatch \ HttpCall \ HttpCallResultPool ;
98use Behatch \Json \Json ;
10- use Behatch \Json \JsonSchema ;
119use Behatch \Json \JsonInspector ;
12- use Behatch \HttpCall \ HttpCallResultPool ;
10+ use Behatch \Json \ JsonSchema ;
1311
1412class JsonContext extends BaseContext
1513{
@@ -49,17 +47,19 @@ public function theResponseShouldNotBeInJson()
4947 /**
5048 * Checks, that given JSON node is equal to given value
5149 *
52- * @Then the JSON node :node should be equal to :text
50+ * @Then the JSON node :node should be equal to :expected
5351 */
54- public function theJsonNodeShouldBeEqualTo ($ node , $ text )
52+ public function theJsonNodeShouldBeEqualTo ($ node , $ expected )
5553 {
5654 $ json = $ this ->getJson ();
5755
56+ $ expected = self ::reespaceSpecialGherkinValue ($ expected );
57+
5858 $ actual = $ this ->inspector ->evaluate ($ json , $ node );
5959
60- if ($ actual != $ text ) {
60+ if ($ actual != $ expected ) {
6161 throw new \Exception (
62- sprintf ("The node value is '%s' " , json_encode ($ actual ))
62+ sprintf ("The node '%s' value is '%s', '%s' expected " , $ node , json_encode ($ actual ), $ expected )
6363 );
6464 }
6565 }
@@ -71,8 +71,21 @@ public function theJsonNodeShouldBeEqualTo($node, $text)
7171 */
7272 public function theJsonNodesShouldBeEqualTo (TableNode $ nodes )
7373 {
74- foreach ($ nodes ->getRowsHash () as $ node => $ text ) {
75- $ this ->theJsonNodeShouldBeEqualTo ($ node , $ text );
74+ $ json = $ this ->getJson ();
75+
76+ $ errors = [];
77+ foreach ($ nodes ->getRowsHash () as $ node => $ expected ) {
78+ $ actual = $ this ->inspector ->evaluate ($ json , $ node );
79+
80+ $ expected = self ::reespaceSpecialGherkinValue ($ expected );
81+
82+ if ($ actual != $ expected ) {
83+ $ errors [] = sprintf ("The node '%s' value is '%s', '%s' expected " , $ node , json_encode ($ actual ), $ expected );
84+ }
85+ }
86+
87+ if (!empty ($ errors )) {
88+ throw new \Exception (implode ("\n" , $ errors ));
7689 }
7790 }
7891
@@ -89,7 +102,7 @@ public function theJsonNodeShouldMatch($node, $pattern)
89102
90103 if (preg_match ($ pattern , $ actual ) === 0 ) {
91104 throw new \Exception (
92- sprintf ("The node value is '%s' " , json_encode ($ actual ))
105+ sprintf ("The node '%s' value is '%s', '%s' pattern expected " , $ node , json_encode ($ actual ), $ pattern )
93106 );
94107 }
95108 }
@@ -107,7 +120,7 @@ public function theJsonNodeShouldBeNull($node)
107120
108121 if (null !== $ actual ) {
109122 throw new \Exception (
110- sprintf (' The node value is `%s` ' , json_encode ($ actual ))
123+ sprintf (" The node '%s' value is '%s', null expected " , $ node , json_encode ($ actual ))
111124 );
112125 }
113126 }
@@ -119,9 +132,15 @@ public function theJsonNodeShouldBeNull($node)
119132 */
120133 public function theJsonNodeShouldNotBeNull ($ node )
121134 {
122- $ this ->not (function () use ($ node ) {
123- return $ this ->theJsonNodeShouldBeNull ($ node );
124- }, sprintf ('The node %s should not be null ' , $ node ));
135+ $ json = $ this ->getJson ();
136+
137+ $ actual = $ this ->inspector ->evaluate ($ json , $ node );
138+
139+ if (null === $ actual ) {
140+ throw new \Exception (
141+ sprintf ("The node '%s' value is null, non-null value expected " , $ node )
142+ );
143+ }
125144 }
126145
127146 /**
@@ -137,7 +156,7 @@ public function theJsonNodeShouldBeTrue($node)
137156
138157 if (true !== $ actual ) {
139158 throw new \Exception (
140- sprintf (' The node value is `%s` ' , json_encode ($ actual ))
159+ sprintf (" The node '%s' value is '%s', 'true' expected " , $ node , json_encode ($ actual ))
141160 );
142161 }
143162 }
@@ -155,25 +174,25 @@ public function theJsonNodeShouldBeFalse($node)
155174
156175 if (false !== $ actual ) {
157176 throw new \Exception (
158- sprintf (' The node value is `%s` ' , json_encode ($ actual ))
177+ sprintf (" The node '%s' value is '%s', 'false' expected " , $ node , json_encode ($ actual ))
159178 );
160179 }
161180 }
162181
163182 /**
164183 * Checks, that given JSON node is equal to the given string
165184 *
166- * @Then the JSON node :node should be equal to the string :text
185+ * @Then the JSON node :node should be equal to the string :expected
167186 */
168- public function theJsonNodeShouldBeEqualToTheString ($ node , $ text )
187+ public function theJsonNodeShouldBeEqualToTheString ($ node , $ expected )
169188 {
170189 $ json = $ this ->getJson ();
171190
172191 $ actual = $ this ->inspector ->evaluate ($ json , $ node );
173192
174- if ($ actual !== $ text ) {
193+ if ($ actual !== $ expected ) {
175194 throw new \Exception (
176- sprintf (' The node value is `%s` ' , json_encode ($ actual ))
195+ sprintf (" The node '%s' value is '%s ', string '%s' expected " , $ node , json_encode ($ actual ), $ expected )
177196 );
178197 }
179198 }
@@ -191,7 +210,7 @@ public function theJsonNodeShouldBeEqualToTheNumber($node, $number)
191210
192211 if ($ actual !== (float ) $ number && $ actual !== (int ) $ number ) {
193212 throw new \Exception (
194- sprintf (' The node value is `%s` ' , json_encode ($ actual ))
213+ sprintf (" The node '%s' value is '%s ', number '%s' expected " , $ node , json_encode ($ actual ), ( string ) $ number )
195214 );
196215 }
197216 }
@@ -207,7 +226,7 @@ public function theJsonNodeShouldHaveElements($node, $count)
207226
208227 $ actual = $ this ->inspector ->evaluate ($ json , $ node );
209228
210- $ this ->assertSame ($ count , sizeof ((array ) $ actual ));
229+ $ this ->assertSame ($ count , count ((array ) $ actual ));
211230 }
212231
213232 /**
@@ -276,6 +295,7 @@ public function theJsonNodeShouldExist($name)
276295 } catch (\Exception $ e ) {
277296 throw new \Exception ("The node ' $ name' does not exist. " );
278297 }
298+
279299 return $ node ;
280300 }
281301
@@ -337,7 +357,7 @@ public function theJsonShouldBeInvalidAccordingToTheSchema($filename)
337357
338358 $ this ->not (function () use ($ filename ) {
339359 return $ this ->theJsonShouldBeValidAccordingToTheSchema ($ filename );
340- }, " The schema was valid " );
360+ }, ' The schema was valid ' );
341361 }
342362
343363 /**
@@ -356,7 +376,7 @@ public function theJsonShouldBeEqualTo(PyStringNode $content)
356376 $ this ->assertSame (
357377 (string ) $ expected ,
358378 (string ) $ actual ,
359- "The json is equal to: \n" . $ actual ->encode ()
379+ "The json is equal to: \n" . $ actual ->encode ()
360380 );
361381 }
362382
@@ -390,8 +410,8 @@ public function theJsonShouldBeValidAccordingToTheSwaggerSchema($dumpPath, $sche
390410 )
391411 );
392412 }
413+
393414 /**
394- *
395415 * Checks, that response JSON not matches with a swagger dump
396416 *
397417 * @Then the JSON should not be valid according to swagger :dumpPath dump schema :schemaName
@@ -403,8 +423,6 @@ public function theJsonShouldNotBeValidAccordingToTheSwaggerSchema($dumpPath, $s
403423 }, 'JSON Schema matches but it should not ' );
404424 }
405425
406-
407-
408426 protected function getJson ()
409427 {
410428 return new Json ($ this ->httpCallResultPool ->getResult ()->getValue ());
@@ -418,4 +436,9 @@ private function checkSchemaFile($filename)
418436 );
419437 }
420438 }
439+
440+ public static function reespaceSpecialGherkinValue (string $ value ): string
441+ {
442+ return str_replace ("\\n " , "\n" , $ value );
443+ }
421444}
0 commit comments