Total Complexity | 44 |
Total Lines | 603 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like DynamicRoutesUnitTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DynamicRoutesUnitTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
3 | class DynamicRoutesUnitTest extends \PHPUnit\Framework\TestCase |
||
4 | { |
||
5 | |||
6 | /** |
||
7 | * Default setup |
||
8 | * |
||
9 | * {@inheritdoc} |
||
10 | * @see \PHPUnit\Framework\TestCase::setUp() |
||
11 | */ |
||
12 | public function setUp(): void |
||
13 | { |
||
14 | $_SERVER['REQUEST_METHOD'] = 'GET'; |
||
15 | } |
||
16 | |||
17 | /** |
||
18 | * Testing hasParam method |
||
19 | */ |
||
20 | public function testValidatingParameter(): void |
||
21 | { |
||
22 | // setup |
||
23 | $router = new \Mezon\Router\Router(); |
||
24 | $router->addRoute('/catalog/[i:foo]/', function () { |
||
25 | // do nothing |
||
26 | }); |
||
27 | |||
28 | $router->callRoute('/catalog/1/'); |
||
29 | |||
30 | // test body and assertions |
||
31 | $this->assertTrue($router->hasParam('foo')); |
||
32 | $this->assertFalse($router->hasParam('unexisting')); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Testing getParam for existing param |
||
37 | */ |
||
38 | public function testGettingExistingParameter(): void |
||
39 | { |
||
40 | // setup |
||
41 | $router = new \Mezon\Router\Router(); |
||
42 | $router->addRoute('/catalog/[i:foo]/', function () { |
||
43 | // do nothing |
||
44 | }); |
||
45 | |||
46 | $router->callRoute('/catalog/1/'); |
||
47 | |||
48 | // test body |
||
49 | $foo = $router->getParam('foo'); |
||
50 | |||
51 | // assertions |
||
52 | $this->assertEquals(1, $foo); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Testing getParam for unexisting param |
||
57 | */ |
||
58 | public function testGettingUnexistingParameter(): void |
||
59 | { |
||
60 | // setup |
||
61 | $router = new \Mezon\Router\Router(); |
||
62 | $router->addRoute('/catalog/[i:foo]/', function () { |
||
63 | // do nothing |
||
64 | }); |
||
65 | |||
66 | $router->callRoute('/catalog/1/'); |
||
67 | |||
68 | $this->expectException(Exception::class); |
||
69 | |||
70 | // test body and assertions |
||
71 | $router->getParam('unexisting'); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Testing exception throwing for unexisting request method |
||
76 | */ |
||
77 | public function testExceptionForUnexistingRequestMethod(): void |
||
78 | { |
||
79 | // setup |
||
80 | $_SERVER['REQUEST_METHOD'] = 'OPTION'; |
||
81 | $router = new \Mezon\Router\Router(); |
||
82 | $router->addRoute('/catalog/[i:foo]/', function () { |
||
83 | // do nothing |
||
84 | }); |
||
85 | |||
86 | // assertions |
||
87 | $this->expectException(Exception::class); |
||
88 | |||
89 | // test body |
||
90 | $router->callRoute('/catalog/1/'); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Testing saving of the route parameters |
||
95 | */ |
||
96 | public function testSavingParameters(): void |
||
97 | { |
||
98 | $router = new \Mezon\Router\Router(); |
||
99 | $router->addRoute('/catalog/[i:foo]/', function ($route, $parameters) { |
||
100 | return $parameters['foo']; |
||
101 | }); |
||
102 | |||
103 | $router->callRoute('/catalog/-1/'); |
||
104 | |||
105 | $this->assertEquals($router->getParam('foo'), '-1', 'Float data violation'); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Testing command special chars. |
||
110 | */ |
||
111 | public function testCommandSpecialChars(): void |
||
112 | { |
||
113 | $router = new \Mezon\Router\Router(); |
||
114 | |||
115 | $router->addRoute('/[a:url]/', function () { |
||
116 | return 'GET'; |
||
117 | }, 'GET'); |
||
118 | |||
119 | $result = $router->callRoute('/.-@/'); |
||
120 | $this->assertEquals($result, 'GET', 'Invalid selected route'); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Testing strings. |
||
125 | */ |
||
126 | public function testStringSpecialChars(): void |
||
127 | { |
||
128 | $router = new \Mezon\Router\Router(); |
||
129 | |||
130 | $router->addRoute('/[s:url]/', function () { |
||
131 | return 'GET'; |
||
132 | }, 'GET'); |
||
133 | |||
134 | $result = $router->callRoute('/, ;:/'); |
||
135 | $this->assertEquals($result, 'GET', 'Invalid selected route'); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Testing invalid id list data types behaviour. |
||
140 | */ |
||
141 | public function testInValidIdListParams(): void |
||
142 | { |
||
143 | $exception = ''; |
||
144 | $router = new \Mezon\Router\Router(); |
||
145 | $router->addRoute('/catalog/[il:cat_id]/', [ |
||
146 | $this, |
||
147 | 'helloWorldOutput' |
||
148 | ]); |
||
149 | |||
150 | try { |
||
151 | $router->callRoute('/catalog/12345./'); |
||
152 | } catch (Exception $e) { |
||
153 | $exception = $e->getMessage(); |
||
154 | } |
||
155 | |||
156 | $msg = "The processor was not found for the route /catalog/12345./"; |
||
157 | |||
158 | $this->assertNotFalse(strpos($exception, $msg), 'Invalid error response'); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Method for checking id list. |
||
163 | */ |
||
164 | public function ilTest($route, $params): string |
||
165 | { |
||
166 | return $params['ids']; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Testing valid id list data types behaviour. |
||
171 | */ |
||
172 | public function testValidIdListParams(): void |
||
173 | { |
||
174 | $router = new \Mezon\Router\Router(); |
||
175 | $router->addRoute('/catalog/[il:ids]/', [ |
||
176 | $this, |
||
177 | 'ilTest' |
||
178 | ]); |
||
179 | |||
180 | $result = $router->callRoute('/catalog/123,456,789/'); |
||
181 | |||
182 | $this->assertEquals($result, '123,456,789', 'Invalid router response'); |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Testing valid id list data types behaviour. |
||
187 | */ |
||
188 | public function testStringParamSecurity(): void |
||
189 | { |
||
190 | $router = new \Mezon\Router\Router(); |
||
191 | $router->addRoute('/catalog/[s:foo]/', function ($route, $parameters) { |
||
192 | return $parameters['foo']; |
||
193 | }); |
||
194 | |||
195 | $result = $router->callRoute('/catalog/123&456/'); |
||
196 | |||
197 | $this->assertEquals($result, '123&456', 'Security data violation'); |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Testing float value. |
||
202 | */ |
||
203 | public function testFloatI(): void |
||
204 | { |
||
205 | $router = new \Mezon\Router\Router(); |
||
206 | $router->addRoute('/catalog/[i:foo]/', function ($route, $parameters) { |
||
207 | return $parameters['foo']; |
||
208 | }); |
||
209 | |||
210 | $result = $router->callRoute('/catalog/1.1/'); |
||
211 | |||
212 | $this->assertEquals($result, '1.1', 'Float data violation'); |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Testing negative float value. |
||
217 | */ |
||
218 | public function testNegativeFloatI(): void |
||
219 | { |
||
220 | $router = new \Mezon\Router\Router(); |
||
221 | $router->addRoute('/catalog/[i:foo]/', function ($route, $parameters) { |
||
222 | return $parameters['foo']; |
||
223 | }); |
||
224 | |||
225 | $result = $router->callRoute('/catalog/-1.1/'); |
||
226 | |||
227 | $this->assertEquals($result, '-1.1', 'Float data violation'); |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Testing positive float value. |
||
232 | */ |
||
233 | public function testPositiveFloatI(): void |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Testing negative integer value |
||
247 | */ |
||
248 | public function testNegativeIntegerI(): void |
||
249 | { |
||
250 | $router = new \Mezon\Router\Router(); |
||
251 | $router->addRoute('/catalog/[i:foo]/', function ($route, $parameters) { |
||
252 | return $parameters['foo']; |
||
253 | }); |
||
254 | |||
255 | $result = $router->callRoute('/catalog/-1/'); |
||
256 | |||
257 | $this->assertEquals('-1', $result, 'Float data violation'); |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Testing positive integer value |
||
262 | */ |
||
263 | public function testPositiveIntegerI(): void |
||
264 | { |
||
265 | $router = new \Mezon\Router\Router(); |
||
266 | $router->addRoute('/catalog/[i:foo]/', function ($route, $parameters) { |
||
267 | return $parameters['foo']; |
||
268 | }); |
||
269 | |||
270 | $result = $router->callRoute('/catalog/1/'); |
||
271 | |||
272 | $this->assertEquals('1', $result, 'Float data violation'); |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Testing dynamic routes for DELETE requests. |
||
277 | */ |
||
278 | public function testDeleteRequestForUnExistingDynamicRoute(): void |
||
279 | { |
||
280 | $_SERVER['REQUEST_METHOD'] = 'DELETE'; |
||
281 | |||
282 | $exception = ''; |
||
283 | $router = new \Mezon\Router\Router(); |
||
284 | $router->addRoute('/catalog/[i:cat_id]', [ |
||
285 | $this, |
||
286 | 'helloWorldOutput' |
||
287 | ]); |
||
288 | |||
289 | try { |
||
290 | $router->callRoute('/catalog/1024/'); |
||
291 | } catch (Exception $e) { |
||
292 | $exception = $e->getMessage(); |
||
293 | } |
||
294 | |||
295 | $msg = "The processor was not found for the route /catalog/1024/"; |
||
296 | |||
297 | $this->assertNotFalse(strpos($exception, $msg), 'Invalid error response'); |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * Testing dynamic routes for DELETE requests. |
||
302 | */ |
||
303 | public function testDeleteRequestForExistingDynamicRoute(): void |
||
304 | { |
||
305 | $_SERVER['REQUEST_METHOD'] = 'DELETE'; |
||
306 | |||
307 | $router = new \Mezon\Router\Router(); |
||
308 | $router->addRoute('/catalog/[i:cat_id]', function ($route) { |
||
309 | return $route; |
||
310 | }, 'DELETE'); |
||
311 | |||
312 | $result = $router->callRoute('/catalog/1024/'); |
||
313 | |||
314 | $this->assertEquals($result, '/catalog/1024/', 'Invalid extracted route'); |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Testing dynamic routes for PUT requests. |
||
319 | */ |
||
320 | public function testPutRequestForUnExistingDynamicRoute(): void |
||
321 | { |
||
322 | $_SERVER['REQUEST_METHOD'] = 'PUT'; |
||
323 | |||
324 | $exception = ''; |
||
325 | $router = new \Mezon\Router\Router(); |
||
326 | $router->addRoute('/catalog/[i:cat_id]', [ |
||
327 | $this, |
||
328 | 'helloWorldOutput' |
||
329 | ]); |
||
330 | |||
331 | try { |
||
332 | $router->callRoute('/catalog/1024/'); |
||
333 | } catch (Exception $e) { |
||
334 | $exception = $e->getMessage(); |
||
335 | } |
||
336 | |||
337 | $msg = "The processor was not found for the route /catalog/1024/"; |
||
338 | |||
339 | $this->assertNotFalse(strpos($exception, $msg), 'Invalid error response'); |
||
340 | } |
||
341 | |||
342 | /** |
||
343 | * Testing dynamic routes for PUT requests. |
||
344 | */ |
||
345 | public function testPutRequestForExistingDynamicRoute(): void |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * Testing dynamic routes for POST requests. |
||
361 | */ |
||
362 | public function testPostRequestForUnExistingDynamicRoute(): void |
||
363 | { |
||
364 | $_SERVER['REQUEST_METHOD'] = 'POST'; |
||
365 | |||
366 | $exception = ''; |
||
367 | $router = new \Mezon\Router\Router(); |
||
368 | $router->addRoute('/catalog/[i:cat_id]', [ |
||
369 | $this, |
||
370 | 'helloWorldOutput' |
||
371 | ]); |
||
372 | |||
373 | try { |
||
374 | $router->callRoute('/catalog/1024/'); |
||
375 | } catch (Exception $e) { |
||
376 | $exception = $e->getMessage(); |
||
377 | } |
||
378 | |||
379 | $msg = "The processor was not found for the route /catalog/1024/"; |
||
380 | |||
381 | $this->assertNotFalse(strpos($exception, $msg), 'Invalid error response'); |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * Testing dynamic routes for POST requests. |
||
386 | */ |
||
387 | public function testPostRequestForExistingDynamicRoute(): void |
||
388 | { |
||
389 | $_SERVER['REQUEST_METHOD'] = 'POST'; |
||
390 | |||
391 | $router = new \Mezon\Router\Router(); |
||
392 | $router->addRoute('/catalog/[i:cat_id]', function ($route) { |
||
393 | return $route; |
||
394 | }, 'POST'); |
||
395 | |||
396 | $result = $router->callRoute('/catalog/1024/'); |
||
397 | |||
398 | $this->assertEquals($result, '/catalog/1024/', 'Invalid extracted route'); |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * Testing invalid data types behaviour. |
||
403 | */ |
||
404 | public function testInvalidType(): void |
||
405 | { |
||
406 | $router = new \Mezon\Router\Router(); |
||
407 | $router->addRoute('/catalog/[unexisting-type:i]/item/', [ |
||
408 | $this, |
||
409 | 'helloWorldOutput' |
||
410 | ]); |
||
411 | |||
412 | try { |
||
413 | $router->callRoute('/catalog/1024/item/'); |
||
414 | $this->assertFalse(true, 'Exception expected'); |
||
415 | } catch (Exception $e) { |
||
416 | $this->assertFalse(false, ''); |
||
417 | } |
||
418 | } |
||
419 | |||
420 | /** |
||
421 | * Testing invalid data types behaviour. |
||
422 | */ |
||
423 | public function testValidInvalidTypes(): void |
||
424 | { |
||
425 | $router = new \Mezon\Router\Router(); |
||
426 | $router->addRoute('/catalog/[i:cat_id]/item/[unexisting-type-trace:item_id]/', [ |
||
427 | $this, |
||
428 | 'helloWorldOutput' |
||
429 | ]); |
||
430 | |||
431 | try { |
||
432 | $router->callRoute('/catalog/1024/item/2048/'); |
||
433 | $this->assertFalse(true, 'Exception expected'); |
||
434 | } catch (Exception $e) { |
||
435 | $this->assertFalse(false, ''); |
||
436 | } |
||
437 | } |
||
438 | |||
439 | /** |
||
440 | * Testing valid data types behaviour. |
||
441 | */ |
||
442 | public function testValidTypes(): void |
||
443 | { |
||
444 | $exception = ''; |
||
445 | $router = new \Mezon\Router\Router(); |
||
446 | $router->addRoute('/catalog/[i:cat_id]/item/[i:item_id]/', [ |
||
447 | $this, |
||
448 | 'helloWorldOutput' |
||
449 | ]); |
||
450 | |||
451 | try { |
||
452 | $router->callRoute('/catalog/1024/item/2048/'); |
||
453 | } catch (Exception $e) { |
||
454 | $exception = $e->getMessage(); |
||
455 | } |
||
456 | |||
457 | $msg = "Illegal parameter type"; |
||
458 | |||
459 | $this->assertFalse(strpos($exception, $msg), 'Valid type expected'); |
||
460 | } |
||
461 | |||
462 | /** |
||
463 | * Testing valid integer data types behaviour. |
||
464 | */ |
||
465 | public function testValidIntegerParams(): void |
||
483 | } |
||
484 | |||
485 | /** |
||
486 | * Testing valid alnum data types behaviour. |
||
487 | */ |
||
488 | public function testValidAlnumParams(): void |
||
506 | } |
||
507 | |||
508 | /** |
||
509 | * Testing invalid integer data types behaviour. |
||
510 | */ |
||
511 | public function testInValidIntegerParams(): void |
||
512 | { |
||
513 | $exception = ''; |
||
514 | $router = new \Mezon\Router\Router(); |
||
515 | $router->addRoute('/catalog/[i:cat_id]/', [ |
||
516 | $this, |
||
517 | 'helloWorldOutput' |
||
518 | ]); |
||
519 | |||
520 | try { |
||
521 | $router->callRoute('/catalog/a1024/'); |
||
522 | } catch (Exception $e) { |
||
523 | $exception = $e->getMessage(); |
||
524 | } |
||
525 | |||
526 | $msg = "The processor was not found for the route /catalog/a1024/"; |
||
527 | |||
528 | $this->assertNotFalse(strpos($exception, $msg), 'Invalid error response'); |
||
529 | } |
||
530 | |||
531 | /** |
||
532 | * Testing invalid alnum data types behaviour. |
||
533 | */ |
||
534 | public function testInValidAlnumParams(): void |
||
552 | } |
||
553 | |||
554 | /** |
||
555 | * Testing parameter extractor. |
||
556 | */ |
||
557 | public function testValidExtractedParameter(): void |
||
567 | } |
||
568 | |||
569 | /** |
||
570 | * Testing parameter extractor. |
||
571 | */ |
||
572 | public function testValidExtractedParameters(): void |
||
573 | { |
||
574 | $router = new \Mezon\Router\Router(); |
||
575 | $router->addRoute( |
||
576 | '/catalog/[a:cat_id]/[i:item_id]', |
||
577 | function ($route, $parameters) { |
||
578 | return $parameters['cat_id'] . $parameters['item_id']; |
||
579 | }); |
||
580 | |||
581 | $result = $router->callRoute('/catalog/foo/1024/'); |
||
582 | |||
583 | $this->assertEquals($result, 'foo1024', 'Invalid extracted parameter'); |
||
584 | } |
||
585 | |||
586 | /** |
||
587 | * Testing parameter extractor. |
||
588 | */ |
||
589 | public function testValidRouteParameter(): void |
||
606 | } |
||
607 | } |
||
608 |