1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
namespace Behapi\Json; |
3
|
|
|
|
4
|
|
|
use stdClass; |
5
|
|
|
use DateTime; |
6
|
|
|
|
7
|
|
|
use Throwable; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
use Behat\Gherkin\Node\PyStringNode; |
11
|
|
|
use Behat\Behat\Context\Context as BehatContext; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
14
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
15
|
|
|
|
16
|
|
|
use Webmozart\Assert\Assert; |
17
|
|
|
|
18
|
|
|
use function preg_match; |
19
|
|
|
use function json_last_error; |
20
|
|
|
|
21
|
|
|
use const JSON_ERROR_NONE; |
22
|
|
|
use const PREG_OFFSET_CAPTURE; |
23
|
|
|
|
24
|
|
|
abstract class AbstractContext implements BehatContext |
25
|
|
|
{ |
26
|
|
|
/** @var PropertyAccessor */ |
27
|
|
|
private $accessor; |
28
|
|
|
|
29
|
|
|
public function __construct() |
30
|
|
|
{ |
31
|
|
|
$this->accessor = PropertyAccess::createPropertyAccessor(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
abstract protected function getJson(); |
35
|
|
|
|
36
|
|
|
protected function getValue(string $path) |
37
|
|
|
{ |
38
|
|
|
return $this->accessor->getValue($this->getJson(), $path); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** @Then :path should be accessible in the latest json response */ |
42
|
|
|
final public function path_should_be_readable(string $path): void |
43
|
|
|
{ |
44
|
|
|
Assert::true($this->accessor->isReadable($this->getJson(), $path), "The path $path should be a valid path"); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** @Then :path should not exist in the latest json response */ |
48
|
|
|
final public function path_should_not_be_readable(string $path): void |
49
|
|
|
{ |
50
|
|
|
Assert::false($this->accessor->isReadable($this->getJson(), $path), "The path $path should not be a valid path"); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @Then in the json, :path should be equal to :expected |
55
|
|
|
* @Then in the json, :path should :not be equal to :expected |
56
|
|
|
*/ |
57
|
|
|
final public function the_json_path_should_be_equal_to(string $path, ?string $not = null, $expected): void |
58
|
|
|
{ |
59
|
|
|
$assert = [Assert::class, $not !== null ? 'notEq' : 'eq']; |
60
|
|
|
assert(is_callable($assert)); |
61
|
|
|
|
62
|
|
|
$assert($this->getValue($path), $expected); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** @Then in the json, :path should be: */ |
66
|
|
|
final public function the_json_path_should_be_py_string(string $path, PyStringNode $expected): void |
67
|
|
|
{ |
68
|
|
|
Assert::same($this->getValue($path), $expected->getRaw()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @Then /^in the json, "(?P<path>(?:[^"]|\\")*)" should be (?P<expected>true|false)$/ |
73
|
|
|
* @Then /^in the json, "(?P<path>(?:[^"]|\\")*)" should :not be (?P<expected>true|false)$/ |
74
|
|
|
*/ |
75
|
|
|
final public function the_json_path_should_be(string $path, ?string $not = null, string $expected): void |
76
|
|
|
{ |
77
|
|
|
$assert = [Assert::class, $not !== null ? 'notSame' : 'same']; |
78
|
|
|
assert(is_callable($assert)); |
79
|
|
|
|
80
|
|
|
$assert($this->getValue($path), 'true' === $expected); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @Then in the json, :path should be null |
85
|
|
|
* @Then in the json, :path should :not be null |
86
|
|
|
*/ |
87
|
|
|
final public function the_json_path_should_be_null(string $path, ?string $not = null): void |
88
|
|
|
{ |
89
|
|
|
$assert = [Assert::class, $not !== null ? 'notNull' : 'null']; |
90
|
|
|
assert(is_callable($assert)); |
91
|
|
|
|
92
|
|
|
$assert($this->getValue($path)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @Then in the json, :path should be empty |
97
|
|
|
* @Then in the json, :path should :not be empty |
98
|
|
|
*/ |
99
|
|
|
final public function the_json_path_should_be_empty(string $path, ?string $not = null): void |
100
|
|
|
{ |
101
|
|
|
$assert = [Assert::class, $not !== null ? 'notEmpty' : 'isEmpty']; |
102
|
|
|
assert(is_callable($assert)); |
103
|
|
|
|
104
|
|
|
$assert($this->getValue($path)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @Then in the json, :path should contain :expected |
109
|
|
|
* @Then in the json, :path should :not contain :expected |
110
|
|
|
*/ |
111
|
|
|
final public function the_json_path_contains(string $path, ?string $not = null, $expected): void |
112
|
|
|
{ |
113
|
|
|
$assert = [Assert::class, $not !== null ? 'notContains' : 'contains']; |
114
|
|
|
assert(is_callable($assert)); |
115
|
|
|
|
116
|
|
|
$assert($this->getValue($path), $expected); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** @Then in the json, :path collection should contain an element with :value equal to :expected */ |
120
|
|
|
final public function the_json_path_collection_contains(string $path, string $value, $expected): void |
121
|
|
|
{ |
122
|
|
|
$collection = $this->accessor->getValue($this->getJson(), $path); |
123
|
|
|
Assert::isIterable($collection); |
124
|
|
|
|
125
|
|
|
foreach ($collection as $element) { |
126
|
|
|
if ($expected === $this->accessor->getValue($element, $value)) { |
127
|
|
|
return; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
throw new InvalidArgumentException("$path collection does not contain an element with $value equal to $expected"); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** @Then in the json, :path should be a valid date(time) */ |
135
|
|
|
final public function the_json_path_should_be_a_valid_date(string $path): void |
136
|
|
|
{ |
137
|
|
|
try { |
138
|
|
|
new DateTime($this->getValue($path)); |
139
|
|
|
} catch (Throwable $t) { |
140
|
|
|
throw new InvalidArgumentException("$path does not contain a valid date"); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** @Then in the json, :path should be greater than :expected */ |
145
|
|
|
final public function the_json_path_should_be_greater_than(string $path, int $expected): void |
146
|
|
|
{ |
147
|
|
|
Assert::greaterThan($this->getValue($path), $expected); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** @Then in the json, :path should be greater than or equal to :expected */ |
151
|
|
|
final public function the_json_path_should_be_greater_or_equal_than(string $path, int $expected): void |
152
|
|
|
{ |
153
|
|
|
Assert::greaterThanEq($this->getValue($path), $expected); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** @Then in the json, :path should be less than :expected */ |
157
|
|
|
final public function the_json_path_should_be_less_than(string $path, int $expected): void |
158
|
|
|
{ |
159
|
|
|
Assert::lessThan($this->getValue($path), $expected); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** @Then in the json, :path should be less than or equal to :expected */ |
163
|
|
|
final public function the_json_path_should_be_less_or_equal_than(string $path, int $expected): void |
164
|
|
|
{ |
165
|
|
|
Assert::lessThanEq($this->getValue($path), $expected); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @Then in the json, the root should be an array |
170
|
|
|
* @Then in the json, :path should be an array |
171
|
|
|
*/ |
172
|
|
|
final public function should_be_an_array(?string $path = null): void |
173
|
|
|
{ |
174
|
|
|
Assert::isArray($path === null ? $this->getJson() : $this->getValue($path)); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @Then in the json, the root should have at :limit :count element(s) |
179
|
|
|
* @Then in the json, :path should have at :limit :count element(s) |
180
|
|
|
*/ |
181
|
|
|
final public function the_json_collection_should_have_limit_elements(?string $path = null, string $limit, int $count): void |
182
|
|
|
{ |
183
|
|
|
$value = $path === null ? $this->getJson() : $this->getValue($path); |
184
|
|
|
$assert = [Assert::class, $limit === 'most' ? 'maxCount' : 'minCount']; |
185
|
|
|
|
186
|
|
|
assert(is_callable($assert)); |
187
|
|
|
|
188
|
|
|
Assert::isCountable($value); |
189
|
|
|
$assert($value, $count); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @Then in the json, the root should have :count element(s) |
194
|
|
|
* @Then in the json, :path should have :count element(s) |
195
|
|
|
*/ |
196
|
|
|
final public function the_json_path_should_have_elements(?string $path = null, int $count): void |
197
|
|
|
{ |
198
|
|
|
$value = $path === null ? $this->getJson() : $this->getValue($path); |
199
|
|
|
|
200
|
|
|
Assert::isCountable($value); |
201
|
|
|
Assert::count($value, $count); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** @Then in the json, :path should match :pattern */ |
205
|
|
|
final public function the_json_path_should_match(string $path, string $pattern): void |
206
|
|
|
{ |
207
|
|
|
Assert::regex($this->getValue($path), $pattern); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @Then in the json, :path should not match :pattern |
212
|
|
|
* |
213
|
|
|
* ----- |
214
|
|
|
* |
215
|
|
|
* Note :: The body of this assertion should be replaced by a |
216
|
|
|
* `Assert::notRegex` as soon as the Assert's PR |
217
|
|
|
* https://github.com/webmozart/assert/pull/58 is merged and released. |
218
|
|
|
*/ |
219
|
|
|
final public function the_json_path_should_not_match(string $path, string $pattern): void |
220
|
|
|
{ |
221
|
|
|
if (!preg_match($pattern, $this->getValue($path), $matches, PREG_OFFSET_CAPTURE)) { |
222
|
|
|
// it's all good, it is supposed not to match. :} |
223
|
|
|
return; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
throw new InvalidArgumentException("The value matches {$pattern} at offset {$matches[0][1]}"); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** @Then in the json, :path should be a valid json encoded string */ |
230
|
|
|
final public function the_json_path_should_be_a_valid_json_encoded_string(string $path): void |
231
|
|
|
{ |
232
|
|
|
$value = json_decode($this->getValue($path)); |
233
|
|
|
|
234
|
|
|
Assert::notNull($value); |
235
|
|
|
Assert::same(json_last_error(), JSON_ERROR_NONE); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @Then /^in the json, all the elements in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should have a(?:n?) "(?P<property>(?:[^"]|\\")*)" property$/ |
240
|
|
|
* @Then /^in the json, all the elements in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should (?P<not>not) have a(?:n?) "(?P<property>(?:[^"]|\\")*)" property$/ |
241
|
|
|
**/ |
242
|
|
|
final public function the_json_path_elements_in_collection_should_have_a_property(string $path, ?string $not = null, string $property): void |
243
|
|
|
{ |
244
|
|
|
$assert = [Assert::class, $not !== null ? 'allPropertyNotExists' : 'allPropertyExists']; |
245
|
|
|
assert(is_callable($assert)); |
246
|
|
|
|
247
|
|
|
$assert(empty($path) ? $this->getJson() : $this->getValue($path), $property); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should be equal to "(?P<expected>(?:[^"]|\\")*)"$/ |
252
|
|
|
* @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should (?P<not>not) be equal to "(?P<expected>(?:[^"]|\\")*)"$/ |
253
|
|
|
*/ |
254
|
|
|
final public function the_json_each_elements_in_collection_should_be_equal_to(string $path, string $property, ?string $not = null, string $expected): void |
255
|
|
|
{ |
256
|
|
|
$values = empty($path) ? $this->getJson() : $this->getValue($path); |
257
|
|
|
Assert::isIterable($values); |
258
|
|
|
|
259
|
|
|
$assert = [Assert::class, $not !== null ? 'notSame' : 'same']; |
260
|
|
|
assert(is_callable($assert)); |
261
|
|
|
|
262
|
|
|
foreach ($values as $element) { |
263
|
|
|
$assert($this->accessor->getValue($element, $property), $expected); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should be (?P<expected>true|false)$/ |
269
|
|
|
* @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should (?P<not>not) be (?P<expected>true|false)$/ |
270
|
|
|
*/ |
271
|
|
|
final public function the_json_each_elements_in_collection_should_be_bool(string $path, string $property, ?string $not = null, string $expected): void |
272
|
|
|
{ |
273
|
|
|
$values = empty($path) ? $this->getJson() : $this->getValue($path); |
274
|
|
|
Assert::isIterable($values); |
275
|
|
|
|
276
|
|
|
$assert = [Assert::class, $not !== null ? 'notSame' : 'same']; |
277
|
|
|
assert(is_callable($assert)); |
278
|
|
|
|
279
|
|
|
foreach ($values as $element) { |
280
|
|
|
$assert($this->accessor->getValue($element, $property), $expected === 'true'); |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should be equal to (?P<expected>[0-9]+)$/ |
286
|
|
|
* @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should (?P<not>not) be equal to (?P<expected>[0-9]+)$/ |
287
|
|
|
*/ |
288
|
|
|
final public function the_json_each_elements_in_collection_should_be_equal_to_int(string $path, string $property, ?string $not = null, int $expected): void |
289
|
|
|
{ |
290
|
|
|
$values = empty($path) ? $this->getJson() : $this->getValue($path); |
291
|
|
|
Assert::isIterable($values); |
292
|
|
|
|
293
|
|
|
$assert = [Assert::class, $not !== null ? 'notSame' : 'same']; |
294
|
|
|
assert(is_callable($assert)); |
295
|
|
|
|
296
|
|
|
foreach ($values as $element) { |
297
|
|
|
$assert($this->accessor->getValue($element, $property), $expected); |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should contain "(?P<expected>(?:[^"]|\\")*)"$/ |
303
|
|
|
* @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should (?P<not>not) contain "(?P<expected>(?:[^"]|\\")*)"$/ |
304
|
|
|
**/ |
305
|
|
|
final public function the_json_each_elements_in_collection_should_contain(string $path, string $property, ?string $not = null, string $expected): void |
306
|
|
|
{ |
307
|
|
|
$values = empty($path) ? $this->getJson() : $this->getValue($path); |
308
|
|
|
Assert::isIterable($values); |
309
|
|
|
|
310
|
|
|
$assert = [Assert::class, $not !== null ? 'notSame' : 'same']; |
311
|
|
|
assert(is_callable($assert)); |
312
|
|
|
|
313
|
|
|
foreach ($values as $element) { |
314
|
|
|
$assert($this->accessor->getValue($element, $property), $expected); |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|