@@ -43,9 +43,10 @@ public function responseShouldBeInJson()
4343 */
4444 public function theJsonNodeShouldBeEqualTo ($ jsonNode , $ expectedValue )
4545 {
46- $ realValue = $ this ->evaluateJsonNodeValue ($ jsonNode );
47-
48- $ this ->asserter ->variable ($ realValue )->isEqualTo ($ expectedValue );
46+ $ this ->assert (function () use ($ jsonNode , $ expectedValue ) {
47+ $ realValue = $ this ->evaluateJsonNodeValue ($ jsonNode );
48+ $ this ->asserter ->variable ($ realValue )->isEqualTo ($ expectedValue );
49+ });
4950 }
5051
5152 /**
@@ -54,39 +55,43 @@ public function theJsonNodeShouldBeEqualTo($jsonNode, $expectedValue)
5455 */
5556 public function theJsonNodeShouldHaveElements ($ jsonNode , $ expectedNth )
5657 {
57- $ realValue = $ this ->evaluateJsonNodeValue ($ jsonNode );
58-
59- $ this ->asserter ->phpArray ($ realValue )->hasSize ($ expectedNth );
58+ $ this ->assert (function () use ($ jsonNode , $ expectedNth ) {
59+ $ realValue = $ this ->evaluateJsonNodeValue ($ jsonNode );
60+ $ this ->asserter ->phpArray ($ realValue )->hasSize ($ expectedNth );
61+ });
6062 }
6163
6264 /**
6365 * @Then /^the JSON array node "(?P<jsonNode>[^"]*)" should contain "(?P<expectedValue>.*)" element$/
6466 */
6567 public function theJsonArrayNodeShouldContainElements ($ jsonNode , $ expectedValue )
6668 {
67- $ realValue = $ this ->evaluateJsonNodeValue ($ jsonNode );
68-
69- $ this ->asserter ->phpArray ($ realValue )->contains ($ expectedValue );
69+ $ this ->assert (function () use ($ jsonNode , $ expectedValue ) {
70+ $ realValue = $ this ->evaluateJsonNodeValue ($ jsonNode );
71+ $ this ->asserter ->phpArray ($ realValue )->contains ($ expectedValue );
72+ });
7073 }
7174
7275 /**
7376 * @Then /^the JSON array node "(?P<jsonNode>[^"]*)" should not contain "(?P<expectedValue>.*)" element$/
7477 */
7578 public function theJsonArrayNodeShouldNotContainElements ($ jsonNode , $ expectedValue )
7679 {
77- $ realValue = $ this ->evaluateJsonNodeValue ($ jsonNode );
78-
79- $ this ->asserter ->phpArray ($ realValue )->notContains ($ expectedValue );
80+ $ this ->assert (function () use ($ jsonNode , $ expectedValue ) {
81+ $ realValue = $ this ->evaluateJsonNodeValue ($ jsonNode );
82+ $ this ->asserter ->phpArray ($ realValue )->notContains ($ expectedValue );
83+ });
8084 }
8185
8286 /**
8387 * @Then /^the JSON node "(?P<jsonNode>[^"]*)" should contain "(?P<expectedValue>.*)"$/
8488 */
8589 public function theJsonNodeShouldContain ($ jsonNode , $ expectedValue )
8690 {
87- $ realValue = $ this ->evaluateJsonNodeValue ($ jsonNode );
88-
89- $ this ->asserter ->string ((string ) $ realValue )->contains ($ expectedValue );
91+ $ this ->assert (function () use ($ jsonNode , $ expectedValue ) {
92+ $ realValue = $ this ->evaluateJsonNodeValue ($ jsonNode );
93+ $ this ->asserter ->string ((string ) $ realValue )->contains ($ expectedValue );
94+ });
9095 }
9196
9297 /**
@@ -96,9 +101,10 @@ public function theJsonNodeShouldContain($jsonNode, $expectedValue)
96101 */
97102 public function theJsonNodeShouldNotContain ($ jsonNode , $ unexpectedValue )
98103 {
99- $ realValue = $ this ->evaluateJsonNodeValue ($ jsonNode );
100-
101- $ this ->asserter ->string ((string ) $ realValue )->notContains ($ unexpectedValue );
104+ $ this ->assert (function () use ($ jsonNode , $ unexpectedValue ) {
105+ $ realValue = $ this ->evaluateJsonNodeValue ($ jsonNode );
106+ $ this ->asserter ->string ((string ) $ realValue )->notContains ($ unexpectedValue );
107+ });
102108 }
103109
104110 /**
@@ -111,7 +117,7 @@ public function theJsonNodeShouldExist($jsonNode)
111117 try {
112118 $ this ->evaluateJsonNodeValue ($ jsonNode );
113119 } catch (\Exception $ e ) {
114- throw new \ Exception (sprintf ("The node '%s' does not exist. " , $ jsonNode ), 0 , $ e );
120+ throw new WrongJsonExpectation (sprintf ("The node '%s' does not exist. " , $ jsonNode ), $ this -> readJson () , $ e );
115121 }
116122 }
117123
@@ -131,7 +137,11 @@ public function theJsonNodeShouldNotExist($jsonNode)
131137 }
132138
133139 if ($ e === null ) {
134- throw new \Exception (sprintf ("The node '%s' exists and contains '%s'. " , $ jsonNode , json_encode ($ realValue )));
140+ throw new WrongJsonExpectation (
141+ sprintf ("The node '%s' exists and contains '%s'. " , $ jsonNode , json_encode ($ realValue )),
142+ $ this ->readJson (),
143+ $ e
144+ );
135145 }
136146 }
137147
@@ -142,9 +152,11 @@ public function theJsonShouldBeValidAccordingToThisSchema(PyStringNode $jsonSche
142152 {
143153 $ tempFilename = tempnam (sys_get_temp_dir (), 'rae ' );
144154 file_put_contents ($ tempFilename , $ jsonSchemaContent );
145- $ this ->jsonInspector ->validateJson (
146- new JsonSchema ($ tempFilename )
147- );
155+ $ this ->assert (function () use ($ tempFilename ) {
156+ $ this ->jsonInspector ->validateJson (
157+ new JsonSchema ($ tempFilename )
158+ );
159+ });
148160 unlink ($ tempFilename );
149161 }
150162
@@ -155,11 +167,11 @@ public function theJsonShouldBeValidAccordingToTheSchema($filename)
155167 {
156168 $ filename = $ this ->resolveFilename ($ filename );
157169
158- $ this ->jsonInspector -> validateJson (
159- new JsonSchema (
160- $ filename
161- )
162- );
170+ $ this ->assert ( function () use ( $ filename ) {
171+ $ this -> jsonInspector -> validateJson (
172+ new JsonSchema ( $ filename)
173+ );
174+ } );
163175 }
164176
165177 /**
@@ -175,7 +187,9 @@ public function theJsonShouldBeEqualTo(PyStringNode $jsonContent)
175187 throw new \Exception ('The expected JSON is not a valid ' );
176188 }
177189
178- $ this ->asserter ->castToString ($ realJsonValue )->isEqualTo ((string ) $ expectedJsonValue );
190+ $ this ->assert (function () use ($ realJsonValue , $ expectedJsonValue ) {
191+ $ this ->asserter ->castToString ($ realJsonValue )->isEqualTo ((string ) $ expectedJsonValue );
192+ });
179193 }
180194
181195 private function evaluateJsonNodeValue ($ jsonNode )
@@ -212,4 +226,13 @@ private function resolveFilename($filename)
212226
213227 return realpath ($ filename );
214228 }
229+
230+ private function assert (callable $ assertion )
231+ {
232+ try {
233+ $ assertion ();
234+ } catch (\Exception $ e ) {
235+ throw new WrongJsonExpectation ($ e ->getMessage (), $ this ->readJson (), $ e );
236+ }
237+ }
215238}
0 commit comments