Completed
Push — 3.x ( 3697fd...aa4b65 )
by San
04:48
created

JsonContext::theJsonNodeShouldBeNull()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 12
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace Behatch\Context;
4
5
use Behat\Gherkin\Node\PyStringNode;
6
7
use Behat\Gherkin\Node\TableNode;
8
use Behat\Mink\Exception\ExpectationException;
9
use Behatch\Json\Json;
10
use Behatch\Json\JsonSchema;
11
use Behatch\Json\JsonInspector;
12
use Behatch\HttpCall\HttpCallResultPool;
13
14
class JsonContext extends BaseContext
15
{
16
    protected $inspector;
17
18
    protected $httpCallResultPool;
19
20
    public function __construct(HttpCallResultPool $httpCallResultPool, $evaluationMode = 'javascript')
21
    {
22
        $this->inspector = new JsonInspector($evaluationMode);
23
        $this->httpCallResultPool = $httpCallResultPool;
24
    }
25
26
    /**
27
     * Checks, that the response is correct JSON
28
     *
29
     * @Then the response should be in JSON
30
     */
31
    public function theResponseShouldBeInJson()
32
    {
33
        $this->getJson();
34
    }
35
36
    /**
37
     * Checks, that the response is not correct JSON
38
     *
39
     * @Then the response should not be in JSON
40
     */
41
    public function theResponseShouldNotBeInJson()
42
    {
43
        $this->not(
44
            [$this, 'theResponseShouldBeInJson'],
45
            'The response is in JSON'
46
        );
47
    }
48
49
    /**
50
     * Checks, that given JSON node is equal to given value
51
     *
52
     * @Then the JSON node :node should be equal to :text
53
     */
54
    public function theJsonNodeShouldBeEqualTo($node, $text)
55
    {
56
        $json = $this->getJson();
57
58
        $actual = $this->inspector->evaluate($json, $node);
59
60
        if ($actual != $text) {
61
            throw new \Exception(
62
                sprintf("The node value is '%s'", json_encode($actual))
63
            );
64
        }
65
    }
66
67
    /**
68
     * Checks, that given JSON nodes are equal to givens values
69
     *
70
     * @Then the JSON nodes should be equal to:
71
     */
72
    public function theJsonNodesShoudBeEqualTo(TableNode $nodes)
73
    {
74
        foreach ($nodes->getRowsHash() as $node => $text) {
75
            $this->theJsonNodeShouldBeEqualTo($node, $text);
76
        }
77
    }
78
79
    /**
80
     * Checks, that given JSON node is null
81
     *
82
     * @Then the JSON node :node should be null
83
     */
84
    public function theJsonNodeShouldBeNull($node)
85
    {
86
        $json = $this->getJson();
87
88
        $actual = $this->inspector->evaluate($json, $node);
89
90
        if (null !== $actual) {
91
            throw new \Exception(
92
                sprintf('The node value is `%s`', json_encode($actual))
93
            );
94
        }
95
    }
96
97
    /**
98
     * Checks, that given JSON node is true
99
     *
100
     * @Then the JSON node :node should be true
101
     */
102
    public function theJsonNodeShouldBeTrue($node)
103
    {
104
        $json = $this->getJson();
105
106
        $actual = $this->inspector->evaluate($json, $node);
107
108
        if (true !== $actual) {
109
            throw new \Exception(
110
                sprintf('The node value is `%s`', json_encode($actual))
111
            );
112
        }
113
    }
114
115
    /**
116
     * Checks, that given JSON node is false
117
     *
118
     * @Then the JSON node :node should be false
119
     */
120
    public function theJsonNodeShouldBeFalse($node)
121
    {
122
        $json = $this->getJson();
123
124
        $actual = $this->inspector->evaluate($json, $node);
125
126
        if (false !== $actual) {
127
            throw new \Exception(
128
                sprintf('The node value is `%s`', json_encode($actual))
129
            );
130
        }
131
    }
132
133
    /**
134
     * Checks, that given JSON node is equal to the given string
135
     *
136
     * @Then the JSON node :node should be equal to the string :text
137
     */
138 View Code Duplication
    public function theJsonNodeShouldBeEqualToTheString($node, $text)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
    {
140
        $json = $this->getJson();
141
142
        $actual = $this->inspector->evaluate($json, $node);
143
144
        if ($actual !== $text) {
145
            throw new \Exception(
146
                sprintf('The node value is `%s`', json_encode($actual))
147
            );
148
        }
149
    }
150
151
    /**
152
     * Checks, that given JSON node is equal to the given number
153
     *
154
     * @Then the JSON node :node should be equal to the number :number
155
     */
156 View Code Duplication
    public function theJsonNodeShouldBeEqualToTheNumber($node, $number)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
157
    {
158
        $json = $this->getJson();
159
160
        $actual = $this->inspector->evaluate($json, $node);
161
162
        if ($actual !== (float) $number && $actual !== (int) $number) {
163
            throw new \Exception(
164
                sprintf('The node value is `%s`', json_encode($actual))
165
            );
166
        }
167
    }
168
169
    /**
170
     * Checks, that given JSON node has N element(s)
171
     *
172
     * @Then the JSON node :node should have :count element(s)
173
     */
174
    public function theJsonNodeShouldHaveElements($node, $count)
175
    {
176
        $json = $this->getJson();
177
178
        $actual = $this->inspector->evaluate($json, $node);
179
180
        $this->assertSame($count, sizeof((array) $actual));
181
    }
182
183
    /**
184
     * Checks, that given JSON node contains given value
185
     *
186
     * @Then the JSON node :node should contain :text
187
     */
188 View Code Duplication
    public function theJsonNodeShouldContain($node, $text)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
189
    {
190
        $json = $this->getJson();
191
192
        $actual = $this->inspector->evaluate($json, $node);
193
194
        $this->assertContains($text, (string) $actual);
195
    }
196
197
    /**
198
     * Checks, that given JSON nodes contains values
199
     *
200
     * @Then the JSON nodes should contain:
201
     */
202
    public function theJsonNodesShoudContain(TableNode $nodes)
203
    {
204
        foreach ($nodes->getRowsHash() as $node => $text) {
205
            $this->theJsonNodeShouldContain($node, $text);
206
        }
207
    }
208
209
    /**
210
     * Checks, that given JSON node does not contain given value
211
     *
212
     * @Then the JSON node :node should not contain :text
213
     */
214 View Code Duplication
    public function theJsonNodeShouldNotContain($node, $text)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
215
    {
216
        $json = $this->getJson();
217
218
        $actual = $this->inspector->evaluate($json, $node);
219
220
        $this->assertNotContains($text, (string) $actual);
221
    }
222
223
    /**
224
     * Checks, that given JSON nodes does not contain given value
225
     *
226
     * @Then the JSON nodes should not contain:
227
     */
228
    public function theJsonNodesShoudNotContain(TableNode $nodes)
229
    {
230
        foreach ($nodes->getRowsHash() as $node => $text) {
231
            $this->theJsonNodeShouldNotContain($node, $text);
232
        }
233
    }
234
235
    /**
236
     * Checks, that given JSON node exist
237
     *
238
     * @Given the JSON node :name should exist
239
     */
240
    public function theJsonNodeShouldExist($name)
241
    {
242
        $json = $this->getJson();
243
244
        try {
245
            $node = $this->inspector->evaluate($json, $name);
246
        }
247
        catch (\Exception $e) {
248
            throw new \Exception("The node '$name' does not exist.");
249
        }
250
        return $node;
251
    }
252
253
    /**
254
     * Checks, that given JSON node does not exist
255
     *
256
     * @Given the JSON node :name should not exist
257
     */
258
    public function theJsonNodeShouldNotExist($name)
259
    {
260
        $this->not(function () use($name) {
261
            return $this->theJsonNodeShouldExist($name);
262
        }, "The node '$name' exists.");
263
    }
264
265
    /**
266
     * @Then the JSON should be valid according to this schema:
267
     */
268
    public function theJsonShouldBeValidAccordingToThisSchema(PyStringNode $schema)
269
    {
270
        $this->inspector->validate(
271
            $this->getJson(),
272
            new JsonSchema($schema)
273
        );
274
    }
275
276
    /**
277
     * @Then the JSON should be invalid according to this schema:
278
     */
279
    public function theJsonShouldBeInvalidAccordingToThisSchema(PyStringNode $schema)
280
    {
281
        try {
282
            $isValid = $this->inspector->validate(
283
                $this->getJson(),
284
                new JsonSchema($schema)
285
            );
286
287
        } catch (\Exception $e) {
288
            $isValid = false;
289
        }
290
291
        if (true === $isValid) {
292
            throw new ExpectationException('Expected to receive invalid json, got valid one', $this->getSession());
293
        }
294
    }
295
296
    /**
297
     * @Then the JSON should be valid according to the schema :filename
298
     */
299
    public function theJsonShouldBeValidAccordingToTheSchema($filename)
300
    {
301
        $this->checkSchemaFile($filename);
302
303
        $this->inspector->validate(
304
            $this->getJson(),
305
            new JsonSchema(
306
                file_get_contents($filename),
307
                'file://' . getcwd() . '/' . $filename
308
            )
309
        );
310
    }
311
312
    /**
313
     * @Then the JSON should be invalid according to the schema :filename
314
     */
315
    public function theJsonShouldBeInvalidAccordingToTheSchema($filename)
316
    {
317
        $this->checkSchemaFile($filename);
318
319
        $this->not(function () use($filename) {
320
            return $this->theJsonShouldBeValidAccordingToTheSchema($filename);
321
        }, "The schema was valid");
322
    }
323
324
    /**
325
     * @Then the JSON should be equal to:
326
     */
327
    public function theJsonShouldBeEqualTo(PyStringNode $content)
328
    {
329
        $actual = $this->getJson();
330
331
        try {
332
            $expected = new Json($content);
333
        }
334
        catch (\Exception $e) {
335
            throw new \Exception('The expected JSON is not a valid');
336
        }
337
338
        $this->assertSame(
339
            (string) $expected,
340
            (string) $actual,
341
            "The json is equal to:\n". $actual->encode()
342
        );
343
    }
344
345
    /**
346
     * @Then print last JSON response
347
     */
348
    public function printLastJsonResponse()
349
    {
350
        echo $this->getJson()
351
            ->encode();
352
    }
353
354
    protected function getJson()
355
    {
356
        return new Json($this->httpCallResultPool->getResult()->getValue());
357
    }
358
359
    private function checkSchemaFile($filename)
360
    {
361
        if (false === is_file($filename)) {
362
            throw new \RuntimeException(
363
                'The JSON schema doesn\'t exist'
364
            );
365
        }
366
    }
367
}
368