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