Completed
Push — master ( 00cc1c...3873f0 )
by Alex
01:35
created

clearMethodTestDataProvider()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 11
rs 10
1
<?php
2
namespace Mezon\Router\Tests;
3
4
class StaticRoutesUnitTest extends \PHPUnit\Framework\TestCase
5
{
6
7
    const HELLO_WORLD = 'Hello world!';
8
9
    const HELLO_STATIC_WORLD = 'Hello static world!';
10
11
    const HELLO_STATIC_WORLD_METHOD = '\Mezon\Router\Tests\StaticRoutesUnitTest::staticHelloWorldOutput';
12
13
    use Utils;
14
15
    /**
16
     * Function simply returns string.
17
     */
18
    public function helloWorldOutput(): string
19
    {
20
        return StaticRoutesUnitTest::HELLO_WORLD;
21
    }
22
23
    /**
24
     * Function simply returns string.
25
     */
26
    static public function staticHelloWorldOutput(): string
27
    {
28
        return StaticRoutesUnitTest::HELLO_STATIC_WORLD;
29
    }
30
31
    /**
32
     * Default setup
33
     *
34
     * {@inheritdoc}
35
     * @see \PHPUnit\Framework\TestCase::setUp()
36
     */
37
    public function setUp(): void
38
    {
39
        $_SERVER['REQUEST_METHOD'] = 'GET';
40
    }
41
42
    /**
43
     * Method returns data sets
44
     *
45
     * @return array testing data
46
     */
47
    public function routesDataProvider(): array
48
    {
49
        return [
50
            [
51
                [
52
                    [
53
                        '*',
54
                        [
55
                            $this,
56
                            'helloWorldOutput'
57
                        ]
58
                    ]
59
                ],
60
                '/some-star-compatible-route/',
61
                StaticRoutesUnitTest::HELLO_WORLD
62
            ],
63
            [
64
                [
65
                    [
66
                        '/one-component-static/',
67
                        StaticRoutesUnitTest::HELLO_STATIC_WORLD_METHOD
68
                    ]
69
                ],
70
                '/one-component-static/',
71
                StaticRoutesUnitTest::HELLO_STATIC_WORLD
72
            ],
73
            [
74
                [
75
                    [
76
                        '*',
77
                        [
78
                            $this,
79
                            'helloWorldOutput'
80
                        ]
81
                    ],
82
                    [
83
                        '/to-be-overlapped/',
84
                        StaticRoutesUnitTest::HELLO_STATIC_WORLD_METHOD
85
                    ]
86
                ],
87
                '/some-route/',
88
                StaticRoutesUnitTest::HELLO_WORLD
89
            ],
90
            [
91
                [
92
                    [
93
                        '*',
94
                        [
95
                            $this,
96
                            'helloWorldOutput'
97
                        ]
98
                    ],
99
                    [
100
                        '/index/',
101
                        StaticRoutesUnitTest::HELLO_STATIC_WORLD_METHOD
102
                    ]
103
                ],
104
                '/index/',
105
                StaticRoutesUnitTest::HELLO_WORLD
106
            ],
107
            [
108
                [
109
                    [
110
                        '/index/',
111
                        StaticRoutesUnitTest::HELLO_STATIC_WORLD_METHOD
112
                    ],
113
                    [
114
                        '*',
115
                        [
116
                            $this,
117
                            'helloWorldOutput'
118
                        ]
119
                    ]
120
                ],
121
                '/index/',
122
                StaticRoutesUnitTest::HELLO_STATIC_WORLD
123
            ],
124
            [
125
                [
126
                    [
127
                        '/index/',
128
                        'StaticRoutesUnitTest::staticHelloWorldOutput'
129
                    ],
130
                    [
131
                        '*',
132
                        [
133
                            $this,
134
                            'helloWorldOutput'
135
                        ]
136
                    ]
137
                ],
138
                '/some-route/',
139
                StaticRoutesUnitTest::HELLO_WORLD
140
            ]
141
        ];
142
    }
143
144
    /**
145
     * Testing one processor for all routes
146
     *
147
     * @param array $routes
148
     *            list of routes
149
     * @param string $uri
150
     *            uri to be requested
151
     * @param string $result
152
     *            expected result
153
     * @dataProvider routesDataProvider
154
     */
155
    public function testRoutesHandler(array $routes, string $uri, string $result): void
156
    {
157
        // setup
158
        $router = new \Mezon\Router\Router();
159
160
        foreach ($routes as $route) {
161
            $router->addRoute($route[0], $route[1]);
162
        }
163
164
        // test body
165
        $content = $router->callRoute($uri);
166
167
        // assertions
168
        $this->assertEquals($result, $content);
169
    }
170
171
    /**
172
     * Testing exception throwing if the method was not found
173
     */
174
    public function testUnknownMethodException(): void
175
    {
176
        // setup
177
        $_GET['r'] = 'unexisting-route-method';
178
        $router = new \Mezon\Router\Router();
179
        $router->addRoute('/unexisting-route-method/', [
180
            $this,
181
            'unexistingMethod'
182
        ]);
183
184
        // assertions
185
        $this->expectException(\Exception::class);
186
187
        // test body
188
        $router->callRoute('/unexisting-route-method/');
189
    }
190
191
    /**
192
     * Method returns some testing string
193
     *
194
     * @return string
195
     */
196
    public function subArray(): string
197
    {
198
        return 'subArrayResult';
199
    }
200
201
    /**
202
     * Testing completely not callable trash
203
     */
204
    public function testNotCallableTrash(): void
205
    {
206
        // setup
207
        $_GET['r'] = 'trash';
208
        $router = new \Mezon\Router\Router();
209
        $router->addRoute('/trash/', []);
210
211
        // assertions
212
        $this->expectException(\Exception::class);
213
214
        // test body
215
        $router->callRoute('/trash/');
216
    }
217
218
    /**
219
     * Testing array routes
220
     */
221
    public function testArrayRoutes(): void
222
    {
223
        $router = new \Mezon\Router\Router();
224
        $router->addRoute('/part1/part2/', function ($route) {
225
            return $route;
226
        }, 'GET');
227
228
        $result = $router->callRoute([
229
            'part1',
230
            'part2'
231
        ]);
232
233
        $this->assertEquals($result, 'part1/part2');
234
    }
235
236
    /**
237
     * Testing empty array routes
238
     */
239
    public function testEmptyArrayRoutes(): void
240
    {
241
        $this->setRequestUri('/catalog/item/');
242
243
        $router = new \Mezon\Router\Router();
244
        $router->addRoute('/catalog/item/', function ($route) {
245
            return $route;
246
        }, 'GET');
247
248
        $result = $router->callRoute([
249
            0 => ''
250
        ]);
251
252
        $this->assertEquals($result, 'catalog/item');
253
    }
254
255
    /**
256
     * Testing empty array routes
257
     */
258
    public function testIndexRoute(): void
259
    {
260
        $this->setRequestUri('/');
261
262
        $router = new \Mezon\Router\Router();
263
        $router->addRoute('/index/', function ($route) {
264
            return $route;
265
        }, 'GET');
266
267
        $result = $router->callRoute([
268
            0 => ''
269
        ]);
270
271
        $this->assertEquals($result, 'index');
272
    }
273
274
    /**
275
     * Testing empty array routes
276
     */
277
    public function testMultipleRequestTypes(): void
278
    {
279
        // setup
280
        $this->setRequestUri('/');
281
282
        $router = new \Mezon\Router\Router();
283
        $router->addRoute('/index/', function ($route) {
284
            return $route;
285
        }, [
286
            'GET',
287
            'POST'
288
        ]);
289
290
        $router->callRoute([
291
            0 => ''
292
        ]);
293
294
        $_SERVER['REQUEST_METHOD'] = 'POST';
295
        $result = $router->callRoute([
296
            0 => ''
297
        ]);
298
299
        $this->assertEquals($result, 'index');
300
    }
301
302
    /**
303
     * Testing static routes for DELETE requests.
304
     */
305
    public function testDeleteRequestForUnExistingStaticRoute(): void
306
    {
307
        $_SERVER['REQUEST_METHOD'] = RouterUnitTest::DELETE_REQUEST_METHOD;
308
309
        $exception = '';
310
        $router = new \Mezon\Router\Router();
311
        $router->addRoute('/static-delete-unexisting/', [
312
            $this,
313
            'helloWorldOutput'
314
        ]);
315
316
        try {
317
            $router->callRoute('/static-delete-unexisting/');
318
        } catch (\Exception $e) {
319
            $exception = $e->getMessage();
320
        }
321
322
        $msg = "The processor was not found for the route static-delete-unexisting";
323
324
        $this->assertNotFalse(strpos($exception, $msg));
325
    }
326
327
    /**
328
     * Testing static routes for PUT requests.
329
     */
330
    public function testPutRequestForUnExistingStaticRoute(): void
331
    {
332
        $_SERVER['REQUEST_METHOD'] = 'PUT';
333
334
        $exception = '';
335
        $router = new \Mezon\Router\Router();
336
        $router->addRoute('/static-put-unexisting/', [
337
            $this,
338
            'helloWorldOutput'
339
        ]);
340
341
        try {
342
            $router->callRoute('/static-put-unexisting/');
343
        } catch (\Exception $e) {
344
            $exception = $e->getMessage();
345
        }
346
347
        $msg = "The processor was not found for the route static-put-unexisting";
348
349
        $this->assertNotFalse(strpos($exception, $msg));
350
    }
351
352
    /**
353
     * Testing static routes for POST requests.
354
     */
355
    public function testPostRequestForUnExistingStaticRoute(): void
356
    {
357
        $_SERVER['REQUEST_METHOD'] = 'POST';
358
359
        $exception = '';
360
        $router = new \Mezon\Router\Router();
361
        $router->addRoute('/static-post-unexisting/', [
362
            $this,
363
            'helloWorldOutput'
364
        ]);
365
366
        try {
367
            $router->callRoute('/static-post-unexisting/');
368
        } catch (\Exception $e) {
369
            $exception = $e->getMessage();
370
        }
371
372
        $msg = "The processor was not found for the route static-post-unexisting";
373
374
        $this->assertNotFalse(strpos($exception, $msg));
375
    }
376
377
    /**
378
     * Data provider
379
     *
380
     * @return array
381
     */
382
    public function clearMethodTestDataProvider(): array
383
    {
384
        $result = [];
385
386
        foreach (\Mezon\Router\Router::getListOfSupportedRequestMethods() as $method) {
387
            $result[] = [
388
                $method
389
            ];
390
        }
391
392
        return $result;
393
    }
394
395
    /**
396
     * Testing 'clear' method
397
     *
398
     * @param string $method
399
     *            request method
400
     * @dataProvider clearMethodTestDataProvider
401
     */
402
    public function testClearMethod(string $method): void
403
    {
404
        $router = new \Mezon\Router\Router();
405
        $router->addRoute('/route-to-clear/', function () use ($method) {
406
            return $method;
407
        }, $method);
408
        $router->clear();
409
410
        try {
411
            RouterUnitTest::setRequestMethod($method);
412
            $router->callRoute('/route-to-clear/');
413
            $flag = 'not cleared';
414
        } catch (\Exception $e) {
415
            $flag = 'cleared';
416
        }
417
        $this->assertEquals($flag, 'cleared', 'Data was not cleared');
418
    }
419
420
    /**
421
     * Testing static routes calls for all possible request methods
422
     *
423
     * @param string $method
424
     * @dataProvider clearMethodTestDataProvider
425
     */
426
    public function testRequestForExistingStaticRoute(string $method): void
427
    {
428
        $_SERVER['REQUEST_METHOD'] = $method;
429
430
        $router = new \Mezon\Router\Router();
431
        $router->addRoute('/catalog/', function ($route) {
432
            return $route;
433
        }, $method);
434
435
        $result = $router->callRoute('/catalog/');
436
437
        $this->assertEquals($result, 'catalog');
438
    }
439
440
    /**
441
     * Testing routeExists
442
     */
443
    public function testRouteExists(): void
444
    {
445
        // setup
446
        $router = new \Mezon\Router\Router();
447
        $router->addRoute('/searching-route/', function (string $route) {
448
            return $route;
449
        });
450
451
        // test body and assertions
452
        $this->assertTrue($router->routeExists('searching-route'));
453
        $this->assertFalse($router->routeExists('not-searching-route'));
454
    }
455
}
456