DynamicRoutesTestClass   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 8
eloc 67
c 1
b 1
f 0
dl 0
loc 198
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 3 1
A testTypes() 0 17 2
A testMultyple() 0 17 2
A typesDataProvider() 0 119 3
1
<?php
2
declare(strict_types = 1);
3
namespace Mezon\Router\Tests\Base;
4
5
use Mezon\Router\Types\DateRouterType;
6
use PHPUnit\Framework\TestCase;
7
use Mezon\Router\SupportedRequestMethods;
8
9
/**
10
 *
11
 * @psalm-suppress PropertyNotSetInConstructor
12
 */
13
abstract class DynamicRoutesTestClass extends BaseRouterUnitTestClass
14
{
15
16
    /**
17
     * Default setup
18
     *
19
     * {@inheritdoc}
20
     * @see TestCase::setUp()
21
     */
22
    public function setUp(): void
23
    {
24
        $_SERVER['REQUEST_METHOD'] = 'GET';
25
    }
26
27
    const TYPES_ROUTE_CATALOG_INT_BAR = '/catalog/[i:bar]/';
28
29
    const TYPES_ROUTE_CATALOG_FIX_POINT_BAR = '/catalog/[fp:bar]/';
30
31
    /**
32
     * Data provider for the testTypes
33
     *
34
     * @return array test data
35
     */
36
    public function typesDataProvider(): array
37
    {
38
        $data = [
39
            // #0
40
            [
41
                [
42
                    DynamicRoutesTestClass::TYPES_ROUTE_CATALOG_INT_BAR
43
                ],
44
                '/catalog/1/',
45
                1
46
            ],
47
            // #1
48
            [
49
                [
50
                    DynamicRoutesTestClass::TYPES_ROUTE_CATALOG_INT_BAR
51
                ],
52
                '/catalog/-1/',
53
                - 1
54
            ],
55
            // #2
56
            [
57
                [
58
                    DynamicRoutesTestClass::TYPES_ROUTE_CATALOG_FIX_POINT_BAR
59
                ],
60
                '/catalog/1.1/',
61
                1.1
62
            ],
63
            // #3
64
            [
65
                [
66
                    DynamicRoutesTestClass::TYPES_ROUTE_CATALOG_FIX_POINT_BAR
67
                ],
68
                '/catalog/-1.1/',
69
                - 1.1
70
            ],
71
            // #4
72
            [
73
                [
74
                    '/[a:bar]/'
75
                ],
76
                '/.-@/',
77
                '.-@'
78
            ],
79
            // #5
80
            [
81
                [
82
                    '/[s:bar]/'
83
                ],
84
                '/, ;:/',
85
                ', ;:'
86
            ],
87
            // #6
88
            [
89
                [
90
                    '/[fp:number]/',
91
                    '/[s:bar]/'
92
                ],
93
                '/abc/',
94
                'abc'
95
            ],
96
            // #7, list of integers
97
            [
98
                [
99
                    '/catalog/[il:bar]/'
100
                ],
101
                '/catalog/123,456,789/',
102
                '123,456,789'
103
            ],
104
            // #8, string
105
            [
106
                [
107
                    '/catalog/[s:bar]/'
108
                ],
109
                '/catalog/123&456/',
110
                '123&456'
111
            ],
112
            // #9, parameter name chars testing
113
            [
114
                [
115
                    '/[s:Aa_x-0]/'
116
                ],
117
                '/abc123/',
118
                'abc123',
119
                'Aa_x-0'
120
            ],
121
            // #10, date type testing 1
122
            [
123
                [
124
                    '/[date:dfield]/'
125
                ],
126
                '/2020-02-02/',
127
                '2020-02-02',
128
                'dfield'
129
            ],
130
            // #11, date type testing 2
131
            [
132
                [
133
                    '/posts-[date:dfield]/'
134
                ],
135
                '/posts-2020-02-02/',
136
                '2020-02-02',
137
                'dfield'
138
            ]
139
        ];
140
141
        $return = [];
142
143
        foreach (SupportedRequestMethods::getListOfSupportedRequestMethods() as $method) {
144
            $tmp = array_merge($data);
145
146
            foreach ($tmp as $item) {
147
                $item = array_merge([
148
                    $method
149
                ], $item);
150
                $return[] = $item;
151
            }
152
        }
153
154
        return $return;
155
    }
156
157
    /**
158
     * Testing router types
159
     *
160
     * @param string $method
161
     *            request method
162
     * @param string[] $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, array $pattern, string $route, $expected, string $paramName = 'bar'): void
173
    {
174
        // setup
175
        $_SERVER['REQUEST_METHOD'] = $method;
176
        $router = $this->getRouter();
177
        $router->addType('date', DateRouterType::class);
178
179
        foreach ($pattern as $r) {
180
            $router->addRoute($r, function () {
181
                // do nothing
182
            }, $method);
183
        }
184
185
        $router->callRoute($route);
186
187
        // test body and assertions
188
        $this->assertEquals($expected, $router->getParam($paramName));
189
    }
190
191
    /**
192
     * Testing multyple routes
193
     */
194
    public function testMultyple(): void
195
    {
196
        // setup
197
        $router = $this->getRouter();
198
        for ($i = 0; $i < 15; $i ++) {
199
            $router->addRoute('/multiple/' . $i . '/[i:id]', function () {
200
                return 'done!';
201
            });
202
        }
203
204
        // test body
205
        /** @var string $result */
206
        $result = $router->callRoute('/multiple/' . rand(0, 14) . '/12345');
207
208
        // assertions
209
        $this->assertEquals('done!', $result);
210
        $this->assertEquals('12345', $router->getParam('id'));
211
    }
212
}
213