Passed
Pull Request — master (#52)
by Baptiste
03:33
created

AbstractContext::path_should_not_be_readable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
    /** @Then /^in the json, (?:the root|\"(?P<path>(?:[^"]|\\")*)\") should be an array$/ */
169
    final public function should_be_an_array(?string $path = null): void
170
    {
171
        Assert::isArray($path === null ? $this->getJson() : $this->getValue($path));
172
    }
173
174
    /** @Then /^in the json, (?:the root|\"(?P<path>(?:[^"]|\\")*)\") collection should have (?:at (?P<limit>least|most) )?(?P<count>[1-9][0-9]*) elements?$/ */
175
    final public function the_json_path_should_have_elements(?string $path = null, ?string $limit = null, int $count): void
176
    {
177
        $value = $path === null ? $this->getJson() : $this->getValue($path);
178
179
        $assert = [Assert::class, 'count'];
180
181
        if ($limit !== null) {
182
            $assert[1] = $limit === 'most' ? 'maxCount' : 'minCount';
183
        }
184
185
        assert(is_callable($assert));
186
187
        Assert::isCountable($value);
188
        $assert($value, $count);
189
    }
190
191
    /** @Then in the json, :path should match :pattern */
192
    final public function the_json_path_should_match(string $path, string $pattern): void
193
    {
194
        Assert::regex($this->getValue($path), $pattern);
195
    }
196
197
    /**
198
     * @Then in the json, :path should not match :pattern
199
     *
200
     * -----
201
     *
202
     * Note :: The body of this assertion should be replaced by a
203
     * `Assert::notRegex` as soon as the Assert's PR
204
     * https://github.com/webmozart/assert/pull/58 is merged and released.
205
     */
206
    final public function the_json_path_should_not_match(string $path, string $pattern): void
207
    {
208
        if (!preg_match($pattern, $this->getValue($path), $matches, PREG_OFFSET_CAPTURE)) {
209
            // it's all good, it is supposed not to match. :}
210
            return;
211
        }
212
213
        throw new InvalidArgumentException("The value matches {$pattern} at offset {$matches[0][1]}");
214
    }
215
216
    /** @Then in the json, :path should be a valid json encoded string */
217
    final public function the_json_path_should_be_a_valid_json_encoded_string(string $path): void
218
    {
219
        $value = json_decode($this->getValue($path));
220
221
        Assert::notNull($value);
222
        Assert::same(json_last_error(), JSON_ERROR_NONE);
223
    }
224
225
    /**
226
     * @Then /^in the json, all the elements in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should have a(?:n?) "(?P<property>(?:[^"]|\\")*)" property$/
227
     * @Then /^in the json, all the elements in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should (?P<not>not) have a(?:n?) "(?P<property>(?:[^"]|\\")*)" property$/
228
     **/
229
    final public function the_json_path_elements_in_collection_should_have_a_property(string $path, ?string $not = null, string $property): void
230
    {
231
        $assert = [Assert::class, $not !== null ? 'allPropertyNotExists' : 'allPropertyExists'];
232
        assert(is_callable($assert));
233
234
        $assert(empty($path) ? $this->getJson() : $this->getValue($path), $property);
235
    }
236
237
    /**
238
     * @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should be equal to "(?P<expected>(?:[^"]|\\")*)"$/
239
     * @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should (?P<not>not) be equal to "(?P<expected>(?:[^"]|\\")*)"$/
240
     */
241
    final public function the_json_each_elements_in_collection_should_be_equal_to(string $path, string $property, ?string $not = null, string $expected): void
242
    {
243
        $values = empty($path) ? $this->getJson() : $this->getValue($path);
244
        Assert::isIterable($values);
245
246
        $assert = [Assert::class, $not !== null ? 'notSame' : 'same'];
247
        assert(is_callable($assert));
248
249
        foreach ($values as $element) {
250
            $assert($this->accessor->getValue($element, $property), $expected);
251
        }
252
    }
253
254
    /**
255
     * @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should be (?P<expected>true|false)$/
256
     * @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should (?P<not>not) be (?P<expected>true|false)$/
257
     */
258
    final public function the_json_each_elements_in_collection_should_be_bool(string $path, string $property, ?string $not = null, string $expected): void
259
    {
260
        $values = empty($path) ? $this->getJson() : $this->getValue($path);
261
        Assert::isIterable($values);
262
263
        $assert = [Assert::class, $not !== null ? 'notSame' : 'same'];
264
        assert(is_callable($assert));
265
266
        foreach ($values as $element) {
267
            $assert($this->accessor->getValue($element, $property), $expected === 'true');
268
        }
269
    }
270
271
    /**
272
     * @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should be equal to (?P<expected>[0-9]+)$/
273
     * @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]+)$/
274
     */
275
    final public function the_json_each_elements_in_collection_should_be_equal_to_int(string $path, string $property, ?string $not = null, int $expected): void
276
    {
277
        $values = empty($path) ? $this->getJson() : $this->getValue($path);
278
        Assert::isIterable($values);
279
280
        $assert = [Assert::class, $not !== null ? 'notSame' : 'same'];
281
        assert(is_callable($assert));
282
283
        foreach ($values as $element) {
284
            $assert($this->accessor->getValue($element, $property), $expected);
285
        }
286
    }
287
288
    /**
289
     * @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should contain "(?P<expected>(?:[^"]|\\")*)"$/
290
     * @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should (?P<not>not) contain "(?P<expected>(?:[^"]|\\")*)"$/
291
     **/
292
    final public function the_json_each_elements_in_collection_should_contain(string $path, string $property, ?string $not = null, string $expected): void
293
    {
294
        $values = empty($path) ? $this->getJson() : $this->getValue($path);
295
        Assert::isIterable($values);
296
297
        $assert = [Assert::class, $not !== null ? 'notSame' : 'same'];
298
        assert(is_callable($assert));
299
300
        foreach ($values as $element) {
301
            $assert($this->accessor->getValue($element, $property), $expected);
302
        }
303
    }
304
}
305