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