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

testPutRequestForExistingDynamicRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
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 12
rs 10
1
<?php
2
namespace Mezon\Router\Tests;
3
4
class DynamicRoutesUnitTest extends \PHPUnit\Framework\TestCase
5
{
6
7
    /**
8
     * Default setup
9
     *
10
     * {@inheritdoc}
11
     * @see \PHPUnit\Framework\TestCase::setUp()
12
     */
13
    public function setUp(): void
14
    {
15
        $_SERVER['REQUEST_METHOD'] = 'GET';
16
    }
17
18
    /**
19
     * Testing hasParam method
20
     */
21
    public function testValidatingParameter(): void
22
    {
23
        // setup
24
        $router = new \Mezon\Router\Router();
25
        $router->addRoute('/catalog/[i:foo]/', function () {
26
            // do nothing
27
        });
28
29
        $router->callRoute('/catalog/1/');
30
31
        // test body and assertions
32
        $this->assertTrue($router->hasParam('foo'));
33
        $this->assertFalse($router->hasParam('unexisting'));
34
    }
35
36
    const TYPES_ROUTE_CATALOG_INT_BAR = '/catalog/[i:bar]/';
37
38
    const TYPES_ROUTE_CATALOG_FIX_POINT_BAR = '/catalog/[fp:bar]/';
39
40
    /**
41
     * Data provider for the testTypes
42
     *
43
     * @return array test data
44
     */
45
    public function typesDataProvider(): array
46
    {
47
        return [
48
            [
49
                DynamicRoutesUnitTest::TYPES_ROUTE_CATALOG_INT_BAR,
50
                '/catalog/1/',
51
                1
52
            ],
53
            [
54
                DynamicRoutesUnitTest::TYPES_ROUTE_CATALOG_INT_BAR,
55
                '/catalog/-1/',
56
                - 1
57
            ],
58
            [
59
                DynamicRoutesUnitTest::TYPES_ROUTE_CATALOG_INT_BAR,
60
                '/catalog/+1/',
61
                1
62
            ],
63
            [
64
                DynamicRoutesUnitTest::TYPES_ROUTE_CATALOG_FIX_POINT_BAR,
65
                '/catalog/1.1/',
66
                1.1
67
            ],
68
            [
69
                DynamicRoutesUnitTest::TYPES_ROUTE_CATALOG_FIX_POINT_BAR,
70
                '/catalog/-1.1/',
71
                - 1.1
72
            ],
73
            [
74
                DynamicRoutesUnitTest::TYPES_ROUTE_CATALOG_FIX_POINT_BAR,
75
                '/catalog/+1.1/',
76
                1.1
77
            ],
78
            [
79
                '/[a:bar]/',
80
                '/.-@/',
81
                '.-@'
82
            ],
83
            [
84
                '/[s:bar]/',
85
                '/, ;:/',
86
                ', ;:'
87
            ],
88
            [
89
                [
90
                    '/[fp:number]/',
91
                    '/[s:bar]/'
92
                ],
93
                '/abc/',
94
                'abc'
95
            ]
96
        ];
97
    }
98
99
    /**
100
     * Testing router types
101
     *
102
     * @param mixed $pattern
103
     *            route pattern
104
     * @param string $route
105
     *            real route
106
     * @param mixed $expected
107
     *            expected value
108
     * @dataProvider typesDataProvider
109
     */
110
    public function testTypes($pattern, string $route, $expected): void
111
    {
112
        // setup
113
        $router = new \Mezon\Router\Router();
114
        if (is_string($pattern)) {
115
            $router->addRoute($pattern, function () {
116
                // do nothing
117
            });
118
        } else {
119
            foreach ($pattern as $r) {
120
                $router->addRoute($r, function () {
121
                    // do nothing
122
                });
123
            }
124
        }
125
        $router->callRoute($route);
126
127
        // test body and assertions
128
        $this->assertEquals($expected, $router->getParam('bar'));
129
    }
130
131
    /**
132
     * Method for checking id list.
133
     */
134
    public function ilTest($route, $params): string
135
    {
136
        return $params['ids'];
137
    }
138
139
    /**
140
     * Testing valid id list data types behaviour.
141
     */
142
    public function testValidIdListParams(): void
143
    {
144
        $router = new \Mezon\Router\Router();
145
        $router->addRoute('/catalog/[il:ids]/', [
146
            $this,
147
            'ilTest'
148
        ]);
149
150
        $result = $router->callRoute('/catalog/123,456,789/');
151
152
        $this->assertEquals($result, '123,456,789', 'Invalid router response');
153
    }
154
155
    /**
156
     * Testing valid id list data types behaviour.
157
     */
158
    public function testStringParamSecurity(): void
159
    {
160
        $router = new \Mezon\Router\Router();
161
        $router->addRoute('/catalog/[s:foo]/', function ($route, $parameters) {
162
            return $parameters['foo'];
163
        });
164
165
        $result = $router->callRoute('/catalog/123&456/');
166
167
        $this->assertEquals($result, '123&456', 'Security data violation');
168
    }
169
170
    /**
171
     * Testing dynamic routes for DELETE requests.
172
     */
173
    public function testDeleteRequestForExistingDynamicRoute(): void
174
    {
175
        $_SERVER['REQUEST_METHOD'] = 'DELETE';
176
177
        $router = new \Mezon\Router\Router();
178
        $router->addRoute('/catalog/[i:cat_id]', function ($route) {
179
            return $route;
180
        }, 'DELETE');
181
182
        $result = $router->callRoute('/catalog/123/');
183
184
        $this->assertEquals($result, 'catalog/123');
185
    }
186
187
    /**
188
     * Testing dynamic routes for PUT requests.
189
     */
190
    public function testPutRequestForExistingDynamicRoute(): void
191
    {
192
        $_SERVER['REQUEST_METHOD'] = 'PUT';
193
194
        $router = new \Mezon\Router\Router();
195
        $router->addRoute('/catalog/[i:cat_id]', function ($route) {
196
            return $route;
197
        }, 'PUT');
198
199
        $result = $router->callRoute('/catalog/1024/');
200
201
        $this->assertEquals($result, 'catalog/1024');
202
    }
203
204
    /**
205
     * Testing dynamic routes for POST requests.
206
     */
207
    public function testPostRequestForExistingDynamicRoute(): void
208
    {
209
        $_SERVER['REQUEST_METHOD'] = 'POST';
210
211
        $router = new \Mezon\Router\Router();
212
        $router->addRoute('/catalog/[i:cat_id]', function ($route) {
213
            return $route;
214
        }, 'POST');
215
216
        $result = $router->callRoute('/catalog/1024/');
217
218
        $this->assertEquals($result, 'catalog/1024');
219
    }
220
221
    /**
222
     * Testing invalid data types behaviour.
223
     */
224
    public function testInvalidType(): void
225
    {
226
        $router = new \Mezon\Router\Router();
227
        $router->addRoute('/catalog/[unexisting-type:i]/item/', [
228
            $this,
229
            'helloWorldOutput'
230
        ]);
231
232
        try {
233
            $router->callRoute('/catalog/1024/item/');
234
            $this->assertFalse(true, 'Exception expected');
235
        } catch (\Exception $e) {
236
            $this->assertFalse(false, '');
237
        }
238
    }
239
240
    /**
241
     * Testing invalid data types behaviour.
242
     */
243
    public function testValidInvalidTypes(): void
244
    {
245
        $router = new \Mezon\Router\Router();
246
        $router->addRoute('/catalog/[i:cat_id]/item/[unexisting-type-trace:item_id]/', [
247
            $this,
248
            'helloWorldOutput'
249
        ]);
250
251
        try {
252
            $router->callRoute('/catalog/1024/item/2048/');
253
            $this->assertFalse(true, 'Exception expected');
254
        } catch (\Exception $e) {
255
            $this->assertFalse(false, '');
256
        }
257
    }
258
259
    /**
260
     * Testing valid data types behaviour.
261
     */
262
    public function testValidTypes(): void
263
    {
264
        $exception = '';
265
        $router = new \Mezon\Router\Router();
266
        $router->addRoute('/catalog/[i:cat_id]/item/[i:item_id]/', [
267
            $this,
268
            'helloWorldOutput'
269
        ]);
270
271
        try {
272
            $router->callRoute('/catalog/1024/item/2048/');
273
        } catch (\Exception $e) {
274
            $exception = $e->getMessage();
275
        }
276
277
        $msg = "Illegal parameter type";
278
279
        $this->assertFalse(strpos($exception, $msg));
280
    }
281
282
    /**
283
     * Testing valid integer data types behaviour.
284
     */
285
    public function testValidIntegerParams(): void
286
    {
287
        $exception = '';
288
        $router = new \Mezon\Router\Router();
289
        $router->addRoute('/catalog/[i:cat_id]/', [
290
            $this,
291
            'helloWorldOutput'
292
        ]);
293
294
        try {
295
            $router->callRoute('/catalog/1024/');
296
        } catch (\Exception $e) {
297
            $exception = $e->getMessage();
298
        }
299
300
        $msg = "Illegal parameter type";
301
302
        $this->assertFalse(strpos($exception, $msg));
303
    }
304
305
    /**
306
     * Testing valid alnum data types behaviour.
307
     */
308
    public function testValidAlnumParams(): void
309
    {
310
        $exception = '';
311
        $router = new \Mezon\Router\Router();
312
        $router->addRoute('/catalog/[a:cat_id]/', [
313
            $this,
314
            'helloWorldOutput'
315
        ]);
316
317
        try {
318
            $router->callRoute('/catalog/foo/');
319
        } catch (\Exception $e) {
320
            $exception = $e->getMessage();
321
        }
322
323
        $msg = "Illegal parameter type";
324
325
        $this->assertFalse(strpos($exception, $msg));
326
    }
327
328
    /**
329
     * Testing invalid integer data types behaviour.
330
     */
331
    public function testInValidIntegerParams(): void
332
    {
333
        $exception = '';
334
        $router = new \Mezon\Router\Router();
335
        $router->addRoute('/catalog/[i:cat_id]/', [
336
            $this,
337
            'helloWorldOutput'
338
        ]);
339
340
        try {
341
            $router->callRoute('/catalog/a1024/');
342
        } catch (\Exception $e) {
343
            $exception = $e->getMessage();
344
        }
345
346
        $msg = "The processor was not found for the route catalog/a1024";
347
348
        $this->assertNotFalse(strpos($exception, $msg), 'Invalid error response');
349
    }
350
351
    /**
352
     * Testing invalid alnum data types behaviour.
353
     */
354
    public function testInValidAlnumParams(): void
355
    {
356
        $exception = '';
357
        $router = new \Mezon\Router\Router();
358
        $router->addRoute('/catalog/[a:cat_id]/', [
359
            $this,
360
            'helloWorldOutput'
361
        ]);
362
363
        try {
364
            $router->callRoute('/catalog/~foo/');
365
        } catch (\Exception $e) {
366
            $exception = $e->getMessage();
367
        }
368
369
        $msg = "The processor was not found for the route catalog/~foo";
370
371
        $this->assertNotFalse(strpos($exception, $msg), 'Invalid error response');
372
    }
373
374
    /**
375
     * Testing parameter extractor.
376
     */
377
    public function testValidExtractedParameter(): void
378
    {
379
        $router = new \Mezon\Router\Router();
380
        $router->addRoute('/catalog/[a:cat_id]/', function ($route, $parameters) {
381
            return $parameters['cat_id'];
382
        });
383
384
        $result = $router->callRoute('/catalog/foo/');
385
386
        $this->assertEquals($result, 'foo', 'Invalid extracted parameter');
387
    }
388
389
    /**
390
     * Testing parameter extractor.
391
     */
392
    public function testValidExtractedParameters(): void
393
    {
394
        $router = new \Mezon\Router\Router();
395
        $router->addRoute(
396
            '/catalog/[a:cat_id]/[i:item_id]',
397
            function ($route, $parameters) {
398
                return $parameters['cat_id'] . $parameters['item_id'];
399
            });
400
401
        $result = $router->callRoute('catalog/foo/1024');
402
403
        $this->assertEquals($result, 'foo1024', 'Invalid extracted parameter');
404
    }
405
406
    /**
407
     * Testing parameter extractor.
408
     */
409
    public function testValidRouteParameter(): void
410
    {
411
        $router = new \Mezon\Router\Router();
412
        $router->addRoute('/catalog/all/', function ($route) {
413
            return $route;
414
        });
415
        $router->addRoute('/catalog/[i:cat_id]', function ($route) {
416
            return $route;
417
        });
418
419
        // first reading
420
        $result = $router->callRoute('/catalog/all/');
421
        $this->assertEquals($result, 'catalog/all');
422
423
        // second route
424
        $result = $router->callRoute('/catalog/1024/');
425
        $this->assertEquals($result, 'catalog/1024');
426
427
        // reading regexp from cache in the _getRouteMatcherRegExPattern method
428
        $result = $router->callRoute('/catalog/1024/');
429
        $this->assertEquals($result, 'catalog/1024');
430
    }
431
}
432