Passed
Push — master ( a4ddd3...82bd79 )
by Alex
06:44
created

DynamicRoutesTestClass::typesDataProvider()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 135
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 53
c 1
b 1
f 0
dl 0
loc 135
rs 9.0254
cc 3
nc 3
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\SuppportedRequestMethods;
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_INT_BAR
59
                ],
60
                '/catalog/+1/',
61
                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
                    DynamicRoutesTestClass::TYPES_ROUTE_CATALOG_FIX_POINT_BAR
75
                ],
76
                '/catalog/-1.1/',
77
                - 1.1
78
            ],
79
            // #5
80
            [
81
                [
82
                    DynamicRoutesTestClass::TYPES_ROUTE_CATALOG_FIX_POINT_BAR
83
                ],
84
                '/catalog/+1.1/',
85
                1.1
86
            ],
87
            // #6
88
            [
89
                [
90
                    '/[a:bar]/'
91
                ],
92
                '/.-@/',
93
                '.-@'
94
            ],
95
            // #7
96
            [
97
                [
98
                    '/[s:bar]/'
99
                ],
100
                '/, ;:/',
101
                ', ;:'
102
            ],
103
            // #8
104
            [
105
                [
106
                    '/[fp:number]/',
107
                    '/[s:bar]/'
108
                ],
109
                '/abc/',
110
                'abc'
111
            ],
112
            // #9
113
            [
114
                [
115
                    '/catalog/[il:bar]/'
116
                ],
117
                '/catalog/123,456,789/',
118
                '123,456,789'
119
            ],
120
            // #10
121
            [
122
                [
123
                    '/catalog/[s:bar]/'
124
                ],
125
                '/catalog/123&456/',
126
                '123&456'
127
            ],
128
            // #11, parameter name chars testing
129
            [
130
                [
131
                    '/[s:Aa_x-0]/'
132
                ],
133
                '/abc123/',
134
                'abc123',
135
                'Aa_x-0'
136
            ],
137
            // #12, date type testing 1
138
            [
139
                [
140
                    '/[date:dfield]/'
141
                ],
142
                '/2020-02-02/',
143
                '2020-02-02',
144
                'dfield'
145
            ],
146
            // #13, date type testing 2
147
            [
148
                [
149
                    '/posts-[date:dfield]/'
150
                ],
151
                '/posts-2020-02-02/',
152
                '2020-02-02',
153
                'dfield'
154
            ]
155
        ];
156
157
        $return = [];
158
159
        foreach (SuppportedRequestMethods::getListOfSupportedRequestMethods() as $method) {
160
            $tmp = array_merge($data);
161
162
            foreach ($tmp as $item) {
163
                $item = array_merge([
164
                    $method
165
                ], $item);
166
                $return[] = $item;
167
            }
168
        }
169
170
        return $return;
171
    }
172
173
    /**
174
     * Testing router types
175
     *
176
     * @param string $method
177
     *            request method
178
     * @param string[] $pattern
179
     *            route pattern
180
     * @param string $route
181
     *            real route
182
     * @param mixed $expected
183
     *            expected value
184
     * @param string $paramName
185
     *            name of the validating parameter
186
     * @dataProvider typesDataProvider
187
     */
188
    public function testTypes(string $method, array $pattern, string $route, $expected, string $paramName = 'bar'): void
189
    {
190
        // setup
191
        $_SERVER['REQUEST_METHOD'] = $method;
192
        $router = $this->getRouter();
193
        $router->addType('date', DateRouterType::class);
194
195
        foreach ($pattern as $r) {
196
            $router->addRoute($r, function () {
197
                // do nothing
198
            }, $method);
199
        }
200
201
        $router->callRoute($route);
202
203
        // test body and assertions
204
        $this->assertEquals($expected, $router->getParam($paramName));
205
    }
206
207
    /**
208
     * Testing multyple routes
209
     */
210
    public function testMultyple(): void
211
    {
212
        // setup
213
        $router = $this->getRouter();
214
        for ($i = 0; $i < 15; $i ++) {
215
            $router->addRoute('/multiple/' . $i . '/[i:id]', function () {
216
                return 'done!';
217
            });
218
        }
219
220
        // test body
221
        /** @var string $result */
222
        $result = $router->callRoute('/multiple/' . rand(0, 14) . '/12345');
223
224
        // assertions
225
        $this->assertEquals('done!', $result);
226
        $this->assertEquals('12345', $router->getParam('id'));
227
    }
228
229
    /**
230
     * Testing real life example #1
231
     */
232
    public function testRealLifeExample1(): void
233
    {
234
        // setup
235
        $router = $this->getRouter();
236
        $router->addRoute('/user/[s:login]/custom-field/[s:name]', function () {
237
            return 'get-custom-field';
238
        });
239
        $router->addRoute('/user/[s:login]/custom-field/[s:name]/add', function () {
240
            return 'add-custom-field';
241
        });
242
        $router->addRoute('/user/[s:login]/custom-field/[s:name]/delete', function () {
243
            return 'delete-custom-field';
244
        });
245
        $router->addRoute('/restore-password/[s:token]', function () {
246
            return 'restore-password';
247
        });
248
        $router->addRoute('/reset-password/[s:token]', function () {
249
            return 'reset-password';
250
        });
251
        $router->addRoute('/user/[s:login]/delete', function () {
252
            return 'user-delete';
253
        });
254
255
        // test body
256
        /** @var string $result */
257
        $result = $router->callRoute('/user/index@localhost/custom-field/name/add');
258
259
        // assertions
260
        $this->assertEquals('add-custom-field', $result);
261
    }
262
}
263