Completed
Pull Request — master (#155)
by Andy
05:52
created

JsonContext::theJsonNodeShouldNotContain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 8
loc 8
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
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
    public function theJsonNodeShouldBeEqualTo($node, $text)
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 true
98
     *
99
     * @Then the JSON node :node should be true
100
     */
101
    public function theJsonNodeShouldBeTrue($node)
102
    {
103
        $json = $this->getJson();
104
105
        $actual = $this->inspector->evaluate($json, $node);
106
107
        if (true !== $actual) {
108
            throw new \Exception(
109
                sprintf('The node value is `%s`', json_encode($actual))
110
            );
111
        }
112
    }
113
114
    /**
115
     * Checks, that given JSON node is false
116
     *
117
     * @Then the JSON node :node should be false
118
     */
119
    public function theJsonNodeShouldBeFalse($node)
120
    {
121
        $json = $this->getJson();
122
123
        $actual = $this->inspector->evaluate($json, $node);
124
125
        if (false !== $actual) {
126
            throw new \Exception(
127
                sprintf('The node value is `%s`', json_encode($actual))
128
            );
129
        }
130
    }
131
132
    /**
133
     * Checks, that given JSON node is equal to the given string
134
     *
135
     * @Then the JSON node :node should be equal to the string :text
136
     */
137 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...
138
    {
139
        $json = $this->getJson();
140
141
        $actual = $this->inspector->evaluate($json, $node);
142
143
        if ($actual !== $text) {
144
            throw new \Exception(
145
                sprintf('The node value is `%s`', json_encode($actual))
146
            );
147
        }
148
    }
149
150
    /**
151
     * Checks, that given JSON node is equal to the given number
152
     *
153
     * @Then the JSON node :node should be equal to the number :number
154
     */
155 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...
156
    {
157
        $json = $this->getJson();
158
159
        $actual = $this->inspector->evaluate($json, $node);
160
161
        if ($actual !== (float) $number && $actual !== (int) $number) {
162
            throw new \Exception(
163
                sprintf('The node value is `%s`', json_encode($actual))
164
            );
165
        }
166
    }
167
168
    /**
169
     * Checks, that given JSON node has N element(s)
170
     *
171
     * @Then the JSON node :node should have :count element(s)
172
     */
173
    public function theJsonNodeShouldHaveElements($node, $count)
174
    {
175
        $json = $this->getJson();
176
177
        $actual = $this->inspector->evaluate($json, $node);
178
179
        $this->assertSame($count, sizeof((array) $actual));
180
    }
181
182
    /**
183
     * Checks, that given JSON node contains given value
184
     *
185
     * @Then the JSON node :node should contain :text
186
     */
187 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...
188
    {
189
        $json = $this->getJson();
190
191
        $actual = $this->inspector->evaluate($json, $node);
192
193
        $this->assertContains($text, (string) $actual);
194
    }
195
196
    /**
197
     * Checks, that given JSON nodes contains values
198
     *
199
     * @Then the JSON nodes should contain:
200
     */
201
    public function theJsonNodesShoudContain(TableNode $nodes)
202
    {
203
        foreach ($nodes->getRowsHash() as $node => $text) {
204
            $this->theJsonNodeShouldContain($node, $text);
205
        }
206
    }
207
208
    /**
209
     * Checks, that given JSON node does not contain given value
210
     *
211
     * @Then the JSON node :node should not contain :text
212
     */
213 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...
214
    {
215
        $json = $this->getJson();
216
217
        $actual = $this->inspector->evaluate($json, $node);
218
219
        $this->assertNotContains($text, (string) $actual);
220
    }
221
222
    /**
223
     * Checks, that given JSON nodes does not contain given value
224
     *
225
     * @Then the JSON nodes should not contain:
226
     */
227
    public function theJsonNodesShoudNotContain(TableNode $nodes)
228
    {
229
        foreach ($nodes->getRowsHash() as $node => $text) {
230
            $this->theJsonNodeShouldNotContain($node, $text);
231
        }
232
    }
233
234
    /**
235
     * Checks, that given JSON node exist
236
     *
237
     * @Given the JSON node :name should exist
238
     */
239
    public function theJsonNodeShouldExist($name)
240
    {
241
        $json = $this->getJson();
242
243
        try {
244
            $node = $this->inspector->evaluate($json, $name);
245
        }
246
        catch (\Exception $e) {
247
            throw new \Exception("The node '$name' does not exist.");
248
        }
249
        return $node;
250
    }
251
252
    /**
253
     * Checks, that given JSON node does not exist
254
     *
255
     * @Given the JSON node :name should not exist
256
     */
257
    public function theJsonNodeShouldNotExist($name)
258
    {
259
        $this->not(function () use($name) {
260
            return $this->theJsonNodeShouldExist($name);
261
        }, "The node '$name' exists.");
262
    }
263
264
    /**
265
     * @Then the JSON should be valid according to this schema:
266
     */
267
    public function theJsonShouldBeValidAccordingToThisSchema(PyStringNode $schema)
268
    {
269
        $this->inspector->validate(
270
            $this->getJson(),
271
            new JsonSchema($schema)
272
        );
273
    }
274
275
    /**
276
     * @Then the JSON should be valid according to the schema :filename
277
     */
278
    public function theJsonShouldBeValidAccordingToTheSchema($filename)
279
    {
280
        $this->checkSchemaFile($filename);
281
282
        $this->inspector->validate(
283
            $this->getJson(),
284
            new JsonSchema(
285
                file_get_contents($filename),
286
                'file://' . getcwd() . '/' . $filename
287
            )
288
        );
289
    }
290
291
    /**
292
     * @Then the JSON should be invalid according to the schema :filename
293
     */
294
    public function theJsonShouldBeInvalidAccordingToTheSchema($filename)
295
    {
296
        $this->checkSchemaFile($filename);
297
298
        $this->not(function () use($filename) {
299
            return $this->theJsonShouldBeValidAccordingToTheSchema($filename);
300
        }, "The schema was valid");
301
    }
302
303
    /**
304
     * @Then the JSON should be equal to:
305
     */
306
    public function theJsonShouldBeEqualTo(PyStringNode $content)
307
    {
308
        $actual = $this->getJson();
309
310
        try {
311
            $expected = new Json($content);
312
        }
313
        catch (\Exception $e) {
314
            throw new \Exception('The expected JSON is not a valid');
315
        }
316
317
        $this->assertSame(
318
            (string) $expected,
319
            (string) $actual,
320
            "The json is equal to:\n". $actual->encode()
321
        );
322
    }
323
324
    /**
325
     * @Then print last JSON response
326
     */
327
    public function printLastJsonResponse()
328
    {
329
        echo $this->getJson()
330
            ->encode();
331
    }
332
333
    protected function getJson()
334
    {
335
        return new Json($this->httpCallResultPool->getResult()->getValue());
336
    }
337
338
    private function checkSchemaFile($filename)
339
    {
340
        if (false === is_file($filename)) {
341
            throw new \RuntimeException(
342
                'The JSON schema doesn\'t exist'
343
            );
344
        }
345
    }
346
}
347