1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class StaticRoutesUnitTest extends \PHPUnit\Framework\TestCase |
4
|
|
|
{ |
5
|
|
|
|
6
|
|
|
const HELLO_WORLD = 'Hello world!'; |
7
|
|
|
|
8
|
|
|
const HELLO_STATIC_WORLD = 'Hello static world!'; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Function simply returns string. |
12
|
|
|
*/ |
13
|
|
|
public function helloWorldOutput(): string |
14
|
|
|
{ |
15
|
|
|
return StaticRoutesUnitTest::HELLO_WORLD; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Default setup |
20
|
|
|
* |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
* @see \PHPUnit\Framework\TestCase::setUp() |
23
|
|
|
*/ |
24
|
|
|
public function setUp(): void |
25
|
|
|
{ |
26
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Testing one processor for all routes. |
31
|
|
|
*/ |
32
|
|
|
public function testSingleAllProcessor(): void |
33
|
|
|
{ |
34
|
|
|
$router = new \Mezon\Router\Router(); |
35
|
|
|
$router->addRoute('*', [ |
36
|
|
|
$this, |
37
|
|
|
'helloWorldOutput' |
38
|
|
|
]); |
39
|
|
|
|
40
|
|
|
$content = $router->callRoute('/some-star-compatible-route/'); |
41
|
|
|
|
42
|
|
|
$this->assertEquals(StaticRoutesUnitTest::HELLO_WORLD, $content); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Function simply returns string. |
47
|
|
|
*/ |
48
|
|
|
static public function staticHelloWorldOutput(): string |
49
|
|
|
{ |
50
|
|
|
return StaticRoutesUnitTest::HELLO_STATIC_WORLD; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Testing one component router. |
55
|
|
|
*/ |
56
|
|
|
public function testOneComponentRouterStatic(): void |
57
|
|
|
{ |
58
|
|
|
$router = new \Mezon\Router\Router(); |
59
|
|
|
$router->addRoute('/index/', 'StaticRoutesUnitTest::staticHelloWorldOutput'); |
60
|
|
|
|
61
|
|
|
$content = $router->callRoute('/index/'); |
62
|
|
|
|
63
|
|
|
$this->assertEquals(StaticRoutesUnitTest::HELLO_STATIC_WORLD, $content); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Testing one processor for all routes overlap. |
68
|
|
|
*/ |
69
|
|
|
public function testSingleAllProcessorOverlapUnexisting(): void |
70
|
|
|
{ |
71
|
|
|
$router = new \Mezon\Router\Router(); |
72
|
|
|
$router->addRoute('*', [ |
73
|
|
|
$this, |
74
|
|
|
'helloWorldOutput' |
75
|
|
|
]); |
76
|
|
|
$router->addRoute('/index/', 'StaticRoutesUnitTest::staticHelloWorldOutput'); |
77
|
|
|
|
78
|
|
|
$content = $router->callRoute('/some-route/'); |
79
|
|
|
|
80
|
|
|
$this->assertEquals(StaticRoutesUnitTest::HELLO_WORLD, $content); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Testing one processor for all routes overlap. |
85
|
|
|
*/ |
86
|
|
|
public function testSingleAllProcessorOverlapExisting(): void |
87
|
|
|
{ |
88
|
|
|
$router = new \Mezon\Router\Router(); |
89
|
|
|
$router->addRoute('*', [ |
90
|
|
|
$this, |
91
|
|
|
'helloWorldOutput' |
92
|
|
|
]); |
93
|
|
|
$router->addRoute('/index/', 'StaticRoutesUnitTest::staticHelloWorldOutput'); |
94
|
|
|
|
95
|
|
|
$content = $router->callRoute('/index/'); |
96
|
|
|
|
97
|
|
|
$this->assertEquals(StaticRoutesUnitTest::HELLO_WORLD, $content); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Testing one processor for all routes overlap. |
102
|
|
|
*/ |
103
|
|
|
public function testSingleAllProcessorExisting(): void |
104
|
|
|
{ |
105
|
|
|
// setup |
106
|
|
|
$router = new \Mezon\Router\Router(); |
107
|
|
|
$router->addRoute('/index/', 'StaticRoutesUnitTest::staticHelloWorldOutput'); |
108
|
|
|
$router->addRoute('*', [ |
109
|
|
|
$this, |
110
|
|
|
'helloWorldOutput' |
111
|
|
|
]); |
112
|
|
|
|
113
|
|
|
// test body |
114
|
|
|
$content = $router->callRoute('/index/'); |
115
|
|
|
|
116
|
|
|
// assertions |
117
|
|
|
$this->assertEquals(StaticRoutesUnitTest::HELLO_STATIC_WORLD, $content); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Testing one processor for all routes overlap. |
122
|
|
|
*/ |
123
|
|
|
public function testSingleAllProcessorUnexisting(): void |
124
|
|
|
{ |
125
|
|
|
// setup |
126
|
|
|
$router = new \Mezon\Router\Router(); |
127
|
|
|
$router->addRoute('/index/', 'StaticRoutesUnitTest::staticHelloWorldOutput'); |
128
|
|
|
$router->addRoute('*', [ |
129
|
|
|
$this, |
130
|
|
|
'helloWorldOutput' |
131
|
|
|
]); |
132
|
|
|
|
133
|
|
|
// test body |
134
|
|
|
$content = $router->callRoute('/some-route/'); |
135
|
|
|
|
136
|
|
|
// assertions |
137
|
|
|
$this->assertEquals(StaticRoutesUnitTest::HELLO_WORLD, $content); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Testing routeExists |
142
|
|
|
*/ |
143
|
|
|
public function testRouteExists(): void |
144
|
|
|
{ |
145
|
|
|
// setup |
146
|
|
|
$router = new \Mezon\Router\Router(); |
147
|
|
|
$router->addRoute('/searching-route/', function () {}); |
148
|
|
|
|
149
|
|
|
// test body and assertions |
150
|
|
|
$this->assertTrue($router->routeExists('/searching-route/')); |
151
|
|
|
$this->assertFalse($router->routeExists('not-searching-route')); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Testing exception throwing if the method was not found |
156
|
|
|
*/ |
157
|
|
|
public function testUnknownMethodException(): void |
158
|
|
|
{ |
159
|
|
|
// setup |
160
|
|
|
$_GET['r'] = 'unexisting-route-method'; |
161
|
|
|
$router = new \Mezon\Router\Router(); |
162
|
|
|
$router->addRoute('/unexisting-route-method/', [ |
163
|
|
|
$this, |
164
|
|
|
'unexistingMethod' |
165
|
|
|
]); |
166
|
|
|
|
167
|
|
|
// assertions |
168
|
|
|
$this->expectException(\Exception::class); |
169
|
|
|
|
170
|
|
|
// test body |
171
|
|
|
$router->callRoute('/unexisting-route-method/'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Method returns some testing string |
176
|
|
|
* |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
|
|
public function subArray(): string |
180
|
|
|
{ |
181
|
|
|
return 'subArrayResult'; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Testing sub array in route description |
186
|
|
|
*/ |
187
|
|
|
public function testSubArrayCompatibility(): void |
188
|
|
|
{ |
189
|
|
|
// setup and test body |
190
|
|
|
$_GET['r'] = 'sub-array'; |
191
|
|
|
$router = new \Mezon\Router\Router(); |
192
|
|
|
$router->addRoute('/sub-array/', [ |
193
|
|
|
$this, |
194
|
|
|
[ |
195
|
|
|
$this, |
196
|
|
|
'subArray' |
197
|
|
|
] |
198
|
|
|
]); |
199
|
|
|
|
200
|
|
|
// assertions |
201
|
|
|
$this->assertEquals('subArrayResult', $router->callRoute('/sub-array/')); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Testing completely not callable trash |
206
|
|
|
*/ |
207
|
|
|
public function testNotCallableTrash(): void |
208
|
|
|
{ |
209
|
|
|
// setup |
210
|
|
|
$_GET['r'] = 'trash'; |
211
|
|
|
$router = new \Mezon\Router\Router(); |
212
|
|
|
$router->addRoute('/trash/', []); |
213
|
|
|
|
214
|
|
|
// assertions |
215
|
|
|
$this->expectException(\Exception::class); |
216
|
|
|
|
217
|
|
|
// test body |
218
|
|
|
$router->callRoute('/trash/'); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Testing array routes |
223
|
|
|
*/ |
224
|
|
|
public function testArrayRoutes(): void |
225
|
|
|
{ |
226
|
|
|
$router = new \Mezon\Router\Router(); |
227
|
|
|
$router->addRoute('/catalog/item/', function ($route) { |
228
|
|
|
return $route; |
229
|
|
|
}, 'GET'); |
230
|
|
|
|
231
|
|
|
$result = $router->callRoute([ |
232
|
|
|
'catalog', |
233
|
|
|
'item' |
234
|
|
|
]); |
235
|
|
|
|
236
|
|
|
$this->assertEquals($result, '/catalog/item/', 'Invalid extracted route'); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Testing empty array routes |
241
|
|
|
*/ |
242
|
|
|
public function testEmptyArrayRoutes(): void |
243
|
|
|
{ |
244
|
|
|
$_SERVER['REQUEST_URI'] = '/catalog/item/'; |
245
|
|
|
|
246
|
|
|
$router = new \Mezon\Router\Router(); |
247
|
|
|
$router->addRoute('/catalog/item/', function ($route) { |
248
|
|
|
return $route; |
249
|
|
|
}, 'GET'); |
250
|
|
|
|
251
|
|
|
$result = $router->callRoute([ |
252
|
|
|
0 => '' |
253
|
|
|
]); |
254
|
|
|
|
255
|
|
|
$this->assertEquals($result, '/catalog/item/', 'Invalid extracted route'); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Testing empty array routes |
260
|
|
|
*/ |
261
|
|
|
public function testIndexRoute(): void |
262
|
|
|
{ |
263
|
|
|
$_SERVER['REQUEST_URI'] = '/'; |
264
|
|
|
|
265
|
|
|
$router = new \Mezon\Router\Router(); |
266
|
|
|
$router->addRoute('/index/', function ($route) { |
267
|
|
|
return $route; |
268
|
|
|
}, 'GET'); |
269
|
|
|
|
270
|
|
|
$result = $router->callRoute([ |
271
|
|
|
0 => '' |
272
|
|
|
]); |
273
|
|
|
|
274
|
|
|
$this->assertEquals($result, '/index/', 'Invalid extracted route'); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Testing empty array routes |
279
|
|
|
*/ |
280
|
|
|
public function testMultipleRequestTypes(): void |
281
|
|
|
{ |
282
|
|
|
// setup |
283
|
|
|
$_SERVER['REQUEST_URI'] = '/'; |
284
|
|
|
|
285
|
|
|
$router = new \Mezon\Router\Router(); |
286
|
|
|
$router->addRoute('/index/', function ($route) { |
287
|
|
|
return $route; |
288
|
|
|
}, [ |
289
|
|
|
'GET', |
290
|
|
|
'POST' |
291
|
|
|
]); |
292
|
|
|
|
293
|
|
|
$router->callRoute([ |
294
|
|
|
0 => '' |
295
|
|
|
]); |
296
|
|
|
|
297
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST'; |
298
|
|
|
$result = $router->callRoute([ |
299
|
|
|
0 => '' |
300
|
|
|
]); |
301
|
|
|
|
302
|
|
|
$this->assertEquals($result, '/index/', 'Invalid extracted route'); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Testing static routes for DELETE requests. |
307
|
|
|
*/ |
308
|
|
|
public function testDeleteRequestForUnExistingStaticRoute(): void |
309
|
|
|
{ |
310
|
|
|
$_SERVER['REQUEST_METHOD'] = 'DELETE'; |
311
|
|
|
|
312
|
|
|
$exception = ''; |
313
|
|
|
$router = new \Mezon\Router\Router(); |
314
|
|
|
$router->addRoute('/catalog/', [ |
315
|
|
|
$this, |
316
|
|
|
'helloWorldOutput' |
317
|
|
|
]); |
318
|
|
|
|
319
|
|
|
try { |
320
|
|
|
$router->callRoute('/catalog/'); |
321
|
|
|
} catch (Exception $e) { |
322
|
|
|
$exception = $e->getMessage(); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
$msg = "The processor was not found for the route /catalog/"; |
326
|
|
|
|
327
|
|
|
$this->assertNotFalse(strpos($exception, $msg), 'Invalid error response'); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Testing static routes for DELETE requests. |
332
|
|
|
*/ |
333
|
|
|
public function testDeleteRequestForExistingStaticRoute(): void |
334
|
|
|
{ |
335
|
|
|
$_SERVER['REQUEST_METHOD'] = 'DELETE'; |
336
|
|
|
|
337
|
|
|
$router = new \Mezon\Router\Router(); |
338
|
|
|
$router->addRoute('/catalog/', function ($route) { |
339
|
|
|
return $route; |
340
|
|
|
}, 'DELETE'); |
341
|
|
|
|
342
|
|
|
$result = $router->callRoute('/catalog/'); |
343
|
|
|
|
344
|
|
|
$this->assertEquals($result, '/catalog/', 'Invalid extracted route'); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* Testing static routes for PUT requests. |
349
|
|
|
*/ |
350
|
|
|
public function testPutRequestForUnExistingStaticRoute(): void |
351
|
|
|
{ |
352
|
|
|
$_SERVER['REQUEST_METHOD'] = 'PUT'; |
353
|
|
|
|
354
|
|
|
$exception = ''; |
355
|
|
|
$router = new \Mezon\Router\Router(); |
356
|
|
|
$router->addRoute('/catalog/', [ |
357
|
|
|
$this, |
358
|
|
|
'helloWorldOutput' |
359
|
|
|
]); |
360
|
|
|
|
361
|
|
|
try { |
362
|
|
|
$router->callRoute('/catalog/'); |
363
|
|
|
} catch (Exception $e) { |
364
|
|
|
$exception = $e->getMessage(); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
$msg = "The processor was not found for the route /catalog/"; |
368
|
|
|
|
369
|
|
|
$this->assertNotFalse(strpos($exception, $msg), 'Invalid error response'); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* Testing static routes for PUT requests. |
374
|
|
|
*/ |
375
|
|
|
public function testPutRequestForExistingStaticRoute(): void |
376
|
|
|
{ |
377
|
|
|
$_SERVER['REQUEST_METHOD'] = 'PUT'; |
378
|
|
|
|
379
|
|
|
$router = new \Mezon\Router\Router(); |
380
|
|
|
$router->addRoute('/catalog/', function ($route) { |
381
|
|
|
return $route; |
382
|
|
|
}, 'PUT'); |
383
|
|
|
|
384
|
|
|
$result = $router->callRoute('/catalog/'); |
385
|
|
|
|
386
|
|
|
$this->assertEquals($result, '/catalog/', 'Invalid extracted route'); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* Testing static routes for POST requests. |
391
|
|
|
*/ |
392
|
|
|
public function testPostRequestForUnExistingStaticRoute(): void |
393
|
|
|
{ |
394
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST'; |
395
|
|
|
|
396
|
|
|
$exception = ''; |
397
|
|
|
$router = new \Mezon\Router\Router(); |
398
|
|
|
$router->addRoute('/catalog/', [ |
399
|
|
|
$this, |
400
|
|
|
'helloWorldOutput' |
401
|
|
|
]); |
402
|
|
|
|
403
|
|
|
try { |
404
|
|
|
$router->callRoute('/catalog/'); |
405
|
|
|
} catch (Exception $e) { |
406
|
|
|
$exception = $e->getMessage(); |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
$msg = "The processor was not found for the route /catalog/"; |
410
|
|
|
|
411
|
|
|
$this->assertNotFalse(strpos($exception, $msg), 'Invalid error response'); |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* Testing static routes for POST requests. |
416
|
|
|
*/ |
417
|
|
|
public function testPostRequestForExistingStaticRoute(): void |
418
|
|
|
{ |
419
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST'; |
420
|
|
|
|
421
|
|
|
$router = new \Mezon\Router\Router(); |
422
|
|
|
$router->addRoute('/catalog/', function ($route) { |
423
|
|
|
return $route; |
424
|
|
|
}, 'POST'); |
425
|
|
|
|
426
|
|
|
$result = $router->callRoute('/catalog/'); |
427
|
|
|
|
428
|
|
|
$this->assertEquals($result, '/catalog/', 'Invalid extracted route'); |
429
|
|
|
} |
430
|
|
|
} |
431
|
|
|
|