Completed
Push — master ( 46045d...6c2bdc )
by Alex
02:01
created

DynamicRoutesUnitTest::testMultyple()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 16
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Mezon\Router\Tests;
3
4
use Mezon\Router\Router;
5
use Mezon\Router\Types\DateRouterType;
6
7
class DynamicRoutesUnitTest extends \PHPUnit\Framework\TestCase
8
{
9
10
    /**
11
     * Default setup
12
     *
13
     * {@inheritdoc}
14
     * @see \PHPUnit\Framework\TestCase::setUp()
15
     */
16
    public function setUp(): void
17
    {
18
        $_SERVER['REQUEST_METHOD'] = 'GET';
19
    }
20
21
    /**
22
     * Testing hasParam method
23
     */
24
    public function testValidatingParameter(): void
25
    {
26
        // setup
27
        $router = new \Mezon\Router\Router();
28
        $router->addRoute('/catalog/[i:foo]/', function () {
29
            // do nothing
30
        });
31
32
        $router->callRoute('/catalog/1/');
33
34
        // test body and assertions
35
        $this->assertTrue($router->hasParam('foo'));
36
        $this->assertFalse($router->hasParam('unexisting'));
37
    }
38
39
    const TYPES_ROUTE_CATALOG_INT_BAR = '/catalog/[i:bar]/';
40
41
    const TYPES_ROUTE_CATALOG_FIX_POINT_BAR = '/catalog/[fp:bar]/';
42
43
    /**
44
     * Data provider for the testTypes
45
     *
46
     * @return array test data
47
     */
48
    public function typesDataProvider(): array
49
    {
50
        $data = [
51
            // #0
52
            [
53
                DynamicRoutesUnitTest::TYPES_ROUTE_CATALOG_INT_BAR,
54
                '/catalog/1/',
55
                1
56
            ],
57
            // #1
58
            [
59
                DynamicRoutesUnitTest::TYPES_ROUTE_CATALOG_INT_BAR,
60
                '/catalog/-1/',
61
                - 1
62
            ],
63
            // #2
64
            [
65
                DynamicRoutesUnitTest::TYPES_ROUTE_CATALOG_INT_BAR,
66
                '/catalog/+1/',
67
                1
68
            ],
69
            // #3
70
            [
71
                DynamicRoutesUnitTest::TYPES_ROUTE_CATALOG_FIX_POINT_BAR,
72
                '/catalog/1.1/',
73
                1.1
74
            ],
75
            // #4
76
            [
77
                DynamicRoutesUnitTest::TYPES_ROUTE_CATALOG_FIX_POINT_BAR,
78
                '/catalog/-1.1/',
79
                - 1.1
80
            ],
81
            // #5
82
            [
83
                DynamicRoutesUnitTest::TYPES_ROUTE_CATALOG_FIX_POINT_BAR,
84
                '/catalog/+1.1/',
85
                1.1
86
            ],
87
            // #6
88
            [
89
                '/[a:bar]/',
90
                '/.-@/',
91
                '.-@'
92
            ],
93
            // #7
94
            [
95
                '/[s:bar]/',
96
                '/, ;:/',
97
                ', ;:'
98
            ],
99
            // #8
100
            [
101
                [
102
                    '/[fp:number]/',
103
                    '/[s:bar]/'
104
                ],
105
                '/abc/',
106
                'abc'
107
            ],
108
            // #9
109
            [
110
                '/catalog/[il:bar]/',
111
                '/catalog/123,456,789/',
112
                '123,456,789'
113
            ],
114
            // #10
115
            [
116
                '/catalog/[s:bar]/',
117
                '/catalog/123&456/',
118
                '123&456'
119
            ],
120
            // #11, parameter name chars testing
121
            [
122
                '/[s:Aa_x-0]/',
123
                '/abc123/',
124
                'abc123',
125
                'Aa_x-0'
126
            ],
127
            // #12, date type testing 1
128
            [
129
                '/[date:dfield]/',
130
                '/2020-02-02/',
131
                '2020-02-02',
132
                'dfield'
133
            ],
134
            // #13, date type testing 2
135
            [
136
                '/posts-[date:dfield]/',
137
                '/posts-2020-02-02/',
138
                '2020-02-02',
139
                'dfield'
140
            ]
141
        ];
142
143
        $return = [];
144
145
        foreach (Router::getListOfSupportedRequestMethods() as $method) {
146
            $tmp = array_merge($data);
147
148
            foreach ($tmp as $item) {
149
                $item = array_merge([
150
                    $method
151
                ], $item);
152
                $return[] = $item;
153
            }
154
        }
155
156
        return $return;
157
    }
158
159
    /**
160
     * Testing router types
161
     *
162
     * @param mixed $pattern
163
     *            route pattern
164
     * @param string $route
165
     *            real route
166
     * @param mixed $expected
167
     *            expected value
168
     * @param string $paramName
169
     *            name of the validating parameter
170
     * @dataProvider typesDataProvider
171
     */
172
    public function testTypes(string $method, $pattern, string $route, $expected, string $paramName = 'bar'): void
173
    {
174
        // setup
175
        $_SERVER['REQUEST_METHOD'] = $method;
176
        $router = new \Mezon\Router\Router();
177
        $router->addType('date', DateRouterType::class);
178
        if (is_string($pattern)) {
179
            $router->addRoute($pattern, function () {
180
                // do nothing
181
            }, $method);
182
        } else {
183
            foreach ($pattern as $r) {
184
                $router->addRoute($r, function () {
185
                    // do nothing
186
                }, $method);
187
            }
188
        }
189
        $router->callRoute($route);
190
191
        // test body and assertions
192
        $this->assertEquals($expected, $router->getParam($paramName));
193
    }
194
195
    /**
196
     * Testing parameter extractor.
197
     */
198
    public function testValidExtractedParameter(): void
199
    {
200
        $router = new \Mezon\Router\Router();
201
        $router->addRoute('/catalog/[a:cat_id]/', function ($route, $parameters) {
202
            return $parameters['cat_id'];
203
        });
204
205
        $result = $router->callRoute('/catalog/foo/');
206
207
        $this->assertEquals($result, 'foo', 'Invalid extracted parameter');
208
    }
209
210
    /**
211
     * Testing parameter extractor.
212
     */
213
    public function testValidExtractedParameters(): void
214
    {
215
        $router = new \Mezon\Router\Router();
216
        $router->addRoute(
217
            '/catalog/[a:cat_id]/[i:item_id]',
218
            function ($route, $parameters) {
219
                return $parameters['cat_id'] . $parameters['item_id'];
220
            });
221
222
        $result = $router->callRoute('catalog/foo/1024');
223
224
        $this->assertEquals($result, 'foo1024', 'Invalid extracted parameter');
225
    }
226
227
    /**
228
     * Testing parameter extractor.
229
     */
230
    public function testValidRouteParameter(): void
231
    {
232
        $router = new \Mezon\Router\Router();
233
        $router->addRoute('/catalog/all/', function ($route) {
234
            return $route;
235
        });
236
        $router->addRoute('/catalog/[i:cat_id]', function ($route) {
237
            return $route;
238
        });
239
240
        // first reading
241
        $result = $router->callRoute('/catalog/all/');
242
        $this->assertEquals($result, 'catalog/all');
243
244
        // second route
245
        $result = $router->callRoute('/catalog/1024/');
246
        $this->assertEquals($result, 'catalog/1024');
247
248
        // reading regexp from cache in the _getRouteMatcherRegExPattern method
249
        $router->warmCache();
250
        $result = $router->callRoute('/catalog/1024/');
251
        $this->assertEquals($result, 'catalog/1024');
252
    }
253
254
    /**
255
     * Testing multyple routes
256
     */
257
    public function testMultyple(): void
258
    {
259
        // setup
260
        $router = new Router();
261
        for ($i = 0; $i < 15; $i ++) {
262
            $router->addRoute('/multiple/' . $i . '/[i:id]', function () {
263
                return 'done!';
264
            });
265
        }
266
267
        // test body
268
        $result = $router->callRoute('/multiple/' . rand(0, 15) . '/12345');
269
270
        // assertions
271
        $this->assertEquals('done!', $result);
272
        $this->assertEquals('12345', $router->getParam('id'));
273
    }
274
}
275