Completed
Pull Request — master (#171)
by
unknown
09:12
created

JsonContext::theJsonNodeShouldNotBeNull()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 10
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Sanpi\Behatch\Context;
4
5
use Behat\Gherkin\Node\PyStringNode;
6
7
use Behat\Gherkin\Node\TableNode;
8
use Sanpi\Behatch\Json\Json;
9
use Sanpi\Behatch\Json\JsonSchema;
10
use Sanpi\Behatch\Json\JsonInspector;
11
use Sanpi\Behatch\HttpCall\HttpCallResultPool;
12
13
class JsonContext extends BaseContext
14
{
15
    protected $inspector;
16
17
    protected $httpCallResultPool;
18
19
    public function __construct(HttpCallResultPool $httpCallResultPool, $evaluationMode = 'javascript')
20
    {
21
        $this->inspector = new JsonInspector($evaluationMode);
22
        $this->httpCallResultPool = $httpCallResultPool;
23
    }
24
25
    /**
26
     * Checks, that the response is correct JSON
27
     *
28
     * @Then the response should be in JSON
29
     */
30
    public function theResponseShouldBeInJson()
31
    {
32
        $this->getJson();
33
    }
34
35
    /**
36
     * Checks, that the response is not correct JSON
37
     *
38
     * @Then the response should not be in JSON
39
     */
40
    public function theResponseShouldNotBeInJson()
41
    {
42
        $this->not(
43
            [$this, 'theResponseShouldBeInJson'],
44
            'The response is in JSON'
45
        );
46
    }
47
48
    /**
49
     * Checks, that given JSON node is equal to given value
50
     *
51
     * @Then the JSON node :node should be equal to :text
52
     */
53 View Code Duplication
    public function theJsonNodeShouldBeEqualTo($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...
54
    {
55
        $json = $this->getJson();
56
57
        $actual = $this->inspector->evaluate($json, $node);
58
59
        if ($actual != $text) {
60
            throw new \Exception(
61
                sprintf("The node value is '%s'", json_encode($actual))
62
            );
63
        }
64
    }
65
66
    /**
67
     * Checks, that given JSON nodes are equal to givens values
68
     *
69
     * @Then the JSON nodes should be equal to:
70
     */
71
    public function theJsonNodesShoudBeEqualTo(TableNode $nodes)
72
    {
73
        foreach ($nodes->getRowsHash() as $node => $text) {
74
            $this->theJsonNodeShouldBeEqualTo($node, $text);
75
        }
76
    }
77
78
    /**
79
     * Checks, that given JSON node is null
80
     *
81
     * @Then the JSON node :node should be null
82
     */
83
    public function theJsonNodeShouldBeNull($node)
84
    {
85
        $json = $this->getJson();
86
87
        $actual = $this->inspector->evaluate($json, $node);
88
89
        if (null !== $actual) {
90
            throw new \Exception(
91
                sprintf('The node value is `%s`', json_encode($actual))
92
            );
93
        }
94
    }
95
96
    /**
97
     * Checks, that given JSON node is not null.
98
     *
99
     * @Then the JSON node :node should not be null
100
     */
101 View Code Duplication
    public function theJsonNodeShouldNotBeNull($node)
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...
102
    {
103
        $json = $this->getJson();
104
105
        $actual = $this->inspector->evaluate($json, $node);
106
107
        if (is_null($actual)) {
108
            throw new \Exception(sprintf('The JSON node %s should not be null', $node));
109
        }
110
    }
111
112
    /**
113
     * Checks, that given JSON node is true
114
     *
115
     * @Then the JSON node :node should be true
116
     */
117
    public function theJsonNodeShouldBeTrue($node)
118
    {
119
        $json = $this->getJson();
120
121
        $actual = $this->inspector->evaluate($json, $node);
122
123
        if (true !== $actual) {
124
            throw new \Exception(
125
                sprintf('The node value is `%s`', json_encode($actual))
126
            );
127
        }
128
    }
129
130
    /**
131
     * Checks, that given JSON node is false
132
     *
133
     * @Then the JSON node :node should be false
134
     */
135
    public function theJsonNodeShouldBeFalse($node)
136
    {
137
        $json = $this->getJson();
138
139
        $actual = $this->inspector->evaluate($json, $node);
140
141
        if (false !== $actual) {
142
            throw new \Exception(
143
                sprintf('The node value is `%s`', json_encode($actual))
144
            );
145
        }
146
    }
147
148
    /**
149
     * Checks, that given JSON node is equal to the given string
150
     *
151
     * @Then the JSON node :node should be equal to the string :text
152
     */
153 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...
154
    {
155
        $json = $this->getJson();
156
157
        $actual = $this->inspector->evaluate($json, $node);
158
159
        if ($actual !== $text) {
160
            throw new \Exception(
161
                sprintf('The node value is `%s`', json_encode($actual))
162
            );
163
        }
164
    }
165
166
    /**
167
     * Checks, that given JSON node is equal to the given number
168
     *
169
     * @Then the JSON node :node should be equal to the number :number
170
     */
171 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...
172
    {
173
        $json = $this->getJson();
174
175
        $actual = $this->inspector->evaluate($json, $node);
176
177
        if ($actual !== (float) $number && $actual !== (int) $number) {
178
            throw new \Exception(
179
                sprintf('The node value is `%s`', json_encode($actual))
180
            );
181
        }
182
    }
183
184
    /**
185
     * Checks, that given JSON node has N element(s)
186
     *
187
     * @Then the JSON node :node should have :count element(s)
188
     */
189
    public function theJsonNodeShouldHaveElements($node, $count)
190
    {
191
        $json = $this->getJson();
192
193
        $actual = $this->inspector->evaluate($json, $node);
194
195
        $this->assertSame($count, sizeof((array) $actual));
196
    }
197
198
    /**
199
     * Checks, that given JSON node contains given value
200
     *
201
     * @Then the JSON node :node should contain :text
202
     */
203 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...
204
    {
205
        $json = $this->getJson();
206
207
        $actual = $this->inspector->evaluate($json, $node);
208
209
        $this->assertContains($text, (string) $actual);
210
    }
211
212
    /**
213
     * Checks, that given JSON nodes contains values
214
     *
215
     * @Then the JSON nodes should contain:
216
     */
217
    public function theJsonNodesShoudContain(TableNode $nodes)
218
    {
219
        foreach ($nodes->getRowsHash() as $node => $text) {
220
            $this->theJsonNodeShouldContain($node, $text);
221
        }
222
    }
223
224
    /**
225
     * Checks, that given JSON node does not contain given value
226
     *
227
     * @Then the JSON node :node should not contain :text
228
     */
229 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...
230
    {
231
        $json = $this->getJson();
232
233
        $actual = $this->inspector->evaluate($json, $node);
234
235
        $this->assertNotContains($text, (string) $actual);
236
    }
237
238
    /**
239
     * Checks, that given JSON nodes does not contain given value
240
     *
241
     * @Then the JSON nodes should not contain:
242
     */
243
    public function theJsonNodesShoudNotContain(TableNode $nodes)
244
    {
245
        foreach ($nodes->getRowsHash() as $node => $text) {
246
            $this->theJsonNodeShouldNotContain($node, $text);
247
        }
248
    }
249
250
    /**
251
     * Checks, that given JSON node exist
252
     *
253
     * @Given the JSON node :name should exist
254
     */
255
    public function theJsonNodeShouldExist($name)
256
    {
257
        $json = $this->getJson();
258
259
        try {
260
            $node = $this->inspector->evaluate($json, $name);
261
        }
262
        catch (\Exception $e) {
263
            throw new \Exception("The node '$name' does not exist.");
264
        }
265
        return $node;
266
    }
267
268
    /**
269
     * Checks, that given JSON node does not exist
270
     *
271
     * @Given the JSON node :name should not exist
272
     */
273
    public function theJsonNodeShouldNotExist($name)
274
    {
275
        $this->not(function () use($name) {
276
            return $this->theJsonNodeShouldExist($name);
277
        }, "The node '$name' exists.");
278
    }
279
280
    /**
281
     * @Then the JSON should be valid according to this schema:
282
     */
283
    public function theJsonShouldBeValidAccordingToThisSchema(PyStringNode $schema)
284
    {
285
        $this->inspector->validate(
286
            $this->getJson(),
287
            new JsonSchema($schema)
288
        );
289
    }
290
291
    /**
292
     * @Then the JSON should be valid according to the schema :filename
293
     */
294
    public function theJsonShouldBeValidAccordingToTheSchema($filename)
295
    {
296
        $this->checkSchemaFile($filename);
297
298
        $this->inspector->validate(
299
            $this->getJson(),
300
            new JsonSchema(
301
                file_get_contents($filename),
302
                'file://' . getcwd() . '/' . $filename
303
            )
304
        );
305
    }
306
307
    /**
308
     * @Then the JSON should be invalid according to the schema :filename
309
     */
310
    public function theJsonShouldBeInvalidAccordingToTheSchema($filename)
311
    {
312
        $this->checkSchemaFile($filename);
313
314
        $this->not(function () use($filename) {
315
            return $this->theJsonShouldBeValidAccordingToTheSchema($filename);
316
        }, "The schema was valid");
317
    }
318
319
    /**
320
     * @Then the JSON should be equal to:
321
     */
322
    public function theJsonShouldBeEqualTo(PyStringNode $content)
323
    {
324
        $actual = $this->getJson();
325
326
        try {
327
            $expected = new Json($content);
328
        }
329
        catch (\Exception $e) {
330
            throw new \Exception('The expected JSON is not a valid');
331
        }
332
333
        $this->assertSame(
334
            (string) $expected,
335
            (string) $actual,
336
            "The json is equal to:\n". $actual->encode()
337
        );
338
    }
339
340
    /**
341
     * @Then print last JSON response
342
     */
343
    public function printLastJsonResponse()
344
    {
345
        echo $this->getJson()
346
            ->encode();
347
    }
348
349
    protected function getJson()
350
    {
351
        return new Json($this->httpCallResultPool->getResult()->getValue());
352
    }
353
354
    private function checkSchemaFile($filename)
355
    {
356
        if (false === is_file($filename)) {
357
            throw new \RuntimeException(
358
                'The JSON schema doesn\'t exist'
359
            );
360
        }
361
    }
362
}
363