1
|
|
|
<?php |
2
|
|
|
namespace Mezon\Router\Tests; |
3
|
|
|
|
4
|
|
|
class StaticRoutesUnitTest extends \PHPUnit\Framework\TestCase |
5
|
|
|
{ |
6
|
|
|
|
7
|
|
|
const HELLO_WORLD = 'Hello world!'; |
8
|
|
|
|
9
|
|
|
const HELLO_STATIC_WORLD = 'Hello static world!'; |
10
|
|
|
|
11
|
|
|
const HELLO_STATIC_WORLD_METHOD = '\Mezon\Router\Tests\StaticRoutesUnitTest::staticHelloWorldOutput'; |
12
|
|
|
|
13
|
|
|
use Utils; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Function simply returns string. |
17
|
|
|
*/ |
18
|
|
|
public function helloWorldOutput(): string |
19
|
|
|
{ |
20
|
|
|
return StaticRoutesUnitTest::HELLO_WORLD; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Function simply returns string. |
25
|
|
|
*/ |
26
|
|
|
static public function staticHelloWorldOutput(): string |
27
|
|
|
{ |
28
|
|
|
return StaticRoutesUnitTest::HELLO_STATIC_WORLD; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Default setup |
33
|
|
|
* |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
* @see \PHPUnit\Framework\TestCase::setUp() |
36
|
|
|
*/ |
37
|
|
|
public function setUp(): void |
38
|
|
|
{ |
39
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Testing exception throwing if the method was not found |
44
|
|
|
*/ |
45
|
|
|
public function testUnknownMethodException(): void |
46
|
|
|
{ |
47
|
|
|
// setup |
48
|
|
|
$_GET['r'] = 'unexisting-route-method'; |
49
|
|
|
$router = new \Mezon\Router\Router(); |
50
|
|
|
$router->addRoute('/unexisting-route-method/', [ |
51
|
|
|
$this, |
52
|
|
|
'unexistingMethod' |
53
|
|
|
]); |
54
|
|
|
|
55
|
|
|
// assertions |
56
|
|
|
$this->expectException(\Exception::class); |
57
|
|
|
|
58
|
|
|
// test body |
59
|
|
|
$router->callRoute('/unexisting-route-method/'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Method returns some testing string |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function subArray(): string |
68
|
|
|
{ |
69
|
|
|
return 'subArrayResult'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Testing completely not callable trash |
74
|
|
|
*/ |
75
|
|
|
public function testNotCallableTrash(): void |
76
|
|
|
{ |
77
|
|
|
// setup |
78
|
|
|
$_GET['r'] = 'trash'; |
79
|
|
|
$router = new \Mezon\Router\Router(); |
80
|
|
|
$router->addRoute('/trash/', []); |
81
|
|
|
|
82
|
|
|
// assertions |
83
|
|
|
$this->expectException(\Exception::class); |
84
|
|
|
|
85
|
|
|
// test body |
86
|
|
|
$router->callRoute('/trash/'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Testing array routes |
91
|
|
|
*/ |
92
|
|
|
public function testArrayRoutes(): void |
93
|
|
|
{ |
94
|
|
|
$router = new \Mezon\Router\Router(); |
95
|
|
|
$router->addRoute('/part1/part2/', function ($route) { |
96
|
|
|
return $route; |
97
|
|
|
}, 'GET'); |
98
|
|
|
|
99
|
|
|
$result = $router->callRoute([ |
100
|
|
|
'part1', |
101
|
|
|
'part2' |
102
|
|
|
]); |
103
|
|
|
|
104
|
|
|
$this->assertEquals($result, 'part1/part2'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Testing empty array routes |
109
|
|
|
*/ |
110
|
|
|
public function testEmptyArrayRoutes(): void |
111
|
|
|
{ |
112
|
|
|
$this->setRequestUri('/catalog/item/'); |
113
|
|
|
|
114
|
|
|
$router = new \Mezon\Router\Router(); |
115
|
|
|
$router->addRoute('/catalog/item/', function ($route) { |
116
|
|
|
return $route; |
117
|
|
|
}, 'GET'); |
118
|
|
|
|
119
|
|
|
$result = $router->callRoute([ |
120
|
|
|
0 => '' |
121
|
|
|
]); |
122
|
|
|
|
123
|
|
|
$this->assertEquals($result, 'catalog/item'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Testing empty array routes |
128
|
|
|
*/ |
129
|
|
|
public function testIndexRoute(): void |
130
|
|
|
{ |
131
|
|
|
$this->setRequestUri('/'); |
132
|
|
|
|
133
|
|
|
$router = new \Mezon\Router\Router(); |
134
|
|
|
$router->addRoute('/index/', function ($route) { |
135
|
|
|
return $route; |
136
|
|
|
}, 'GET'); |
137
|
|
|
|
138
|
|
|
$result = $router->callRoute([ |
139
|
|
|
0 => '' |
140
|
|
|
]); |
141
|
|
|
|
142
|
|
|
$this->assertEquals($result, 'index'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Testing empty array routes |
147
|
|
|
*/ |
148
|
|
|
public function testMultipleRequestTypes(): void |
149
|
|
|
{ |
150
|
|
|
// setup |
151
|
|
|
$this->setRequestUri('/'); |
152
|
|
|
|
153
|
|
|
$router = new \Mezon\Router\Router(); |
154
|
|
|
$router->addRoute('/index/', function ($route) { |
155
|
|
|
return $route; |
156
|
|
|
}, [ |
157
|
|
|
'GET', |
158
|
|
|
'POST' |
159
|
|
|
]); |
160
|
|
|
|
161
|
|
|
$router->callRoute([ |
162
|
|
|
0 => '' |
163
|
|
|
]); |
164
|
|
|
|
165
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST'; |
166
|
|
|
$result = $router->callRoute([ |
167
|
|
|
0 => '' |
168
|
|
|
]); |
169
|
|
|
|
170
|
|
|
$this->assertEquals($result, 'index'); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Testing static routes for DELETE requests. |
175
|
|
|
*/ |
176
|
|
|
public function testDeleteRequestForUnExistingStaticRoute(): void |
177
|
|
|
{ |
178
|
|
|
$_SERVER['REQUEST_METHOD'] = RouterUnitTest::DELETE_REQUEST_METHOD; |
179
|
|
|
|
180
|
|
|
$exception = ''; |
181
|
|
|
$router = new \Mezon\Router\Router(); |
182
|
|
|
$router->addRoute('/static-delete-unexisting/', [ |
183
|
|
|
$this, |
184
|
|
|
'helloWorldOutput' |
185
|
|
|
]); |
186
|
|
|
|
187
|
|
|
try { |
188
|
|
|
$router->callRoute('/static-delete-unexisting/'); |
189
|
|
|
} catch (\Exception $e) { |
190
|
|
|
$exception = $e->getMessage(); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$msg = "The processor was not found for the route static-delete-unexisting"; |
194
|
|
|
|
195
|
|
|
$this->assertNotFalse(strpos($exception, $msg)); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Testing static routes for PUT requests. |
200
|
|
|
*/ |
201
|
|
|
public function testPutRequestForUnExistingStaticRoute(): void |
202
|
|
|
{ |
203
|
|
|
$_SERVER['REQUEST_METHOD'] = 'PUT'; |
204
|
|
|
|
205
|
|
|
$exception = ''; |
206
|
|
|
$router = new \Mezon\Router\Router(); |
207
|
|
|
$router->addRoute('/static-put-unexisting/', [ |
208
|
|
|
$this, |
209
|
|
|
'helloWorldOutput' |
210
|
|
|
]); |
211
|
|
|
|
212
|
|
|
try { |
213
|
|
|
$router->callRoute('/static-put-unexisting/'); |
214
|
|
|
} catch (\Exception $e) { |
215
|
|
|
$exception = $e->getMessage(); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
$msg = "The processor was not found for the route static-put-unexisting"; |
219
|
|
|
|
220
|
|
|
$this->assertNotFalse(strpos($exception, $msg)); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Testing static routes for POST requests. |
225
|
|
|
*/ |
226
|
|
|
public function testPostRequestForUnExistingStaticRoute(): void |
227
|
|
|
{ |
228
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST'; |
229
|
|
|
|
230
|
|
|
$exception = ''; |
231
|
|
|
$router = new \Mezon\Router\Router(); |
232
|
|
|
$router->addRoute('/static-post-unexisting/', [ |
233
|
|
|
$this, |
234
|
|
|
'helloWorldOutput' |
235
|
|
|
]); |
236
|
|
|
|
237
|
|
|
try { |
238
|
|
|
$router->callRoute('/static-post-unexisting/'); |
239
|
|
|
} catch (\Exception $e) { |
240
|
|
|
$exception = $e->getMessage(); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
$msg = "The processor was not found for the route static-post-unexisting"; |
244
|
|
|
|
245
|
|
|
$this->assertNotFalse(strpos($exception, $msg)); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Data provider |
250
|
|
|
* |
251
|
|
|
* @return array |
252
|
|
|
*/ |
253
|
|
|
public function clearMethodTestDataProvider(): array |
254
|
|
|
{ |
255
|
|
|
$result = []; |
256
|
|
|
|
257
|
|
|
foreach (\Mezon\Router\Router::getListOfSupportedRequestMethods() as $method) { |
258
|
|
|
$result[] = [ |
259
|
|
|
$method |
260
|
|
|
]; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
return $result; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Testing 'clear' method |
268
|
|
|
* |
269
|
|
|
* @param string $method |
270
|
|
|
* request method |
271
|
|
|
* @dataProvider clearMethodTestDataProvider |
272
|
|
|
*/ |
273
|
|
|
public function testClearMethod(string $method): void |
274
|
|
|
{ |
275
|
|
|
$router = new \Mezon\Router\Router(); |
276
|
|
|
$router->addRoute('/route-to-clear/', function () use ($method) { |
277
|
|
|
return $method; |
278
|
|
|
}, $method); |
279
|
|
|
$router->clear(); |
280
|
|
|
|
281
|
|
|
try { |
282
|
|
|
RouterUnitTest::setRequestMethod($method); |
283
|
|
|
$router->callRoute('/route-to-clear/'); |
284
|
|
|
$flag = 'not cleared'; |
285
|
|
|
} catch (\Exception $e) { |
286
|
|
|
$flag = 'cleared'; |
287
|
|
|
} |
288
|
|
|
$this->assertEquals($flag, 'cleared', 'Data was not cleared'); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Testing static routes calls for all possible request methods |
293
|
|
|
* |
294
|
|
|
* @param string $method |
295
|
|
|
* @dataProvider clearMethodTestDataProvider |
296
|
|
|
*/ |
297
|
|
|
public function testRequestForExistingStaticRoute(string $method): void |
298
|
|
|
{ |
299
|
|
|
$_SERVER['REQUEST_METHOD'] = $method; |
300
|
|
|
|
301
|
|
|
$router = new \Mezon\Router\Router(); |
302
|
|
|
$router->addRoute('/catalog/', function ($route) { |
303
|
|
|
return $route; |
304
|
|
|
}, $method); |
305
|
|
|
|
306
|
|
|
$result = $router->callRoute('/catalog/'); |
307
|
|
|
|
308
|
|
|
$this->assertEquals($result, 'catalog'); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Testing routeExists |
313
|
|
|
*/ |
314
|
|
|
public function testRouteExists(): void |
315
|
|
|
{ |
316
|
|
|
// setup |
317
|
|
|
$router = new \Mezon\Router\Router(); |
318
|
|
|
$router->addRoute('/searching-static-route/', function (string $route) { |
319
|
|
|
return $route; |
320
|
|
|
}); |
321
|
|
|
$router->addRoute('/searching-param-route/[i:id]/', function (string $route) { |
322
|
|
|
return $route; |
323
|
|
|
}); |
324
|
|
|
|
325
|
|
|
// test body and assertions |
326
|
|
|
$this->assertTrue($router->routeExists('searching-static-route')); |
327
|
|
|
$this->assertTrue($router->routeExists('searching-param-route/[i:id]')); |
328
|
|
|
$this->assertFalse($router->routeExists('not-searching-route')); |
329
|
|
|
} |
330
|
|
|
} |
331
|
|
|
|