3
3
namespace Behatch \Context ;
4
4
5
5
use Behat \Gherkin \Node \PyStringNode ;
6
-
7
6
use Behat \Gherkin \Node \TableNode ;
8
- use Behat \ Mink \ Exception \ ExpectationException ;
7
+ use Behatch \ HttpCall \ HttpCallResultPool ;
9
8
use Behatch \Json \Json ;
10
- use Behatch \Json \JsonSchema ;
11
9
use Behatch \Json \JsonInspector ;
12
- use Behatch \HttpCall \ HttpCallResultPool ;
10
+ use Behatch \Json \ JsonSchema ;
13
11
14
12
class JsonContext extends BaseContext
15
13
{
@@ -49,17 +47,17 @@ public function theResponseShouldNotBeInJson()
49
47
/**
50
48
* Checks, that given JSON node is equal to given value
51
49
*
52
- * @Then the JSON node :node should be equal to :text
50
+ * @Then the JSON node :node should be equal to :expected
53
51
*/
54
- public function theJsonNodeShouldBeEqualTo ($ node , $ text )
52
+ public function theJsonNodeShouldBeEqualTo ($ node , $ expected )
55
53
{
56
54
$ json = $ this ->getJson ();
57
55
58
56
$ actual = $ this ->inspector ->evaluate ($ json , $ node );
59
57
60
- if ($ actual != $ text ) {
58
+ if ($ actual != $ expected ) {
61
59
throw new \Exception (
62
- sprintf ("The node value is '%s' " , json_encode ($ actual ))
60
+ sprintf ("The node '%s' value is '%s', '%s' expected " , $ node , json_encode ($ actual), json_encode ( $ expected ))
63
61
);
64
62
}
65
63
}
@@ -89,7 +87,7 @@ public function theJsonNodeShouldMatch($node, $pattern)
89
87
90
88
if (preg_match ($ pattern , $ actual ) === 0 ) {
91
89
throw new \Exception (
92
- sprintf ("The node value is '%s' " , json_encode ($ actual ))
90
+ sprintf ("The node '%s' value is '%s', '%s' pattern expected " , $ node , json_encode ($ actual ), $ pattern )
93
91
);
94
92
}
95
93
}
@@ -107,7 +105,7 @@ public function theJsonNodeShouldBeNull($node)
107
105
108
106
if (null !== $ actual ) {
109
107
throw new \Exception (
110
- sprintf (' The node value is `%s` ' , json_encode ($ actual ))
108
+ sprintf (" The node '%s' value is '%s', null expected " , $ node , json_encode ($ actual ))
111
109
);
112
110
}
113
111
}
@@ -119,9 +117,15 @@ public function theJsonNodeShouldBeNull($node)
119
117
*/
120
118
public function theJsonNodeShouldNotBeNull ($ node )
121
119
{
122
- $ this ->not (function () use ($ node ) {
123
- return $ this ->theJsonNodeShouldBeNull ($ node );
124
- }, sprintf ('The node %s should not be null ' , $ node ));
120
+ $ json = $ this ->getJson ();
121
+
122
+ $ actual = $ this ->inspector ->evaluate ($ json , $ node );
123
+
124
+ if (null === $ actual ) {
125
+ throw new \Exception (
126
+ sprintf ("The node '%s' value is null, '%s' expected " , $ node , json_encode ($ actual ))
127
+ );
128
+ }
125
129
}
126
130
127
131
/**
@@ -137,7 +141,7 @@ public function theJsonNodeShouldBeTrue($node)
137
141
138
142
if (true !== $ actual ) {
139
143
throw new \Exception (
140
- sprintf (' The node value is `%s` ' , json_encode ($ actual ))
144
+ sprintf (" The node '%s' value is '%s', 'true' expected " , $ node , json_encode ($ actual ))
141
145
);
142
146
}
143
147
}
@@ -155,25 +159,25 @@ public function theJsonNodeShouldBeFalse($node)
155
159
156
160
if (false !== $ actual ) {
157
161
throw new \Exception (
158
- sprintf (' The node value is `%s` ' , json_encode ($ actual ))
162
+ sprintf (" The node '%s' value is '%s', 'false' expected " , $ node , json_encode ($ actual ))
159
163
);
160
164
}
161
165
}
162
166
163
167
/**
164
168
* Checks, that given JSON node is equal to the given string
165
169
*
166
- * @Then the JSON node :node should be equal to the string :text
170
+ * @Then the JSON node :node should be equal to the string :expected
167
171
*/
168
- public function theJsonNodeShouldBeEqualToTheString ($ node , $ text )
172
+ public function theJsonNodeShouldBeEqualToTheString ($ node , $ expected )
169
173
{
170
174
$ json = $ this ->getJson ();
171
175
172
176
$ actual = $ this ->inspector ->evaluate ($ json , $ node );
173
177
174
- if ($ actual !== $ text ) {
178
+ if ($ actual !== $ expected ) {
175
179
throw new \Exception (
176
- sprintf (' The node value is `%s` ' , json_encode ($ actual ))
180
+ sprintf (" The node '%s' value is '%s ', string '%s' expected " , $ node , json_encode ($ actual ), $ expected )
177
181
);
178
182
}
179
183
}
@@ -191,7 +195,7 @@ public function theJsonNodeShouldBeEqualToTheNumber($node, $number)
191
195
192
196
if ($ actual !== (float ) $ number && $ actual !== (int ) $ number ) {
193
197
throw new \Exception (
194
- sprintf (' The node value is `%s` ' , json_encode ($ actual ))
198
+ sprintf (" The node '%s' value is '%s ', numder '%6' expected " , $ node , json_encode ($ actual ), ( float ) $ number )
195
199
);
196
200
}
197
201
}
@@ -207,7 +211,7 @@ public function theJsonNodeShouldHaveElements($node, $count)
207
211
208
212
$ actual = $ this ->inspector ->evaluate ($ json , $ node );
209
213
210
- $ this ->assertSame ($ count , sizeof ((array ) $ actual ));
214
+ $ this ->assertSame ($ count , count ((array ) $ actual ));
211
215
}
212
216
213
217
/**
@@ -276,6 +280,7 @@ public function theJsonNodeShouldExist($name)
276
280
} catch (\Exception $ e ) {
277
281
throw new \Exception ("The node ' $ name' does not exist. " );
278
282
}
283
+
279
284
return $ node ;
280
285
}
281
286
@@ -337,7 +342,7 @@ public function theJsonShouldBeInvalidAccordingToTheSchema($filename)
337
342
338
343
$ this ->not (function () use ($ filename ) {
339
344
return $ this ->theJsonShouldBeValidAccordingToTheSchema ($ filename );
340
- }, " The schema was valid " );
345
+ }, ' The schema was valid ' );
341
346
}
342
347
343
348
/**
@@ -356,7 +361,7 @@ public function theJsonShouldBeEqualTo(PyStringNode $content)
356
361
$ this ->assertSame (
357
362
(string ) $ expected ,
358
363
(string ) $ actual ,
359
- "The json is equal to: \n" . $ actual ->encode ()
364
+ "The json is equal to: \n" . $ actual ->encode ()
360
365
);
361
366
}
362
367
@@ -390,8 +395,8 @@ public function theJsonShouldBeValidAccordingToTheSwaggerSchema($dumpPath, $sche
390
395
)
391
396
);
392
397
}
398
+
393
399
/**
394
- *
395
400
* Checks, that response JSON not matches with a swagger dump
396
401
*
397
402
* @Then the JSON should not be valid according to swagger :dumpPath dump schema :schemaName
@@ -403,8 +408,6 @@ public function theJsonShouldNotBeValidAccordingToTheSwaggerSchema($dumpPath, $s
403
408
}, 'JSON Schema matches but it should not ' );
404
409
}
405
410
406
-
407
-
408
411
protected function getJson ()
409
412
{
410
413
return new Json ($ this ->httpCallResultPool ->getResult ()->getValue ());
0 commit comments