1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
namespace Mezon\Router\Tests\Base; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* |
9
|
|
|
* @psalm-suppress PropertyNotSetInConstructor |
10
|
|
|
*/ |
11
|
|
|
abstract class DynamicRoutesInvalidCasesTestClass extends BaseRouterUnitTestClass |
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
|
|
|
/** |
26
|
|
|
* Testing getParam for unexisting param |
27
|
|
|
*/ |
28
|
|
|
public function testGettingUnexistingParameter(): void |
29
|
|
|
{ |
30
|
|
|
// setup |
31
|
|
|
$router = $this->getRouter(); |
32
|
|
|
$router->addRoute('/catalog/[i:foo]/', function () { |
33
|
|
|
// do nothing |
34
|
|
|
}); |
35
|
|
|
|
36
|
|
|
$router->callRoute('/catalog/1/'); |
37
|
|
|
|
38
|
|
|
$this->expectException(\Exception::class); |
39
|
|
|
|
40
|
|
|
// test body and assertions |
41
|
|
|
$router->getParam('unexisting'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Testing exception throwing for unexisting request method |
46
|
|
|
*/ |
47
|
|
|
public function testExceptionForUnexistingRequestMethod(): void |
48
|
|
|
{ |
49
|
|
|
// setup |
50
|
|
|
$_SERVER['REQUEST_METHOD'] = 'HEAD'; |
51
|
|
|
$router = $this->getRouter(); |
52
|
|
|
$router->addRoute('/catalog/[i:foo]/', function () { |
53
|
|
|
// do nothing |
54
|
|
|
}); |
55
|
|
|
|
56
|
|
|
// assertions |
57
|
|
|
$this->expectException(\Exception::class); |
58
|
|
|
|
59
|
|
|
// test body |
60
|
|
|
$router->callRoute('/catalog/1/'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Testing invalid id list data types behaviour. |
65
|
|
|
*/ |
66
|
|
|
public function testInValidIdListParams(): void |
67
|
|
|
{ |
68
|
|
|
// setup |
69
|
|
|
$router = $this->getRouter(); |
70
|
|
|
$router->addRoute('/catalog/[il:cat_id]/', function () { |
71
|
|
|
return 1; |
72
|
|
|
}); |
73
|
|
|
|
74
|
|
|
// assertion |
75
|
|
|
$this->expectException(\Exception::class); |
76
|
|
|
$this->expectExceptionMessage('The processor was not found for the route catalog/12345.'); |
77
|
|
|
|
78
|
|
|
// test body |
79
|
|
|
$router->callRoute('/catalog/12345./'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Testing dynamic routes for DELETE requests. |
84
|
|
|
*/ |
85
|
|
|
public function testDeleteRequestForUnExistingDynamicRoute(): void |
86
|
|
|
{ |
87
|
|
|
// setup |
88
|
|
|
$_SERVER['REQUEST_METHOD'] = 'DELETE'; |
89
|
|
|
|
90
|
|
|
$exception = ''; |
91
|
|
|
$router = $this->getRouter(); |
92
|
|
|
$router->addRoute('/catalog/[i:cat_id]', function () { |
93
|
|
|
return 1; |
94
|
|
|
}); |
95
|
|
|
|
96
|
|
|
// test body |
97
|
|
|
try { |
98
|
|
|
$router->callRoute('/catalog/1025/'); |
99
|
|
|
} catch (\Exception $e) { |
100
|
|
|
$exception = $e->getMessage(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// assertions |
104
|
|
|
$msg = "The processor was not found for the route catalog/1025"; |
105
|
|
|
|
106
|
|
|
$this->assertNotFalse(strpos($exception, $msg)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Testing dynamic routes for PUT requests. |
111
|
|
|
*/ |
112
|
|
|
public function testPutRequestForUnExistingDynamicRoute(): void |
113
|
|
|
{ |
114
|
|
|
// setup |
115
|
|
|
$_SERVER['REQUEST_METHOD'] = 'PUT'; |
116
|
|
|
|
117
|
|
|
$exception = ''; |
118
|
|
|
$router = $this->getRouter(); |
119
|
|
|
$router->addRoute('/catalog/[i:cat_id]', function () { |
120
|
|
|
return 1; |
121
|
|
|
}); |
122
|
|
|
|
123
|
|
|
// test body |
124
|
|
|
try { |
125
|
|
|
$router->callRoute('/catalog/1024/'); |
126
|
|
|
} catch (\Exception $e) { |
127
|
|
|
$exception = $e->getMessage(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// assertions |
131
|
|
|
$msg = "The processor was not found for the route catalog/1024"; |
132
|
|
|
|
133
|
|
|
$this->assertNotFalse(strpos($exception, $msg)); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Testing dynamic routes for POST requests. |
138
|
|
|
*/ |
139
|
|
|
public function testPostRequestForUnExistingDynamicRoute(): void |
140
|
|
|
{ |
141
|
|
|
// setup |
142
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST'; |
143
|
|
|
|
144
|
|
|
$exception = ''; |
145
|
|
|
$router = $this->getRouter(); |
146
|
|
|
$router->addRoute('/catalog/[i:item_id]', function () { |
147
|
|
|
return 1; |
148
|
|
|
}); |
149
|
|
|
|
150
|
|
|
// test body |
151
|
|
|
try { |
152
|
|
|
$router->callRoute('/catalog/1024/'); |
153
|
|
|
} catch (\Exception $e) { |
154
|
|
|
$exception = $e->getMessage(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
// assertions |
158
|
|
|
$msg = "The processor was not found for the route catalog/1024"; |
159
|
|
|
|
160
|
|
|
$this->assertNotFalse(strpos($exception, $msg)); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Data provider for the testNotMatchingRoutes |
165
|
|
|
* |
166
|
|
|
* @return array testing data |
167
|
|
|
*/ |
168
|
|
|
public function invalidCasesDataProvider(): array |
169
|
|
|
{ |
170
|
|
|
return [ |
171
|
|
|
[ |
172
|
|
|
'/catalog/[i:some_id]', |
173
|
|
|
'/catalog/1/a/' |
174
|
|
|
], |
175
|
|
|
[ |
176
|
|
|
'/existing/[i:bar]/', |
177
|
|
|
'/unexisting/1/' |
178
|
|
|
] |
179
|
|
|
]; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Testing that all invalid cases will be covered |
184
|
|
|
* |
185
|
|
|
* @dataProvider invalidCasesDataProvider |
186
|
|
|
*/ |
187
|
|
|
public function testNotMatchingRoutes(string $route, string $calling): void |
188
|
|
|
{ |
189
|
|
|
// setup |
190
|
|
|
$router = $this->getRouter(); |
191
|
|
|
$router->addRoute($route, function () { |
192
|
|
|
// do nothing |
193
|
|
|
}); |
194
|
|
|
|
195
|
|
|
// assertions |
196
|
|
|
$this->expectException(\Exception::class); |
197
|
|
|
|
198
|
|
|
// test body |
199
|
|
|
$router->callRoute($calling); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Testing invalid data types behaviour. |
204
|
|
|
*/ |
205
|
|
|
public function testInvalidType(): void |
206
|
|
|
{ |
207
|
|
|
// setup |
208
|
|
|
$router = $this->getRouter(); |
209
|
|
|
$router->addRoute('/catalog/[unexisting-type:i]/item/', function () { |
210
|
|
|
return 1; |
211
|
|
|
}); |
212
|
|
|
|
213
|
|
|
// test body and assertions |
214
|
|
|
try { |
215
|
|
|
$router->callRoute('/catalog/1024/item/'); |
216
|
|
|
$this->assertFalse(true, 'Exception expected'); |
217
|
|
|
} catch (\Exception $e) { |
218
|
|
|
$this->assertFalse(false, ''); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Testing invalid data types behaviour. |
224
|
|
|
*/ |
225
|
|
|
public function testValidInvalidTypes(): void |
226
|
|
|
{ |
227
|
|
|
// setup |
228
|
|
|
$router = $this->getRouter(); |
229
|
|
|
$router->addRoute('/catalog/[i:cat_id]/item/[unexisting-type-trace:item_id]/', function () { |
230
|
|
|
return 1; |
231
|
|
|
}); |
232
|
|
|
|
233
|
|
|
// test body and assertions |
234
|
|
|
try { |
235
|
|
|
$router->callRoute('/catalog/1024/item/2048/'); |
236
|
|
|
$this->assertFalse(true, 'Exception expected'); |
237
|
|
|
} catch (\Exception $e) { |
238
|
|
|
$this->assertFalse(false, ''); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Function simply returns string. |
244
|
|
|
*/ |
245
|
|
|
public function helloWorldOutput(): string |
246
|
|
|
{ |
247
|
|
|
return 'Hello world!'; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Testing unexisting route behaviour |
252
|
|
|
*/ |
253
|
|
|
public function testUnexistingRoute(): void |
254
|
|
|
{ |
255
|
|
|
// setup |
256
|
|
|
$exception = ''; |
257
|
|
|
$router = $this->getRouter(); |
258
|
|
|
$router->addRoute('/existing-route/', [ |
259
|
|
|
$this, |
260
|
|
|
'helloWorldOutput' |
261
|
|
|
]); |
262
|
|
|
|
263
|
|
|
// test body |
264
|
|
|
try { |
265
|
|
|
$router->callRoute('/unexisting-route/'); |
266
|
|
|
} catch (\Exception $e) { |
267
|
|
|
$exception = $e->getMessage(); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
// assertions |
271
|
|
|
$msg = "The processor was not found for the route"; |
272
|
|
|
|
273
|
|
|
$this->assertNotFalse(strpos($exception, $msg), 'Valid error handling expected'); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Testing invalid integer data types behaviour. |
278
|
|
|
*/ |
279
|
|
|
public function testInValidIntegerParams(): void |
280
|
|
|
{ |
281
|
|
|
// setup |
282
|
|
|
$exception = ''; |
283
|
|
|
$router = $this->getRouter(); |
284
|
|
|
$router->addRoute('/catalog/[i:cat_id]/', [ |
285
|
|
|
$this, |
286
|
|
|
'helloWorldOutput' |
287
|
|
|
]); |
288
|
|
|
|
289
|
|
|
// test body |
290
|
|
|
try { |
291
|
|
|
$router->callRoute('/catalog/a1024/'); |
292
|
|
|
} catch (\Exception $e) { |
293
|
|
|
$exception = $e->getMessage(); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
// assertions |
297
|
|
|
$msg = "The processor was not found for the route catalog/a1024"; |
298
|
|
|
|
299
|
|
|
$this->assertNotFalse(strpos($exception, $msg), 'Invalid error response'); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Testing invalid alnum data types behaviour. |
304
|
|
|
*/ |
305
|
|
|
public function testInValidAlnumParams(): void |
306
|
|
|
{ |
307
|
|
|
// setup |
308
|
|
|
$exception = ''; |
309
|
|
|
$router = $this->getRouter(); |
310
|
|
|
$router->addRoute('/catalog/[a:cat_id]/', [ |
311
|
|
|
$this, |
312
|
|
|
'helloWorldOutput' |
313
|
|
|
]); |
314
|
|
|
|
315
|
|
|
// test body |
316
|
|
|
try { |
317
|
|
|
$router->callRoute('/catalog/~foo/'); |
318
|
|
|
} catch (\Exception $e) { |
319
|
|
|
$exception = $e->getMessage(); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
// assertions |
323
|
|
|
$msg = "The processor was not found for the route catalog/~foo"; |
324
|
|
|
|
325
|
|
|
$this->assertNotFalse(strpos($exception, $msg), 'Invalid error response'); |
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
|