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 node "(?P<jsonNode>[^"]*)" should contain "(?P<expectedValue>.*)"$/ |
74
|
|
|
*/ |
75
|
|
|
public function theJsonNodeShouldContain($jsonNode, $expectedValue) |
76
|
|
|
{ |
77
|
|
|
$realValue = $this->evaluateJsonNodeValue($jsonNode); |
78
|
|
|
|
79
|
|
|
$this->asserter->string((string) $realValue)->contains($expectedValue); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Checks, that given JSON node does not contain given value |
84
|
|
|
* |
85
|
|
|
* @Then /^the JSON node "(?P<jsonNode>[^"]*)" should not contain "(?P<unexpectedValue>.*)"$/ |
86
|
|
|
*/ |
87
|
|
|
public function theJsonNodeShouldNotContain($jsonNode, $unexpectedValue) |
88
|
|
|
{ |
89
|
|
|
$realValue = $this->evaluateJsonNodeValue($jsonNode); |
90
|
|
|
|
91
|
|
|
$this->asserter->string((string) $realValue)->notContains($unexpectedValue); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Checks, that given JSON node exist |
96
|
|
|
* |
97
|
|
|
* @Given /^the JSON node "(?P<jsonNode>[^"]*)" should exist$/ |
98
|
|
|
*/ |
99
|
|
|
public function theJsonNodeShouldExist($jsonNode) |
100
|
|
|
{ |
101
|
|
|
try { |
102
|
|
|
$this->evaluateJsonNodeValue($jsonNode); |
103
|
|
|
} catch (\Exception $e) { |
104
|
|
|
throw new \Exception(sprintf("The node '%s' does not exist.", $jsonNode), 0, $e); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Checks, that given JSON node does not exist |
110
|
|
|
* |
111
|
|
|
* @Given /^the JSON node "(?P<jsonNode>[^"]*)" should not exist$/ |
112
|
|
|
*/ |
113
|
|
|
public function theJsonNodeShouldNotExist($jsonNode) |
114
|
|
|
{ |
115
|
|
|
$e = null; |
116
|
|
|
|
117
|
|
|
try { |
118
|
|
|
$realValue = $this->evaluateJsonNodeValue($jsonNode); |
119
|
|
|
} catch (\Exception $e) { |
120
|
|
|
// If the node does not exist an exception should be throwed |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if ($e === null) { |
124
|
|
|
throw new \Exception(sprintf("The node '%s' exists and contains '%s'.", $jsonNode, json_encode($realValue))); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @Then /^the JSON should be valid according to this schema:$/ |
130
|
|
|
*/ |
131
|
|
|
public function theJsonShouldBeValidAccordingToThisSchema(PyStringNode $jsonSchemaContent) |
132
|
|
|
{ |
133
|
|
|
$this->jsonInspector->validateJson( |
134
|
|
|
new JsonSchema($jsonSchemaContent) |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @Then /^the JSON should be valid according to the schema "(?P<filename>[^"]*)"$/ |
140
|
|
|
*/ |
141
|
|
|
public function theJsonShouldBeValidAccordingToTheSchema($filename) |
142
|
|
|
{ |
143
|
|
|
$filename = $this->resolveFilename($filename); |
144
|
|
|
|
145
|
|
|
$this->jsonInspector->validateJson( |
146
|
|
|
new JsonSchema( |
147
|
|
|
file_get_contents($filename), |
148
|
|
|
'file://' . $filename |
149
|
|
|
) |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @Then /^the JSON should be equal to:$/ |
155
|
|
|
*/ |
156
|
|
|
public function theJsonShouldBeEqualTo(PyStringNode $jsonContent) |
157
|
|
|
{ |
158
|
|
|
$realJsonValue = $this->readJson(); |
159
|
|
|
|
160
|
|
|
try { |
161
|
|
|
$expectedJsonValue = new Json($jsonContent); |
162
|
|
|
} catch (\Exception $e) { |
163
|
|
|
throw new \Exception('The expected JSON is not a valid'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$this->asserter->castToString($realJsonValue)->isEqualTo((string) $expectedJsonValue); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
private function evaluateJsonNodeValue($jsonNode) |
170
|
|
|
{ |
171
|
|
|
return $this->jsonInspector->readJsonNodeValue($jsonNode); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
private function readJson() |
175
|
|
|
{ |
176
|
|
|
return $this->jsonInspector->readJson(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
private function resolveFilename($filename) |
180
|
|
|
{ |
181
|
|
|
if (true === is_file($filename)) { |
182
|
|
|
return realpath($filename); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if (null === $this->jsonSchemaBaseUrl) { |
186
|
|
|
throw new \RuntimeException(sprintf( |
187
|
|
|
'The JSON schema file "%s" doesn\'t exist', |
188
|
|
|
$filename |
189
|
|
|
)); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
$filename = $this->jsonSchemaBaseUrl . '/' . $filename; |
193
|
|
|
|
194
|
|
|
if (false === is_file($filename)) { |
195
|
|
|
throw new \RuntimeException(sprintf( |
196
|
|
|
'The JSON schema file "%s" doesn\'t exist', |
197
|
|
|
$filename |
198
|
|
|
)); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return realpath($filename); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|