Total Complexity | 42 |
Total Lines | 534 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like RouterUnitTest 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 RouterUnitTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
3 | class RouterUnitTest 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 |
||
15 | } |
||
16 | |||
17 | /** |
||
18 | * Function simply returns string. |
||
19 | */ |
||
20 | public function helloWorldOutput(): string |
||
23 | } |
||
24 | |||
25 | /** |
||
26 | * Testing action #1. |
||
27 | */ |
||
28 | public function actionA1(): string |
||
29 | { |
||
30 | return 'action #1'; |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * Testing action #2. |
||
35 | */ |
||
36 | public function actionA2(): string |
||
37 | { |
||
38 | return 'action #2'; |
||
39 | } |
||
40 | |||
41 | public function actionDoubleWord(): string |
||
42 | { |
||
43 | return 'action double word'; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Testing one component router. |
||
48 | */ |
||
49 | public function testOneComponentRouterClassMethod(): void |
||
50 | { |
||
51 | $router = new \Mezon\Router\Router(); |
||
52 | |||
53 | $router->addRoute('/index/', [ |
||
54 | $this, |
||
55 | 'helloWorldOutput' |
||
56 | ]); |
||
57 | |||
58 | $content = $router->callRoute('/index/'); |
||
59 | |||
60 | $this->assertEquals('Hello world!', $content); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Testing one component router. |
||
65 | */ |
||
66 | public function testOneComponentRouterLambda(): void |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Testing unexisting route behaviour. |
||
81 | */ |
||
82 | public function testUnexistingRoute(): void |
||
83 | { |
||
84 | $exception = ''; |
||
85 | $router = new \Mezon\Router\Router(); |
||
86 | $router->addRoute('/index/', [ |
||
87 | $this, |
||
88 | 'helloWorldOutput' |
||
89 | ]); |
||
90 | |||
91 | try { |
||
92 | $router->callRoute('/unexisting-route/'); |
||
93 | } catch (Exception $e) { |
||
94 | $exception = $e->getMessage(); |
||
95 | } |
||
96 | |||
97 | $msg = "The processor was not found for the route"; |
||
98 | |||
99 | $this->assertNotFalse(strpos($exception, $msg), 'Valid error handling expected'); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Testing action fetching method. |
||
104 | */ |
||
105 | public function testClassActions(): void |
||
106 | { |
||
107 | $router = new \Mezon\Router\Router(); |
||
108 | $router->fetchActions($this); |
||
109 | |||
110 | $content = $router->callRoute('/a1/'); |
||
111 | $this->assertEquals('action #1', $content, 'Invalid a1 route'); |
||
112 | |||
113 | $content = $router->callRoute('/a2/'); |
||
114 | $this->assertEquals('action #2', $content, 'Invalid a2 route'); |
||
115 | |||
116 | $content = $router->callRoute('/double-word/'); |
||
117 | $this->assertEquals('action double word', $content, 'Invalid a2 route'); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Method tests POST actions |
||
122 | */ |
||
123 | public function testPostClassAction(): void |
||
124 | { |
||
125 | $_SERVER['REQUEST_METHOD'] = 'POST'; |
||
126 | |||
127 | $router = new \Mezon\Router\Router(); |
||
128 | $router->fetchActions($this); |
||
129 | $content = $router->callRoute('/a1/'); |
||
130 | $this->assertEquals('action #1', $content, 'Invalid a1 route'); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Testing invalid data types behaviour. |
||
135 | */ |
||
136 | public function testInvalidType(): void |
||
137 | { |
||
138 | $router = new \Mezon\Router\Router(); |
||
139 | $router->addRoute('/catalog/[unexisting-type:i]/item/', [ |
||
140 | $this, |
||
141 | 'helloWorldOutput' |
||
142 | ]); |
||
143 | |||
144 | try { |
||
145 | $router->callRoute('/catalog/1024/item/'); |
||
146 | $this->assertFalse(true, 'Exception expected'); |
||
147 | } catch (Exception $e) { |
||
148 | $this->assertFalse(false, ''); |
||
149 | } |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Testing invalid data types behaviour. |
||
154 | */ |
||
155 | public function testValidInvalidTypes(): void |
||
156 | { |
||
157 | $router = new \Mezon\Router\Router(); |
||
158 | $router->addRoute('/catalog/[i:cat_id]/item/[unexisting-type-trace:item_id]/', [ |
||
159 | $this, |
||
160 | 'helloWorldOutput' |
||
161 | ]); |
||
162 | |||
163 | try { |
||
164 | $router->callRoute('/catalog/1024/item/2048/'); |
||
165 | $this->assertFalse(true, 'Exception expected'); |
||
166 | } catch (Exception $e) { |
||
167 | $this->assertFalse(false, ''); |
||
168 | } |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Testing valid data types behaviour. |
||
173 | */ |
||
174 | public function testValidTypes(): void |
||
175 | { |
||
176 | $exception = ''; |
||
177 | $router = new \Mezon\Router\Router(); |
||
178 | $router->addRoute('/catalog/[i:cat_id]/item/[i:item_id]/', [ |
||
179 | $this, |
||
180 | 'helloWorldOutput' |
||
181 | ]); |
||
182 | |||
183 | try { |
||
184 | $router->callRoute('/catalog/1024/item/2048/'); |
||
185 | } catch (Exception $e) { |
||
186 | $exception = $e->getMessage(); |
||
187 | } |
||
188 | |||
189 | $msg = "Illegal parameter type"; |
||
190 | |||
191 | $this->assertFalse(strpos($exception, $msg), 'Valid type expected'); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Testing valid integer data types behaviour. |
||
196 | */ |
||
197 | public function testValidIntegerParams(): void |
||
198 | { |
||
199 | $exception = ''; |
||
200 | $router = new \Mezon\Router\Router(); |
||
201 | $router->addRoute('/catalog/[i:cat_id]/', [ |
||
202 | $this, |
||
203 | 'helloWorldOutput' |
||
204 | ]); |
||
205 | |||
206 | try { |
||
207 | $router->callRoute('/catalog/1024/'); |
||
208 | } catch (Exception $e) { |
||
209 | $exception = $e->getMessage(); |
||
210 | } |
||
211 | |||
212 | $msg = "Illegal parameter type"; |
||
213 | |||
214 | $this->assertFalse(strpos($exception, $msg), 'Valid type expected'); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Testing valid alnum data types behaviour. |
||
219 | */ |
||
220 | public function testValidAlnumParams(): void |
||
221 | { |
||
222 | $exception = ''; |
||
223 | $router = new \Mezon\Router\Router(); |
||
224 | $router->addRoute('/catalog/[a:cat_id]/', [ |
||
225 | $this, |
||
226 | 'helloWorldOutput' |
||
227 | ]); |
||
228 | |||
229 | try { |
||
230 | $router->callRoute('/catalog/foo/'); |
||
231 | } catch (Exception $e) { |
||
232 | $exception = $e->getMessage(); |
||
233 | } |
||
234 | |||
235 | $msg = "Illegal parameter type"; |
||
236 | |||
237 | $this->assertFalse(strpos($exception, $msg), 'Valid type expected'); |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Testing invalid integer data types behaviour. |
||
242 | */ |
||
243 | public function testInValidIntegerParams(): void |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Testing invalid alnum data types behaviour. |
||
265 | */ |
||
266 | public function testInValidAlnumParams(): void |
||
267 | { |
||
268 | $exception = ''; |
||
269 | $router = new \Mezon\Router\Router(); |
||
270 | $router->addRoute('/catalog/[a:cat_id]/', [ |
||
271 | $this, |
||
272 | 'helloWorldOutput' |
||
273 | ]); |
||
274 | |||
275 | try { |
||
276 | $router->callRoute('/catalog/~foo/'); |
||
277 | } catch (Exception $e) { |
||
278 | $exception = $e->getMessage(); |
||
279 | } |
||
280 | |||
281 | $msg = "The processor was not found for the route /catalog/~foo/"; |
||
282 | |||
283 | $this->assertNotFalse(strpos($exception, $msg), 'Invalid error response'); |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * Testing parameter extractor. |
||
288 | */ |
||
289 | public function testValidExtractedParameter(): void |
||
290 | { |
||
291 | $router = new \Mezon\Router\Router(); |
||
292 | $router->addRoute('/catalog/[a:cat_id]/', function ($route, $parameters) { |
||
293 | return $parameters['cat_id']; |
||
294 | }); |
||
295 | |||
296 | $result = $router->callRoute('/catalog/foo/'); |
||
297 | |||
298 | $this->assertEquals($result, 'foo', 'Invalid extracted parameter'); |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Testing parameter extractor. |
||
303 | */ |
||
304 | public function testValidExtractedParameters(): void |
||
305 | { |
||
306 | $router = new \Mezon\Router\Router(); |
||
307 | $router->addRoute( |
||
308 | '/catalog/[a:cat_id]/[i:item_id]', |
||
309 | function ($route, $parameters) { |
||
310 | return $parameters['cat_id'] . $parameters['item_id']; |
||
311 | }); |
||
312 | |||
313 | $result = $router->callRoute('/catalog/foo/1024/'); |
||
314 | |||
315 | $this->assertEquals($result, 'foo1024', 'Invalid extracted parameter'); |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * Testing parameter extractor. |
||
320 | */ |
||
321 | public function testValidRouteParameter(): void |
||
322 | { |
||
323 | $router = new \Mezon\Router\Router(); |
||
324 | $router->addRoute('/catalog/', function ($route) { |
||
325 | return $route; |
||
326 | }); |
||
327 | $router->addRoute('/catalog/[i:cat_id]', function ($route) { |
||
328 | return $route; |
||
329 | }); |
||
330 | |||
331 | $result = $router->callRoute('/catalog/'); |
||
332 | |||
333 | $this->assertEquals($result, '/catalog/', 'Invalid extracted route'); |
||
334 | |||
335 | $result = $router->callRoute('/catalog/1024/'); |
||
336 | |||
337 | $this->assertEquals($result, '/catalog/1024/', 'Invalid extracted route'); |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * Testing static routes for POST requests. |
||
342 | */ |
||
343 | public function testPostRequestForExistingStaticRoute(): void |
||
344 | { |
||
345 | $_SERVER['REQUEST_METHOD'] = 'POST'; |
||
346 | |||
347 | $router = new \Mezon\Router\Router(); |
||
348 | $router->addRoute('/catalog/', function ($route) { |
||
349 | return $route; |
||
350 | }, 'POST'); |
||
351 | |||
352 | $result = $router->callRoute('/catalog/'); |
||
353 | |||
354 | $this->assertEquals($result, '/catalog/', 'Invalid extracted route'); |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * Testing dynamic routes for POST requests. |
||
359 | */ |
||
360 | public function testPostRequestForExistingDynamicRoute(): void |
||
361 | { |
||
362 | $_SERVER['REQUEST_METHOD'] = 'POST'; |
||
363 | |||
364 | $router = new \Mezon\Router\Router(); |
||
365 | $router->addRoute('/catalog/[i:cat_id]', function ($route) { |
||
366 | return $route; |
||
367 | }, 'POST'); |
||
368 | |||
369 | $result = $router->callRoute('/catalog/1024/'); |
||
370 | |||
371 | $this->assertEquals($result, '/catalog/1024/', 'Invalid extracted route'); |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * Testing static routes for POST requests. |
||
376 | */ |
||
377 | public function testPostRequestForUnExistingStaticRoute(): void |
||
378 | { |
||
379 | $_SERVER['REQUEST_METHOD'] = 'POST'; |
||
380 | |||
381 | $exception = ''; |
||
382 | $router = new \Mezon\Router\Router(); |
||
383 | $router->addRoute('/catalog/', [ |
||
384 | $this, |
||
385 | 'helloWorldOutput' |
||
386 | ]); |
||
387 | |||
388 | try { |
||
389 | $router->callRoute('/catalog/'); |
||
390 | } catch (Exception $e) { |
||
391 | $exception = $e->getMessage(); |
||
392 | } |
||
393 | |||
394 | $msg = "The processor was not found for the route /catalog/"; |
||
395 | |||
396 | $this->assertNotFalse(strpos($exception, $msg), 'Invalid error response'); |
||
397 | } |
||
398 | |||
399 | /** |
||
400 | * Testing dynamic routes for POST requests. |
||
401 | */ |
||
402 | public function testPostRequestForUnExistingDynamicRoute(): void |
||
403 | { |
||
404 | $_SERVER['REQUEST_METHOD'] = 'POST'; |
||
405 | |||
406 | $exception = ''; |
||
407 | $router = new \Mezon\Router\Router(); |
||
408 | $router->addRoute('/catalog/[i:cat_id]', [ |
||
409 | $this, |
||
410 | 'helloWorldOutput' |
||
411 | ]); |
||
412 | |||
413 | try { |
||
414 | $router->callRoute('/catalog/1024/'); |
||
415 | } catch (Exception $e) { |
||
416 | $exception = $e->getMessage(); |
||
417 | } |
||
418 | |||
419 | $msg = "The processor was not found for the route /catalog/1024/"; |
||
420 | |||
421 | $this->assertNotFalse(strpos($exception, $msg), 'Invalid error response'); |
||
422 | } |
||
423 | |||
424 | /** |
||
425 | * Testing case when both GET and POST processors exists. |
||
426 | */ |
||
427 | public function testGetPostPostDeleteRouteConcurrency(): void |
||
458 | } |
||
459 | |||
460 | /** |
||
461 | * Testing 'clear' method. |
||
462 | */ |
||
463 | public function testClearMethod(): void |
||
464 | { |
||
465 | $router = new \Mezon\Router\Router(); |
||
466 | $router->addRoute('/catalog/', function () { |
||
467 | return 'POST'; |
||
468 | }, 'POST'); |
||
469 | $router->addRoute('/catalog/', function () { |
||
470 | return 'GET'; |
||
471 | }, 'GET'); |
||
472 | $router->addRoute('/catalog/', function () { |
||
473 | return 'PUT'; |
||
474 | }, 'PUT'); |
||
475 | $router->addRoute('/catalog/', function () { |
||
476 | return 'DELETE'; |
||
477 | }, 'DELETE'); |
||
478 | $router->clear(); |
||
479 | |||
480 | try { |
||
481 | $_SERVER['REQUEST_METHOD'] = 'POST'; |
||
482 | $router->callRoute('/catalog/'); |
||
483 | $flag = 'not cleared'; |
||
484 | } catch (Exception $e) { |
||
485 | $flag = 'cleared'; |
||
486 | } |
||
487 | $this->assertEquals($flag, 'cleared', 'Data was not cleared'); |
||
488 | |||
489 | try { |
||
490 | $_SERVER['REQUEST_METHOD'] = 'GET'; |
||
491 | $router->callRoute('/catalog/'); |
||
492 | $flag = 'not cleared'; |
||
493 | } catch (Exception $e) { |
||
494 | $flag = 'cleared'; |
||
495 | } |
||
496 | $this->assertEquals($flag, 'cleared', 'Data was not cleared'); |
||
497 | |||
498 | try { |
||
499 | $_SERVER['REQUEST_METHOD'] = 'PUT'; |
||
500 | $router->callRoute('/catalog/'); |
||
501 | $flag = 'not cleared'; |
||
502 | } catch (Exception $e) { |
||
503 | $flag = 'cleared'; |
||
504 | } |
||
505 | $this->assertEquals($flag, 'cleared', 'Data was not cleared'); |
||
506 | |||
507 | try { |
||
508 | $_SERVER['REQUEST_METHOD'] = 'DELETE'; |
||
509 | $router->callRoute('/catalog/'); |
||
510 | $flag = 'not cleared'; |
||
511 | } catch (Exception $e) { |
||
512 | $flag = 'cleared'; |
||
513 | } |
||
514 | $this->assertEquals($flag, 'cleared', 'Data was not cleared'); |
||
515 | } |
||
516 | |||
517 | /** |
||
518 | * Method increments assertion count |
||
519 | */ |
||
520 | protected function errorHandler(): void |
||
523 | } |
||
524 | |||
525 | /** |
||
526 | * Test validate custom error handlers. |
||
527 | */ |
||
528 | public function testSetErrorHandler(): void |
||
537 | } |
||
538 | } |
||
539 |