Completed
Push — master ( 15f476...52f8b5 )
by Alex
07:26
created

testSingleAllProcessorUnexisting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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