Completed
Push — master ( 6db92e...c5cf91 )
by Alex
02:09
created

DynamicRoutesUnitTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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
            // #0
49
            [
50
                DynamicRoutesUnitTest::TYPES_ROUTE_CATALOG_INT_BAR,
51
                '/catalog/1/',
52
                1
53
            ],
54
            // #1
55
            [
56
                DynamicRoutesUnitTest::TYPES_ROUTE_CATALOG_INT_BAR,
57
                '/catalog/-1/',
58
                - 1
59
            ],
60
            // #2
61
            [
62
                DynamicRoutesUnitTest::TYPES_ROUTE_CATALOG_INT_BAR,
63
                '/catalog/+1/',
64
                1
65
            ],
66
            // #3
67
            [
68
                DynamicRoutesUnitTest::TYPES_ROUTE_CATALOG_FIX_POINT_BAR,
69
                '/catalog/1.1/',
70
                1.1
71
            ],
72
            // #4
73
            [
74
                DynamicRoutesUnitTest::TYPES_ROUTE_CATALOG_FIX_POINT_BAR,
75
                '/catalog/-1.1/',
76
                - 1.1
77
            ],
78
            // #5
79
            [
80
                DynamicRoutesUnitTest::TYPES_ROUTE_CATALOG_FIX_POINT_BAR,
81
                '/catalog/+1.1/',
82
                1.1
83
            ],
84
            // #6
85
            [
86
                '/[a:bar]/',
87
                '/.-@/',
88
                '.-@'
89
            ],
90
            // #7
91
            [
92
                '/[s:bar]/',
93
                '/, ;:/',
94
                ', ;:'
95
            ],
96
            // #8
97
            [
98
                [
99
                    '/[fp:number]/',
100
                    '/[s:bar]/'
101
                ],
102
                '/abc/',
103
                'abc'
104
            ],
105
            // #9
106
            [
107
                '/catalog/[il:bar]/',
108
                '/catalog/123,456,789/',
109
                '123,456,789'
110
            ],
111
            // #10
112
            [
113
                '/catalog/[s:bar]/',
114
                '/catalog/123&456/',
115
                '123&456'
116
            ],
117
            // #11, parameter name chars testing
118
            [
119
                '/[s:Aa_x-0]/',
120
                '/abc123/',
121
                'abc123',
122
                'Aa_x-0'
123
            ]
124
        ];
125
    }
126
127
    /**
128
     * Testing router types
129
     *
130
     * @param mixed $pattern
131
     *            route pattern
132
     * @param string $route
133
     *            real route
134
     * @param mixed $expected
135
     *            expected value
136
     * @param string $paramName
137
     *            name of the validating parameter
138
     * @dataProvider typesDataProvider
139
     */
140
    public function testTypes($pattern, string $route, $expected, string $paramName = 'bar'): void
141
    {
142
        // setup
143
        $router = new \Mezon\Router\Router();
144
        if (is_string($pattern)) {
145
            $router->addRoute($pattern, function () {
146
                // do nothing
147
            });
148
        } else {
149
            foreach ($pattern as $r) {
150
                $router->addRoute($r, function () {
151
                    // do nothing
152
                });
153
            }
154
        }
155
        $router->callRoute($route);
156
157
        // test body and assertions
158
        $this->assertEquals($expected, $router->getParam($paramName));
159
    }
160
161
    /**
162
     * Testing dynamic routes for DELETE requests.
163
     */
164
    public function testDeleteRequestForExistingDynamicRoute(): void
165
    {
166
        $_SERVER['REQUEST_METHOD'] = 'DELETE';
167
168
        $router = new \Mezon\Router\Router();
169
        $router->addRoute('/catalog/[i:cat_id]', function ($route) {
170
            return $route;
171
        }, 'DELETE');
172
173
        $result = $router->callRoute('/catalog/123/');
174
175
        $this->assertEquals($result, 'catalog/123');
176
    }
177
178
    /**
179
     * Testing dynamic routes for PUT requests.
180
     */
181
    public function testPutRequestForExistingDynamicRoute(): void
182
    {
183
        $_SERVER['REQUEST_METHOD'] = 'PUT';
184
185
        $router = new \Mezon\Router\Router();
186
        $router->addRoute('/catalog/[i:cat_id]', function ($route) {
187
            return $route;
188
        }, 'PUT');
189
190
        $result = $router->callRoute('/catalog/1024/');
191
192
        $this->assertEquals($result, 'catalog/1024');
193
    }
194
195
    /**
196
     * Testing dynamic routes for POST requests.
197
     */
198
    public function testPostRequestForExistingDynamicRoute(): void
199
    {
200
        $_SERVER['REQUEST_METHOD'] = 'POST';
201
202
        $router = new \Mezon\Router\Router();
203
        $router->addRoute('/catalog/[i:cat_id]', function ($route) {
204
            return $route;
205
        }, 'POST');
206
207
        $result = $router->callRoute('/catalog/1024/');
208
209
        $this->assertEquals($result, 'catalog/1024');
210
    }
211
212
    /**
213
     * Testing valid data types behaviour.
214
     */
215
    public function testValidTypes(): void
216
    {
217
        $exception = '';
218
        $router = new \Mezon\Router\Router();
219
        $router->addRoute('/catalog/[i:cat_id]/item/[i:item_id]/', [
220
            $this,
221
            'helloWorldOutput'
222
        ]);
223
224
        try {
225
            $router->callRoute('/catalog/1024/item/2048/');
226
        } catch (\Exception $e) {
227
            $exception = $e->getMessage();
228
        }
229
230
        $msg = "Illegal parameter type";
231
232
        $this->assertFalse(strpos($exception, $msg));
233
    }
234
235
    /**
236
     * Testing valid integer data types behaviour.
237
     */
238
    public function testValidIntegerParams(): void
239
    {
240
        $exception = '';
241
        $router = new \Mezon\Router\Router();
242
        $router->addRoute('/catalog/[i:cat_id]/', [
243
            $this,
244
            'helloWorldOutput'
245
        ]);
246
247
        try {
248
            $router->callRoute('/catalog/1024/');
249
        } catch (\Exception $e) {
250
            $exception = $e->getMessage();
251
        }
252
253
        $msg = "Illegal parameter type";
254
255
        $this->assertFalse(strpos($exception, $msg));
256
    }
257
258
    /**
259
     * Testing valid alnum data types behaviour.
260
     */
261
    public function testValidAlnumParams(): void
262
    {
263
        $exception = '';
264
        $router = new \Mezon\Router\Router();
265
        $router->addRoute('/catalog/[a:cat_id]/', [
266
            $this,
267
            'helloWorldOutput'
268
        ]);
269
270
        try {
271
            $router->callRoute('/catalog/foo/');
272
        } catch (\Exception $e) {
273
            $exception = $e->getMessage();
274
        }
275
276
        $msg = "Illegal parameter type";
277
278
        $this->assertFalse(strpos($exception, $msg));
279
    }
280
281
    /**
282
     * Testing invalid integer data types behaviour.
283
     */
284
    public function testInValidIntegerParams(): void
285
    {
286
        $exception = '';
287
        $router = new \Mezon\Router\Router();
288
        $router->addRoute('/catalog/[i:cat_id]/', [
289
            $this,
290
            'helloWorldOutput'
291
        ]);
292
293
        try {
294
            $router->callRoute('/catalog/a1024/');
295
        } catch (\Exception $e) {
296
            $exception = $e->getMessage();
297
        }
298
299
        $msg = "The processor was not found for the route catalog/a1024";
300
301
        $this->assertNotFalse(strpos($exception, $msg), 'Invalid error response');
302
    }
303
304
    /**
305
     * Testing invalid alnum data types behaviour.
306
     */
307
    public function testInValidAlnumParams(): void
308
    {
309
        $exception = '';
310
        $router = new \Mezon\Router\Router();
311
        $router->addRoute('/catalog/[a:cat_id]/', [
312
            $this,
313
            'helloWorldOutput'
314
        ]);
315
316
        try {
317
            $router->callRoute('/catalog/~foo/');
318
        } catch (\Exception $e) {
319
            $exception = $e->getMessage();
320
        }
321
322
        $msg = "The processor was not found for the route catalog/~foo";
323
324
        $this->assertNotFalse(strpos($exception, $msg), 'Invalid error response');
325
    }
326
327
    /**
328
     * Testing parameter extractor.
329
     */
330
    public function testValidExtractedParameter(): void
331
    {
332
        $router = new \Mezon\Router\Router();
333
        $router->addRoute('/catalog/[a:cat_id]/', function ($route, $parameters) {
334
            return $parameters['cat_id'];
335
        });
336
337
        $result = $router->callRoute('/catalog/foo/');
338
339
        $this->assertEquals($result, 'foo', 'Invalid extracted parameter');
340
    }
341
342
    /**
343
     * Testing parameter extractor.
344
     */
345
    public function testValidExtractedParameters(): void
346
    {
347
        $router = new \Mezon\Router\Router();
348
        $router->addRoute(
349
            '/catalog/[a:cat_id]/[i:item_id]',
350
            function ($route, $parameters) {
351
                return $parameters['cat_id'] . $parameters['item_id'];
352
            });
353
354
        $result = $router->callRoute('catalog/foo/1024');
355
356
        $this->assertEquals($result, 'foo1024', 'Invalid extracted parameter');
357
    }
358
359
    /**
360
     * Testing parameter extractor.
361
     */
362
    public function testValidRouteParameter(): void
363
    {
364
        $router = new \Mezon\Router\Router();
365
        $router->addRoute('/catalog/all/', function ($route) {
366
            return $route;
367
        });
368
        $router->addRoute('/catalog/[i:cat_id]', function ($route) {
369
            return $route;
370
        });
371
372
        // first reading
373
        $result = $router->callRoute('/catalog/all/');
374
        $this->assertEquals($result, 'catalog/all');
375
376
        // second route
377
        $result = $router->callRoute('/catalog/1024/');
378
        $this->assertEquals($result, 'catalog/1024');
379
380
        // reading regexp from cache in the _getRouteMatcherRegExPattern method
381
        $router->warmCache();
382
        $result = $router->callRoute('/catalog/1024/');
383
        $this->assertEquals($result, 'catalog/1024');
384
    }
385
}
386