This repository was archived by the owner on Apr 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathJsonContext.php
More file actions
236 lines (199 loc) · 5.83 KB
/
Copy pathJsonContext.php
File metadata and controls
236 lines (199 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
namespace Sanpi\Behatch\Context;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Mink\Exception\ExpectationException;
use Sanpi\Behatch\Json\Json;
use Sanpi\Behatch\Json\JsonSchema;
use Sanpi\Behatch\Json\JsonInspector;
use Sanpi\Behatch\HttpCall\HttpCallResultAware;
use Sanpi\Behatch\HttpCall\HttpCallResultPool;
class JsonContext extends BaseContext
{
private $inspector;
private $httpCallResultPool;
public function __construct($evaluationMode = 'javascript', HttpCallResultPool $httpCallResultPool)
{
$this->inspector = new JsonInspector($evaluationMode);
$this->httpCallResultPool = $httpCallResultPool;
}
/**
* Checks, that the response is correct JSON
*
* @Then the response should be in JSON
*/
public function theResponseShouldBeInJson()
{
$this->getJson();
}
/**
* Checks, that the response is not correct JSON
*
* @Then the response should not be in JSON
*/
public function theResponseShouldNotBeInJson()
{
try {
$this->getJson();
}
catch (\Exception $e) {
}
if (!isset($e)) {
throw new \Exception("The response is in JSON");
}
}
/**
* Checks, that given JSON node is equal to given value
*
* @Then the JSON node :node should be equal to :text
*/
public function theJsonNodeShouldBeEqualTo($node, $text)
{
$json = $this->getJson();
$actual = $this->inspector->evaluate($json, $node);
if ($actual != $text) {
throw new \Exception(
sprintf("The node value is `%s`", json_encode($actual))
);
}
}
/**
* Checks, that given JSON node has N element(s)
*
* @Then the JSON node :node should have :count element(s)
*/
public function theJsonNodeShouldHaveElements($node, $count)
{
$json = $this->getJson();
$actual = $this->inspector->evaluate($json, $node);
$this->assertSame($count, sizeof((array) $actual));
}
/**
* Checks, that given JSON node contains given value
*
* @Then the JSON node :node should contain :text
*/
public function theJsonNodeShouldContain($node, $text)
{
$json = $this->getJson();
$actual = $this->inspector->evaluate($json, $node);
$this->assertContains($text, (string) $actual);
}
/**
* Checks, that given JSON node does not contain given value
*
* @Then the JSON node :node should not contain :text
*/
public function theJsonNodeShouldNotContain($node, $text)
{
$json = $this->getJson();
$actual = $this->inspector->evaluate($json, $node);
$this->assertNotContains($text, (string) $actual);
}
/**
* Checks, that given JSON node exist
*
* @Given the JSON node :node should exist
*/
public function theJsonNodeShouldExist($node)
{
$json = $this->getJson();
try {
$this->inspector->evaluate($json, $node);
}
catch (\Exception $e) {
throw new \Exception(sprintf("The node '%s' does not exist.", $node));
}
}
/**
* Checks, that given JSON node does not exist
*
* @Given the JSON node :node should not exist
*/
public function theJsonNodeShouldNotExist($node)
{
$json = $this->getJson();
$e = null;
try {
$actual = $this->inspector->evaluate($json, $node);
} catch (\Exception $e) {
}
if ($e === null) {
throw new \Exception(sprintf("The node '%s' exists and contains '%s'.", $node , json_encode($actual)));
}
}
/**
* @Then the JSON should be valid according to this schema:
*/
public function theJsonShouldBeValidAccordingToThisSchema(PyStringNode $schema)
{
$this->inspector->validate(
$this->getJson(),
new JsonSchema($schema)
);
}
/**
* @Then the JSON should be invalid according to this schema:
*/
public function theJsonShouldBeInvalidAccordingToThisSchema(PyStringNode $schema)
{
try {
$isValid = $this->inspector->validate(
$this->getJson(),
new JsonSchema($schema)
);
} catch (\Exception $e) {
$isValid = false;
}
if (true === $isValid) {
throw new ExpectationException('Expected to receive invalid json, got valid one', $this->getSession());
}
}
/**
* @Then the JSON should be valid according to the schema :filename
*/
public function theJsonShouldBeValidAccordingToTheSchema($filename)
{
if (false === is_file($filename)) {
throw new \RuntimeException(
'The JSON schema doesn\'t exist'
);
}
$this->inspector->validate(
$this->getJson(),
new JsonSchema(
file_get_contents($filename),
'file://' . getcwd() . '/' . $filename
)
);
}
/**
* @Then the JSON should be equal to:
*/
public function theJsonShouldBeEqualTo(PyStringNode $content)
{
$actual = $this->getJson();
try {
$expected = new Json($content);
}
catch (\Exception $e) {
throw new \Exception('The expected JSON is not a valid');
}
$this->assertSame(
(string) $expected,
(string) $actual,
"The json is equal to:\n". $actual->encode()
);
}
/**
* @Then print last JSON response
*/
public function printLastJsonResponse()
{
echo $this->getJson()
->encode();
}
private function getJson()
{
return new Json($this->httpCallResultPool->getResult()->getValue());
}
}