This repository was archived by the owner on Apr 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathJsonContext.php
More file actions
294 lines (263 loc) · 8.95 KB
/
Copy pathJsonContext.php
File metadata and controls
294 lines (263 loc) · 8.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
namespace Sanpi\Behatch\Context;
use Behat\Gherkin\Node\PyStringNode;
use Sanpi\Behatch\Json\Json;
use Sanpi\Behatch\Json\JsonSchema;
use Sanpi\Behatch\Json\JsonInspector;
use Sanpi\Behatch\HttpCall\HttpCallResultPool;
class JsonContext extends BaseContext
{
private $inspector;
private $httpCallResultPool;
public function __construct(HttpCallResultPool $httpCallResultPool, $evaluationMode = 'javascript')
{
$this->inspector = new JsonInspector($evaluationMode);
$this->httpCallResultPool = $httpCallResultPool;
}
/**
* Checks, that the response is correct JSON
* Example: Then the response should be in JSON
* Example: And the response should be in JSON
*
* @Then the response should be in JSON
*/
public function theResponseShouldBeInJson()
{
$this->getJson();
}
/**
* Checks, that the response is not correct JSON
* Example: Then the response should not be in JSON
* Example: And the response should not be in JSON
*
* @Then the response should not be in JSON
*/
public function theResponseShouldNotBeInJson()
{
$this->not(
[$this, 'theResponseShouldBeInJson'],
'The response is in JSON'
);
}
/**
* Checks, that given JSON node is equal to given value
* Example: Then the JSON node "isBatman"should be equal to "true"
* Example: And the JSON node "isBatman" should be equal to "true"
*
* @Then the JSON node :node should be equal to :text
*/
public function theJsonNodeShouldBeEqualTo($node, $text)
{
$json = $this->getJson();
$actual = $this->inspector->evaluate($json, $node);
if ($actual != $text) {
throw new \Exception(
sprintf("The node value is '%s'", json_encode($actual))
);
}
}
/**
* Checks, that given JSON node has N element(s)
* Example: Then the JSON node "bio" should have 5 elements
* Example: And the JSON node "bio" should have 5 elements
*
* @Then the JSON node :node should have :count element(s)
*/
public function theJsonNodeShouldHaveElements($node, $count)
{
$json = $this->getJson();
$actual = $this->inspector->evaluate($json, $node);
$this->assertSame($count, sizeof((array) $actual));
}
/**
* Checks, that given JSON node contains given value
* Example: Then the JSON node "trueIdentity" should contain "Bruce Wayne"
* Example: And the JSON node "trueIdentity" should contain "Bruce Wayne"
*
* @Then the JSON node :node should contain :text
*/
public function theJsonNodeShouldContain($node, $text)
{
$json = $this->getJson();
$actual = $this->inspector->evaluate($json, $node);
$this->assertContains($text, (string) $actual);
}
/**
* Checks, that given JSON node does not contain given value
* Example: Then the JSON node "isRobin" should not contain "true"
* Example: And the JSON node "isRobin" should not contain "true"
*
* @Then the JSON node :node should not contain :text
*/
public function theJsonNodeShouldNotContain($node, $text)
{
$json = $this->getJson();
$actual = $this->inspector->evaluate($json, $node);
$this->assertNotContains($text, (string) $actual);
}
/**
* Checks, that given JSON node exist
* Example: Then the JSON node "id" should exist
* Example: And the JSON node "id" should exist
*
* @Given the JSON node :name should exist
*/
public function theJsonNodeShouldExist($name)
{
$json = $this->getJson();
try {
$node = $this->inspector->evaluate($json, $name);
}
catch (\Exception $e) {
throw new \Exception("The node '$name' does not exist.");
}
return $node;
}
/**
* Checks, that given JSON node does not exist
* Example: Then the JSON node "Robin" should not exist
* Example: And the JSON node "Robin" should not exist
*
* @Given the JSON node :name should not exist
*/
public function theJsonNodeShouldNotExist($name)
{
$this->not(function () use($name) {
return $this->theJsonNodeShouldExist($name);
}, "The node '$name' exists.");
}
/**
* Checks provided JSON against a JSON schema
* Example: Then the JSON should be valid according to this schema:
* """
* {
* "$schema": "http://json-schema.org/draft-04/schema#",
* "title": "Heroes",
* "description": "A list of heroes from the known universe",
* "type": "object",
* "properties": {
* "id": {
* "description": "The unique identifier for a hero",
* "type": "integer"
* },
* "name": {
* "description": "Name of the hero",
* "type": "string"
* },
* "alterEgo": {
* "description": "Alter ego for hero",
* "type": "string"
* }
* },
* "required": ["id", "name", "alterEgo"]
* }
* """
* Example: And the JSON should be valid according to this schema:
* """
* {
* "$schema": "http://json-schema.org/draft-04/schema#",
* "title": "Heroes",
* "description": "A list of heroes from the known universe",
* "type": "object",
* "properties": {
* "id": {
* "description": "The unique identifier for a hero",
* "type": "integer"
* },
* "name": {
* "description": "Name of the hero",
* "type": "string"
* },
* "alterEgo": {
* "description": "Alter ego for hero",
* "type": "string"
* }
* },
* "required": ["id", "name", "alterEgo"]
* }
* """
*
* @Then the JSON should be valid according to this schema:
*/
public function theJsonShouldBeValidAccordingToThisSchema(PyStringNode $schema)
{
$this->inspector->validate(
$this->getJson(),
new JsonSchema($schema)
);
}
/**
* Checks provided JSON against a provided JSON schema
* Example: Then the JSON should be valid according to the schema "http://batman.com/secret-schema"
* Example: And the JSON should be valid according to the schema "http://batman.com/secret-schema"
*
* @Then the JSON should be valid according to the schema :filename
*/
public function theJsonShouldBeValidAccordingToTheSchema($filename)
{
if (false === is_file($filename)) {
throw new \RuntimeException(
'The JSON schema doesn\'t exist'
);
}
$this->inspector->validate(
$this->getJson(),
new JsonSchema(
file_get_contents($filename),
'file://' . getcwd() . '/' . $filename
)
);
}
/**
* Checks that the JSON response is equal to value
* Example: Then the JSON should be equal to:
* """
* {
* "id": 1,
* "name": "Batman",
* "alterEgo": "Bruce Wayne"
* }
* """
* Example: And the JSON should be equal to:
* """
* {
* "id": 1,
* "name": "Batman",
* "alterEgo": "Bruce Wayne"
* }
* """
*
* @Then the JSON should be equal to:
*/
public function theJsonShouldBeEqualTo(PyStringNode $content)
{
$actual = $this->getJson();
try {
$expected = new Json($content);
}
catch (\Exception $e) {
throw new \Exception('The expected JSON is not a valid');
}
$this->assertSame(
(string) $expected,
(string) $actual,
"The json is equal to:\n". $actual->encode()
);
}
/**
* Prints last JSON response
* Example: Then print last JSON response
* Example: And print last JSON response
*
* @Then print last JSON response
*/
public function printLastJsonResponse()
{
echo $this->getJson()
->encode();
}
private function getJson()
{
return new Json($this->httpCallResultPool->getResult()->getValue());
}
}