1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JsonSpec\Behat\Context; |
4
|
|
|
|
5
|
|
|
use Behat\Behat\Context\Context; |
6
|
|
|
use Fesor\JsonMatcher\Helper\JsonHelper; |
7
|
|
|
use \JsonSpec\Behat\Context\Traits\JsonMatcherAwareTrait; |
8
|
|
|
use Behat\Gherkin\Node\PyStringNode; |
9
|
|
|
use Behat\Gherkin\Node\TableNode; |
10
|
|
|
use Fesor\JsonMatcher\JsonMatcher; |
11
|
|
|
use JsonSpec\Behat\JsonProvider\JsonHolder; |
12
|
|
|
use JsonSpec\Behat\Helper\MemoryHelper; |
13
|
|
|
use JsonSpec\JsonLoader; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class JsonSpecContext |
17
|
|
|
* @package JsonSpec\Behat\Context |
18
|
|
|
*/ |
19
|
|
|
class JsonSpecContext implements Context, JsonHolderAware, MemoryHelperAware, JsonMatcherAware |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
use JsonMatcherAwareTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var MemoryHelper |
26
|
|
|
*/ |
27
|
|
|
private $memoryHelper; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var JsonHolder |
31
|
|
|
*/ |
32
|
|
|
private $jsonHolder; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var JsonLoader |
36
|
|
|
*/ |
37
|
|
|
private $jsonLoader; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var JsonHelper |
41
|
|
|
*/ |
42
|
|
|
private $jsonHelper; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param JsonLoader $jsonLoader |
46
|
|
|
* @param JsonHelper $jsonHelper |
47
|
|
|
*/ |
48
|
|
|
public function __construct( |
49
|
|
|
JsonLoader $jsonLoader, |
50
|
|
|
JsonHelper $jsonHelper |
51
|
|
|
) |
52
|
|
|
{ |
53
|
|
|
$this->jsonLoader = $jsonLoader; |
54
|
|
|
$this->jsonHelper = $jsonHelper; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @Then /^(?:I )?keep the (?:JSON|json)(?: response)?(?: at "(.*)")? as "(.*)"$/ |
59
|
|
|
*/ |
60
|
|
|
public function keepJson($path, $key) |
61
|
|
|
{ |
62
|
|
|
$json = $this->jsonHolder->getJson(); |
63
|
|
|
$this->memoryHelper->memorize($key, $this->jsonHelper->normalize($json, $this->normalizePath($path))); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? be(:)$/ |
68
|
|
|
*/ |
69
|
|
|
public function checkEquality($path, $isNegative, PyStringNode $json) |
70
|
|
|
{ |
71
|
|
|
$this->checkEqualityInline($path, $isNegative, $json->getRaw()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? be file "(.+)"/ |
76
|
|
|
*/ |
77
|
|
|
public function checkEqualityWithFileContents($path = null, $isNegative = null, $jsonFile) |
78
|
|
|
{ |
79
|
|
|
$this->checkEqualityInline($path, $isNegative, $this->jsonLoader->loadJson($jsonFile)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? be (".*"|\-?\d+(?:\.\d+)?(?:[eE][\+\-]?\d+)?|\[.*\]|%?\{.*\}|true|false|null)$/ |
84
|
|
|
*/ |
85
|
|
|
public function checkEqualityInline($path, $isNegative, $json) |
86
|
|
|
{ |
87
|
|
|
$this |
88
|
|
|
->json($this->rememberJson()) |
89
|
|
|
->equal($this->memoryHelper->remember($json), [ |
90
|
|
|
'at' => $this->normalizePath($path), |
91
|
|
|
JsonMatcher::OPTION_NEGATIVE => !!$isNegative |
92
|
|
|
]) |
93
|
|
|
; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? include(:)$/ |
98
|
|
|
*/ |
99
|
|
|
public function checkInclusion($path, $isNegative, PyStringNode $json) |
100
|
|
|
{ |
101
|
|
|
$this->checkInclusionInline($path, $isNegative, $json->getRaw()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? include file "(.+)"$/ |
106
|
|
|
*/ |
107
|
|
|
public function checkInclusionOfFile($path, $isNegative, $jsonFile) |
108
|
|
|
{ |
109
|
|
|
$this->checkInclusionInline($path, $isNegative, $this->jsonLoader->loadJson($jsonFile)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? include (".*"|\-?\d+(?:\.\d+)?(?:[eE][\+\-]?\d+)?|\[.*\]|%?\{.*\}|true|false|null)$/ |
114
|
|
|
*/ |
115
|
|
View Code Duplication |
public function checkInclusionInline($path, $isNegative, $json) |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
$this |
118
|
|
|
->json($this->rememberJson()) |
119
|
|
|
->includes($this->rememberJson($json), [ |
120
|
|
|
'at' => $this->normalizePath($path), |
121
|
|
|
JsonMatcher::OPTION_NEGATIVE => !!$isNegative |
122
|
|
|
]) |
123
|
|
|
; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should have the following(:)$/ |
128
|
|
|
*/ |
129
|
|
|
public function hasKeys($base, TableNode $table) |
130
|
|
|
{ |
131
|
|
|
$matcher = $this->json($this->rememberJson()); |
132
|
|
|
|
133
|
|
|
foreach ($table->getRows() as $row) { |
134
|
|
|
$path = ltrim("$base/{$row[0]}"); |
135
|
|
|
if (count ($row) == 2) { |
136
|
|
|
$matcher->equal($this->rememberJson($row[1]), ['at' => $this->normalizePath($path)]); |
137
|
|
|
} else { |
138
|
|
|
$matcher->hasPath($path); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @Then /^the (?:JSON|json)(?: response)? should( not)? have "(.*)"$/ |
145
|
|
|
*/ |
146
|
|
|
public function hasKeysInline($isNegative, $path) |
147
|
|
|
{ |
148
|
|
|
$this |
149
|
|
|
->json($this->rememberJson()) |
150
|
|
|
->hasPath($path, [ |
151
|
|
|
JsonMatcher::OPTION_NEGATIVE => !!$isNegative |
152
|
|
|
]) |
153
|
|
|
; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? be an? (.*)$/ |
158
|
|
|
*/ |
159
|
|
View Code Duplication |
public function haveType($path, $isNegative, $type) |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
$this |
162
|
|
|
->json($this->rememberJson()) |
163
|
|
|
->hasType($type, [ |
164
|
|
|
'at' => $this->normalizePath($path), |
165
|
|
|
JsonMatcher::OPTION_NEGATIVE => !!$isNegative |
166
|
|
|
]) |
167
|
|
|
; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? have (\d+)/ |
172
|
|
|
*/ |
173
|
|
|
public function haveSize($path, $isNegative, $size) |
174
|
|
|
{ |
175
|
|
|
$this |
176
|
|
|
->json($this->rememberJson()) |
177
|
|
|
->hasSize(intval($size, 10), [ |
178
|
|
|
'at' => $this->normalizePath($path), |
179
|
|
|
JsonMatcher::OPTION_NEGATIVE => !!$isNegative |
180
|
|
|
]) |
181
|
|
|
; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @Then print last JSON response |
186
|
|
|
*/ |
187
|
|
|
public function printLastJsonResponse() |
188
|
|
|
{ |
189
|
|
|
echo (string) $this->jsonHolder->getJson(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @inheritdoc |
194
|
|
|
*/ |
195
|
|
|
public function setJsonHolder(JsonHolder $holder) |
196
|
|
|
{ |
197
|
|
|
$this->jsonHolder = $holder; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @inheritdoc |
202
|
|
|
*/ |
203
|
|
|
public function setMemoryHelper(MemoryHelper $memoryHelper) |
204
|
|
|
{ |
205
|
|
|
$this->memoryHelper = $memoryHelper; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @return string |
210
|
|
|
*/ |
211
|
|
|
private function rememberJson($json = null) |
212
|
|
|
{ |
213
|
|
|
if (null === $json) { |
214
|
|
|
$json = $this->jsonHolder->getJson(); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $this->memoryHelper->remember($json); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
private function normalizePath($path) |
221
|
|
|
{ |
222
|
|
|
if (0 === strlen(ltrim($path, '/'))) { |
223
|
|
|
return null; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return $path; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
} |
230
|
|
|
|
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.