Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
14 | class JsonContext extends BaseContext |
||
15 | { |
||
16 | protected $inspector; |
||
17 | |||
18 | protected $httpCallResultPool; |
||
19 | |||
20 | public function __construct(HttpCallResultPool $httpCallResultPool, $evaluationMode = 'javascript') |
||
25 | |||
26 | /** |
||
27 | * Checks, that the response is correct JSON |
||
28 | * |
||
29 | * @Then the response should be in JSON |
||
30 | */ |
||
31 | public function theResponseShouldBeInJson() |
||
35 | |||
36 | /** |
||
37 | * Checks, that the response is not correct JSON |
||
38 | * |
||
39 | * @Then the response should not be in JSON |
||
40 | */ |
||
41 | public function theResponseShouldNotBeInJson() |
||
48 | |||
49 | /** |
||
50 | * Checks, that given JSON node is equal to given value |
||
51 | * |
||
52 | * @Then the JSON node :node should be equal to :text |
||
53 | */ |
||
54 | public function theJsonNodeShouldBeEqualTo($node, $text) |
||
66 | |||
67 | /** |
||
68 | * Checks, that given JSON nodes are equal to givens values |
||
69 | * |
||
70 | * @Then the JSON nodes should be equal to: |
||
71 | */ |
||
72 | public function theJsonNodesShouldBeEqualTo(TableNode $nodes) |
||
73 | { |
||
74 | foreach ($nodes->getRowsHash() as $node => $text) { |
||
75 | $this->theJsonNodeShouldBeEqualTo($node, $text); |
||
76 | } |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Checks, that given JSON node is null |
||
81 | * |
||
82 | * @Then the JSON node :node should be null |
||
83 | */ |
||
84 | public function theJsonNodeShouldBeNull($node) |
||
96 | |||
97 | /** |
||
98 | * Checks, that given JSON node is not null. |
||
99 | * |
||
100 | * @Then the JSON node :node should not be null |
||
101 | */ |
||
102 | public function theJsonNodeShouldNotBeNull($node) |
||
108 | |||
109 | /** |
||
110 | * Checks, that given JSON node is true |
||
111 | * |
||
112 | * @Then the JSON node :node should be true |
||
113 | */ |
||
114 | public function theJsonNodeShouldBeTrue($node) |
||
126 | |||
127 | /** |
||
128 | * Checks, that given JSON node is false |
||
129 | * |
||
130 | * @Then the JSON node :node should be false |
||
131 | */ |
||
132 | public function theJsonNodeShouldBeFalse($node) |
||
144 | |||
145 | /** |
||
146 | * Checks, that given JSON node is equal to the given string |
||
147 | * |
||
148 | * @Then the JSON node :node should be equal to the string :text |
||
149 | */ |
||
150 | View Code Duplication | public function theJsonNodeShouldBeEqualToTheString($node, $text) |
|
162 | |||
163 | /** |
||
164 | * Checks, that given JSON node is equal to the given number |
||
165 | * |
||
166 | * @Then the JSON node :node should be equal to the number :number |
||
167 | */ |
||
168 | View Code Duplication | public function theJsonNodeShouldBeEqualToTheNumber($node, $number) |
|
180 | |||
181 | /** |
||
182 | * Checks, that given JSON node has N element(s) |
||
183 | * |
||
184 | * @Then the JSON node :node should have :count element(s) |
||
185 | */ |
||
186 | public function theJsonNodeShouldHaveElements($node, $count) |
||
194 | |||
195 | /** |
||
196 | * Checks, that given JSON node contains given value |
||
197 | * |
||
198 | * @Then the JSON node :node should contain :text |
||
199 | */ |
||
200 | View Code Duplication | public function theJsonNodeShouldContain($node, $text) |
|
208 | |||
209 | /** |
||
210 | * Checks, that given JSON nodes contains values |
||
211 | * |
||
212 | * @Then the JSON nodes should contain: |
||
213 | */ |
||
214 | public function theJsonNodesShouldContain(TableNode $nodes) |
||
215 | { |
||
216 | foreach ($nodes->getRowsHash() as $node => $text) { |
||
217 | $this->theJsonNodeShouldContain($node, $text); |
||
218 | } |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Checks, that given JSON node does not contain given value |
||
223 | * |
||
224 | * @Then the JSON node :node should not contain :text |
||
225 | */ |
||
226 | View Code Duplication | public function theJsonNodeShouldNotContain($node, $text) |
|
234 | |||
235 | /** |
||
236 | * Checks, that given JSON nodes does not contain given value |
||
237 | * |
||
238 | * @Then the JSON nodes should not contain: |
||
239 | */ |
||
240 | public function theJsonNodesShouldNotContain(TableNode $nodes) |
||
241 | { |
||
242 | foreach ($nodes->getRowsHash() as $node => $text) { |
||
243 | $this->theJsonNodeShouldNotContain($node, $text); |
||
244 | } |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Checks, that given JSON node exist |
||
249 | * |
||
250 | * @Given the JSON node :name should exist |
||
251 | */ |
||
252 | public function theJsonNodeShouldExist($name) |
||
264 | |||
265 | /** |
||
266 | * Checks, that given JSON node does not exist |
||
267 | * |
||
268 | * @Given the JSON node :name should not exist |
||
269 | */ |
||
270 | public function theJsonNodeShouldNotExist($name) |
||
276 | |||
277 | /** |
||
278 | * @Then the JSON should be valid according to this schema: |
||
279 | */ |
||
280 | public function theJsonShouldBeValidAccordingToThisSchema(PyStringNode $schema) |
||
287 | |||
288 | /** |
||
289 | * @Then the JSON should be invalid according to this schema: |
||
290 | */ |
||
291 | public function theJsonShouldBeInvalidAccordingToThisSchema(PyStringNode $schema) |
||
297 | |||
298 | /** |
||
299 | * @Then the JSON should be valid according to the schema :filename |
||
300 | */ |
||
301 | public function theJsonShouldBeValidAccordingToTheSchema($filename) |
||
313 | |||
314 | /** |
||
315 | * @Then the JSON should be invalid according to the schema :filename |
||
316 | */ |
||
317 | public function theJsonShouldBeInvalidAccordingToTheSchema($filename) |
||
325 | |||
326 | /** |
||
327 | * @Then the JSON should be equal to: |
||
328 | */ |
||
329 | public function theJsonShouldBeEqualTo(PyStringNode $content) |
||
346 | |||
347 | /** |
||
348 | * @Then print last JSON response |
||
349 | */ |
||
350 | public function printLastJsonResponse() |
||
355 | |||
356 | protected function getJson() |
||
360 | |||
361 | private function checkSchemaFile($filename) |
||
369 | } |
||
370 |