Completed
Push — master ( 369737...64c6ea )
by Alex
07:19
created

testPutRequestForUnExistingStaticRoute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 20
rs 9.8666
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
                        '/index/',
126
                        'StaticRoutesUnitTest::staticHelloWorldOutput'
127
                    ],
128
                    [
129
                        '*',
130
                        [
131
                            $this,
132
                            'helloWorldOutput'
133
                        ]
134
                    ]
135
                ],
136
                '/some-route/',
137
                StaticRoutesUnitTest::HELLO_WORLD
138
            ]
139
        ];
140
    }
141
142
    /**
143
     * Testing one processor for all routes
144
     *
145
     * @param array $routes
146
     *            list of routes
147
     * @param string $uri
148
     *            uri to be requested
149
     * @param string $result
150
     *            expected result
151
     * @dataProvider routesDataProvider
152
     */
153
    public function testRoutesHandler(array $routes, string $uri, string $result): void
154
    {
155
        // setup
156
        $router = new \Mezon\Router\Router();
157
158
        foreach ($routes as $route) {
159
            $router->addRoute($route[0], $route[1]);
160
        }
161
162
        // test body
163
        $content = $router->callRoute($uri);
164
165
        // assertions
166
        $this->assertEquals($result, $content);
167
    }
168
169
    /**
170
     * Testing exception throwing if the method was not found
171
     */
172
    public function testUnknownMethodException(): void
173
    {
174
        // setup
175
        $_GET['r'] = 'unexisting-route-method';
176
        $router = new \Mezon\Router\Router();
177
        $router->addRoute('/unexisting-route-method/', [
178
            $this,
179
            'unexistingMethod'
180
        ]);
181
182
        // assertions
183
        $this->expectException(\Exception::class);
184
185
        // test body
186
        $router->callRoute('/unexisting-route-method/');
187
    }
188
189
    /**
190
     * Method returns some testing string
191
     *
192
     * @return string
193
     */
194
    public function subArray(): string
195
    {
196
        return 'subArrayResult';
197
    }
198
199
    /**
200
     * Testing completely not callable trash
201
     */
202
    public function testNotCallableTrash(): void
203
    {
204
        // setup
205
        $_GET['r'] = 'trash';
206
        $router = new \Mezon\Router\Router();
207
        $router->addRoute('/trash/', []);
208
209
        // assertions
210
        $this->expectException(\Exception::class);
211
212
        // test body
213
        $router->callRoute('/trash/');
214
    }
215
216
    /**
217
     * Testing array routes
218
     */
219
    public function testArrayRoutes(): void
220
    {
221
        $router = new \Mezon\Router\Router();
222
        $router->addRoute('/part1/part2/', function ($route) {
223
            return $route;
224
        }, 'GET');
225
226
        $result = $router->callRoute([
227
            'part1',
228
            'part2'
229
        ]);
230
231
        $this->assertEquals($result, 'part1/part2');
232
    }
233
234
    /**
235
     * Testing empty array routes
236
     */
237
    public function testEmptyArrayRoutes(): void
238
    {
239
        $this->setRequestUri('/catalog/item/');
240
241
        $router = new \Mezon\Router\Router();
242
        $router->addRoute('/catalog/item/', function ($route) {
243
            return $route;
244
        }, 'GET');
245
246
        $result = $router->callRoute([
247
            0 => ''
248
        ]);
249
250
        $this->assertEquals($result, 'catalog/item');
251
    }
252
253
    /**
254
     * Testing empty array routes
255
     */
256
    public function testIndexRoute(): void
257
    {
258
        $this->setRequestUri('/');
259
260
        $router = new \Mezon\Router\Router();
261
        $router->addRoute('/index/', function ($route) {
262
            return $route;
263
        }, 'GET');
264
265
        $result = $router->callRoute([
266
            0 => ''
267
        ]);
268
269
        $this->assertEquals($result, 'index');
270
    }
271
272
    /**
273
     * Testing empty array routes
274
     */
275
    public function testMultipleRequestTypes(): void
276
    {
277
        // setup
278
        $this->setRequestUri('/');
279
280
        $router = new \Mezon\Router\Router();
281
        $router->addRoute('/index/', function ($route) {
282
            return $route;
283
        }, [
284
            'GET',
285
            'POST'
286
        ]);
287
288
        $router->callRoute([
289
            0 => ''
290
        ]);
291
292
        $_SERVER['REQUEST_METHOD'] = 'POST';
293
        $result = $router->callRoute([
294
            0 => ''
295
        ]);
296
297
        $this->assertEquals($result, 'index');
298
    }
299
300
    /**
301
     * Testing static routes for DELETE requests.
302
     */
303
    public function testDeleteRequestForUnExistingStaticRoute(): void
304
    {
305
        $_SERVER['REQUEST_METHOD'] = RouterUnitTest::DELETE_REQUEST_METHOD;
306
307
        $exception = '';
308
        $router = new \Mezon\Router\Router();
309
        $router->addRoute('/static-delete-unexisting/', [
310
            $this,
311
            'helloWorldOutput'
312
        ]);
313
314
        try {
315
            $router->callRoute('/static-delete-unexisting/');
316
        } catch (\Exception $e) {
317
            $exception = $e->getMessage();
318
        }
319
320
        $msg = "The processor was not found for the route static-delete-unexisting";
321
322
        $this->assertNotFalse(strpos($exception, $msg));
323
    }
324
325
    /**
326
     * Testing static routes for DELETE requests.
327
     */
328
    public function testDeleteRequestForExistingStaticRoute(): void
329
    {
330
        $_SERVER['REQUEST_METHOD'] = RouterUnitTest::DELETE_REQUEST_METHOD;
331
332
        $router = new \Mezon\Router\Router();
333
        $router->addRoute('/static-delete-existing/', function ($route) {
334
            return $route;
335
        }, RouterUnitTest::DELETE_REQUEST_METHOD);
336
337
        $result = $router->callRoute('/static-delete-existing/');
338
339
        $this->assertEquals($result, 'static-delete-existing');
340
    }
341
342
    /**
343
     * Testing static routes for PUT requests.
344
     */
345
    public function testPutRequestForUnExistingStaticRoute(): void
346
    {
347
        $_SERVER['REQUEST_METHOD'] = 'PUT';
348
349
        $exception = '';
350
        $router = new \Mezon\Router\Router();
351
        $router->addRoute('/static-put-unexisting/', [
352
            $this,
353
            'helloWorldOutput'
354
        ]);
355
356
        try {
357
            $router->callRoute('/static-put-unexisting/');
358
        } catch (\Exception $e) {
359
            $exception = $e->getMessage();
360
        }
361
362
        $msg = "The processor was not found for the route static-put-unexisting";
363
364
        $this->assertNotFalse(strpos($exception, $msg));
365
    }
366
367
    /**
368
     * Testing static routes for PUT requests.
369
     */
370
    public function testPutRequestForExistingStaticRoute(): void
371
    {
372
        $_SERVER['REQUEST_METHOD'] = 'PUT';
373
374
        $router = new \Mezon\Router\Router();
375
        $router->addRoute('/static-put-existing/', function ($route) {
376
            return $route;
377
        }, 'PUT');
378
379
        $result = $router->callRoute('/static-put-existing/');
380
381
        $this->assertEquals($result, 'static-put-existing');
382
    }
383
384
    /**
385
     * Testing static routes for POST requests.
386
     */
387
    public function testPostRequestForUnExistingStaticRoute(): void
388
    {
389
        $_SERVER['REQUEST_METHOD'] = 'POST';
390
391
        $exception = '';
392
        $router = new \Mezon\Router\Router();
393
        $router->addRoute('/static-post-unexisting/', [
394
            $this,
395
            'helloWorldOutput'
396
        ]);
397
398
        try {
399
            $router->callRoute('/static-post-unexisting/');
400
        } catch (\Exception $e) {
401
            $exception = $e->getMessage();
402
        }
403
404
        $msg = "The processor was not found for the route static-post-unexisting";
405
406
        $this->assertNotFalse(strpos($exception, $msg));
407
    }
408
409
    /**
410
     * Testing static routes for POST requests.
411
     */
412
    public function testPostRequestForExistingStaticRoute(): void
413
    {
414
        $_SERVER['REQUEST_METHOD'] = 'POST';
415
416
        $router = new \Mezon\Router\Router();
417
        $router->addRoute('/catalog/', function ($route) {
418
            return $route;
419
        }, 'POST');
420
421
        $result = $router->callRoute('/catalog/');
422
423
        $this->assertEquals($result, 'catalog');
424
    }
425
426
    /**
427
     * Testing routeExists
428
     */
429
    public function testRouteExists(): void
430
    {
431
        // setup
432
        $router = new \Mezon\Router\Router();
433
        $router->addRoute('/searching-route/', function (string $route) {
434
            return $route;
435
        });
436
437
        // test body and assertions
438
        $this->assertTrue($router->routeExists('searching-route'));
439
        $this->assertFalse($router->routeExists('not-searching-route'));
440
    }
441
}
442