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