Completed
Push — master ( 6d439a...6e0a89 )
by Timothée
06:41 queued 03:31
created

theJsonArrayNodeShouldNotContainElements()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Rezzza\RestApiBehatExtension\Json;
4
5
use mageekguy\atoum\asserter\generator as asserter;
6
use Behat\Behat\Context\Context;
7
use Behat\Behat\Context\SnippetAcceptingContext;
8
use Behat\Gherkin\Node\PyStringNode;
9
10
class JsonContext implements Context, SnippetAcceptingContext
11
{
12
    private $jsonInspector;
13
14
    private $asserter;
15
16
    private $jsonSchemaBaseUrl;
17
18
    public function __construct(JsonInspector $jsonInspector, $jsonSchemaBaseUrl = null)
19
    {
20
        $this->jsonInspector = $jsonInspector;
21
        $this->asserter = new asserter;
22
        $this->jsonSchemaBaseUrl = rtrim($jsonSchemaBaseUrl, '/');
23
    }
24
25
    /**
26
     * @When /^I load JSON:$/
27
     */
28
    public function iLoadJson(PyStringNode $jsonContent)
29
    {
30
        $this->jsonInspector->writeJson((string) $jsonContent);
31
    }
32
33
    /**
34
     * @Then /^the response should be in JSON$/
35
     */
36
    public function responseShouldBeInJson()
37
    {
38
        $this->jsonInspector->readJson();
39
    }
40
41
    /**
42
     * @Then /^the JSON node "(?P<jsonNode>[^"]*)" should be equal to "(?P<expectedValue>.*)"$/
43
     */
44
    public function theJsonNodeShouldBeEqualTo($jsonNode, $expectedValue)
45
    {
46
        $realValue = $this->evaluateJsonNodeValue($jsonNode);
47
48
        $this->asserter->variable($realValue)->isEqualTo($expectedValue);
49
    }
50
51
    /**
52
     * @Then /^the JSON node "(?P<jsonNode>[^"]*)" should have (?P<expectedNth>\d+) elements?$/
53
     * @Then /^the JSON array node "(?P<jsonNode>[^"]*)" should have (?P<expectedNth>\d+) elements?$/
54
     */
55
    public function theJsonNodeShouldHaveElements($jsonNode, $expectedNth)
56
    {
57
        $realValue = $this->evaluateJsonNodeValue($jsonNode);
58
59
        $this->asserter->phpArray($realValue)->hasSize($expectedNth);
60
    }
61
62
    /**
63
     * @Then /^the JSON array node "(?P<jsonNode>[^"]*)" should contain "(?P<expectedValue>.*)" element$/
64
     */
65
    public function theJsonArrayNodeShouldContainElements($jsonNode, $expectedValue)
66
    {
67
        $realValue = $this->evaluateJsonNodeValue($jsonNode);
68
69
        $this->asserter->phpArray($realValue)->contains($expectedValue);
70
    }
71
    
72
    /**
73
     * @Then /^the JSON array node "(?P<jsonNode>[^"]*)" should not contain "(?P<expectedValue>.*)" element$/
74
     */
75
    public function theJsonArrayNodeShouldNotContainElements($jsonNode, $expectedValue)
76
    {
77
        $realValue = $this->evaluateJsonNodeValue($jsonNode);
78
79
        $this->asserter->phpArray($realValue)->notContains($expectedValue);
80
    }
81
82
    /**
83
     * @Then /^the JSON node "(?P<jsonNode>[^"]*)" should contain "(?P<expectedValue>.*)"$/
84
     */
85
    public function theJsonNodeShouldContain($jsonNode, $expectedValue)
86
    {
87
        $realValue = $this->evaluateJsonNodeValue($jsonNode);
88
89
        $this->asserter->string((string) $realValue)->contains($expectedValue);
90
    }
91
92
    /**
93
     * Checks, that given JSON node does not contain given value
94
     *
95
     * @Then /^the JSON node "(?P<jsonNode>[^"]*)" should not contain "(?P<unexpectedValue>.*)"$/
96
     */
97
    public function theJsonNodeShouldNotContain($jsonNode, $unexpectedValue)
98
    {
99
        $realValue = $this->evaluateJsonNodeValue($jsonNode);
100
101
        $this->asserter->string((string) $realValue)->notContains($unexpectedValue);
102
    }
103
104
    /**
105
     * Checks, that given JSON node exist
106
     *
107
     * @Given /^the JSON node "(?P<jsonNode>[^"]*)" should exist$/
108
     */
109
    public function theJsonNodeShouldExist($jsonNode)
110
    {
111
        try {
112
            $this->evaluateJsonNodeValue($jsonNode);
113
        } catch (\Exception $e) {
114
            throw new \Exception(sprintf("The node '%s' does not exist.", $jsonNode), 0, $e);
115
        }
116
    }
117
118
    /**
119
     * Checks, that given JSON node does not exist
120
     *
121
     * @Given /^the JSON node "(?P<jsonNode>[^"]*)" should not exist$/
122
     */
123
    public function theJsonNodeShouldNotExist($jsonNode)
124
    {
125
        $e = null;
126
127
        try {
128
            $realValue = $this->evaluateJsonNodeValue($jsonNode);
129
        } catch (\Exception $e) {
130
            // If the node does not exist an exception should be throwed
131
        }
132
133
        if ($e === null) {
134
            throw new \Exception(sprintf("The node '%s' exists and contains '%s'.", $jsonNode, json_encode($realValue)));
135
        }
136
    }
137
138
    /**
139
     * @Then /^the JSON should be valid according to this schema:$/
140
     */
141
    public function theJsonShouldBeValidAccordingToThisSchema(PyStringNode $jsonSchemaContent)
142
    {
143
        $this->jsonInspector->validateJson(
144
            new JsonSchema($jsonSchemaContent)
145
        );
146
    }
147
148
    /**
149
     * @Then /^the JSON should be valid according to the schema "(?P<filename>[^"]*)"$/
150
     */
151
    public function theJsonShouldBeValidAccordingToTheSchema($filename)
152
    {
153
        $filename = $this->resolveFilename($filename);
154
155
        $this->jsonInspector->validateJson(
156
            new JsonSchema(
157
                file_get_contents($filename),
158
                'file://' . $filename
159
            )
160
        );
161
    }
162
163
    /**
164
     * @Then /^the JSON should be equal to:$/
165
     */
166
    public function theJsonShouldBeEqualTo(PyStringNode $jsonContent)
167
    {
168
        $realJsonValue = $this->readJson();
169
170
        try {
171
            $expectedJsonValue = new Json($jsonContent);
172
        } catch (\Exception $e) {
173
            throw new \Exception('The expected JSON is not a valid');
174
        }
175
176
        $this->asserter->castToString($realJsonValue)->isEqualTo((string) $expectedJsonValue);
177
    }
178
179
    private function evaluateJsonNodeValue($jsonNode)
180
    {
181
        return $this->jsonInspector->readJsonNodeValue($jsonNode);
182
    }
183
184
    private function readJson()
185
    {
186
        return $this->jsonInspector->readJson();
187
    }
188
189
    private function resolveFilename($filename)
190
    {
191
        if (true === is_file($filename)) {
192
            return realpath($filename);
193
        }
194
195
        if (null === $this->jsonSchemaBaseUrl) {
196
            throw new \RuntimeException(sprintf(
197
                'The JSON schema file "%s" doesn\'t exist',
198
                $filename
199
            ));
200
        }
201
202
        $filename = $this->jsonSchemaBaseUrl . '/' . $filename;
203
204
        if (false === is_file($filename)) {
205
            throw new \RuntimeException(sprintf(
206
                'The JSON schema file "%s" doesn\'t exist',
207
                $filename
208
            ));
209
        }
210
211
        return realpath($filename);
212
    }
213
}
214