1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Sunrise\Http\Router\Tests\Annotation; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Import classes |
7
|
|
|
*/ |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Sunrise\Http\Router\Annotation\Route as AnnotationRoute; |
10
|
|
|
use Sunrise\Http\Router\Annotation\AnnotationRouteLoader; |
11
|
|
|
use InvalidArgumentException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Import functions |
15
|
|
|
*/ |
16
|
|
|
use function class_alias; |
17
|
|
|
use function class_exists; |
18
|
|
|
use function get_class; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* AnnotationRouteLoaderTest |
22
|
|
|
*/ |
23
|
|
|
class AnnotationRouteLoaderTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return void |
28
|
|
|
*/ |
29
|
|
|
public static function setUpBeforeClass() : void |
30
|
|
|
{ |
31
|
|
|
if (!class_exists('Route')) { |
32
|
|
|
class_alias(AnnotationRoute::class, 'Route'); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
|
public function testAnnotationRouteNameMissing() : void |
40
|
|
|
{ |
41
|
|
|
$this->expectException(InvalidArgumentException::class); |
42
|
|
|
$this->expectExceptionMessage('@Route.name must be not an empty string.'); |
43
|
|
|
|
44
|
|
|
$root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
45
|
|
|
(new AnnotationRouteLoader)->discover($root . '/AnnotationRouteNameMissing'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
public function testAnnotationRouteNameEmpty() : void |
52
|
|
|
{ |
53
|
|
|
$this->expectException(InvalidArgumentException::class); |
54
|
|
|
$this->expectExceptionMessage('@Route.name must be not an empty string.'); |
55
|
|
|
|
56
|
|
|
$root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
57
|
|
|
(new AnnotationRouteLoader)->discover($root . '/AnnotationRouteNameEmpty'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
|
public function testAnnotationRouteNameNotString() : void |
64
|
|
|
{ |
65
|
|
|
$this->expectException(InvalidArgumentException::class); |
66
|
|
|
$this->expectExceptionMessage('@Route.name must be not an empty string.'); |
67
|
|
|
|
68
|
|
|
$root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
69
|
|
|
(new AnnotationRouteLoader)->discover($root . '/AnnotationRouteNameNotString'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
public function testAnnotationRoutePathMissing() : void |
76
|
|
|
{ |
77
|
|
|
$this->expectException(InvalidArgumentException::class); |
78
|
|
|
$this->expectExceptionMessage('@Route.path must be not an empty string.'); |
79
|
|
|
|
80
|
|
|
$root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
81
|
|
|
(new AnnotationRouteLoader)->discover($root . '/AnnotationRoutePathMissing'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
public function testAnnotationRoutePathEmpty() : void |
88
|
|
|
{ |
89
|
|
|
$this->expectException(InvalidArgumentException::class); |
90
|
|
|
$this->expectExceptionMessage('@Route.path must be not an empty string.'); |
91
|
|
|
|
92
|
|
|
$root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
93
|
|
|
(new AnnotationRouteLoader)->discover($root . '/AnnotationRoutePathEmpty'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
|
|
public function testAnnotationRoutePathNotString() : void |
100
|
|
|
{ |
101
|
|
|
$this->expectException(InvalidArgumentException::class); |
102
|
|
|
$this->expectExceptionMessage('@Route.path must be not an empty string.'); |
103
|
|
|
|
104
|
|
|
$root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
105
|
|
|
(new AnnotationRouteLoader)->discover($root . '/AnnotationRoutePathNotString'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
|
|
public function testAnnotationRouteMethodsMissing() : void |
112
|
|
|
{ |
113
|
|
|
$this->expectException(InvalidArgumentException::class); |
114
|
|
|
$this->expectExceptionMessage('@Route.methods must be not an empty array.'); |
115
|
|
|
|
116
|
|
|
$root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
117
|
|
|
(new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMethodsMissing'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return void |
122
|
|
|
*/ |
123
|
|
|
public function testAnnotationRouteMethodsEmpty() : void |
124
|
|
|
{ |
125
|
|
|
$this->expectException(InvalidArgumentException::class); |
126
|
|
|
$this->expectExceptionMessage('@Route.methods must be not an empty array.'); |
127
|
|
|
|
128
|
|
|
$root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
129
|
|
|
(new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMethodsEmpty'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return void |
134
|
|
|
*/ |
135
|
|
|
public function testAnnotationRouteMethodsNotArray() : void |
136
|
|
|
{ |
137
|
|
|
$this->expectException(InvalidArgumentException::class); |
138
|
|
|
$this->expectExceptionMessage('@Route.methods must be not an empty array.'); |
139
|
|
|
|
140
|
|
|
$root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
141
|
|
|
(new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMethodsNotArray'); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return void |
146
|
|
|
*/ |
147
|
|
|
public function testAnnotationRouteMethodsNotStringable() : void |
148
|
|
|
{ |
149
|
|
|
$this->expectException(InvalidArgumentException::class); |
150
|
|
|
$this->expectExceptionMessage('@Route.methods must contain only strings.'); |
151
|
|
|
|
152
|
|
|
$root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
153
|
|
|
(new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMethodsNotStringable'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return void |
158
|
|
|
*/ |
159
|
|
|
public function testAnnotationRouteMiddlewaresNotArray() : void |
160
|
|
|
{ |
161
|
|
|
$this->expectException(InvalidArgumentException::class); |
162
|
|
|
$this->expectExceptionMessage('@Route.middlewares must be an array.'); |
163
|
|
|
|
164
|
|
|
$root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
165
|
|
|
(new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMiddlewaresNotArray'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return void |
170
|
|
|
*/ |
171
|
|
|
public function testAnnotationRouteMiddlewaresNotStringable() : void |
172
|
|
|
{ |
173
|
|
|
$this->expectException(InvalidArgumentException::class); |
174
|
|
|
$this->expectExceptionMessage('@Route.middlewares must contain only middlewares.'); |
175
|
|
|
|
176
|
|
|
$root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
177
|
|
|
(new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMiddlewaresNotStringable'); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @return void |
182
|
|
|
*/ |
183
|
|
|
public function testAnnotationRouteMiddlewaresContainNonexistenceClass() : void |
184
|
|
|
{ |
185
|
|
|
$this->expectException(InvalidArgumentException::class); |
186
|
|
|
$this->expectExceptionMessage('@Route.middlewares must contain only middlewares.'); |
187
|
|
|
|
188
|
|
|
$root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
189
|
|
|
(new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMiddlewaresContainNonexistenceClass'); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return void |
194
|
|
|
*/ |
195
|
|
|
public function testAnnotationRouteMiddlewaresContainNotMiddleware() : void |
196
|
|
|
{ |
197
|
|
|
$this->expectException(InvalidArgumentException::class); |
198
|
|
|
$this->expectExceptionMessage('@Route.middlewares must contain only middlewares.'); |
199
|
|
|
|
200
|
|
|
$root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
201
|
|
|
(new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMiddlewaresContainNotMiddleware'); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @return void |
206
|
|
|
*/ |
207
|
|
|
public function testAnnotationRouteAttributesNotArray() : void |
208
|
|
|
{ |
209
|
|
|
$this->expectException(InvalidArgumentException::class); |
210
|
|
|
$this->expectExceptionMessage('@Route.attributes must be an array.'); |
211
|
|
|
|
212
|
|
|
$root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
213
|
|
|
(new AnnotationRouteLoader)->discover($root . '/AnnotationRouteAttributesNotArray'); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return void |
218
|
|
|
*/ |
219
|
|
|
public function testAnnotationRoutePriorityNotInteger() : void |
220
|
|
|
{ |
221
|
|
|
$this->expectException(InvalidArgumentException::class); |
222
|
|
|
$this->expectExceptionMessage('@Route.priority must be an integer.'); |
223
|
|
|
|
224
|
|
|
$root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
225
|
|
|
(new AnnotationRouteLoader)->discover($root . '/AnnotationRoutePriorityNotInteger'); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @return void |
230
|
|
|
* |
231
|
|
|
* @todo This test needs to be improved... |
232
|
|
|
*/ |
233
|
|
|
public function testBuildRoutes() : void |
234
|
|
|
{ |
235
|
|
|
$loader = new AnnotationRouteLoader(); |
236
|
|
|
$loader->discover(__DIR__ . '/../Fixture/Annotation/AnnotationRouteValid'); |
237
|
|
|
|
238
|
|
|
$routesMap = []; |
239
|
|
|
$builtRoutes = $loader->buildRoutes(); |
240
|
|
|
|
241
|
|
|
foreach ($builtRoutes as $i => $route) { |
242
|
|
|
$routesMap[$i]['name'] = $route->getName(); |
243
|
|
|
$routesMap[$i]['path'] = $route->getPath(); |
244
|
|
|
$routesMap[$i]['methods'] = $route->getMethods(); |
245
|
|
|
$routesMap[$i]['requestHandler'] = get_class($route->getRequestHandler()); |
246
|
|
|
$routesMap[$i]['middlewares'] = []; |
247
|
|
|
$routesMap[$i]['attributes'] = $route->getAttributes(); |
248
|
|
|
|
249
|
|
|
foreach ($route->getMiddlewares() as $middleware) { |
250
|
|
|
$routesMap[$i]['middlewares'][] = get_class($middleware); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
$expectedMap = [ |
255
|
|
|
[ |
256
|
|
|
'name' => 'quuuux', |
257
|
|
|
'path' => '/quuuux', |
258
|
|
|
'methods' => ['PATCH', 'DELETE'], |
259
|
|
|
'requestHandler' => 'Sunrise\Http\Router\Tests\Fixture\Annotation' . |
260
|
|
|
'\AnnotationRouteValid\Subdirectory\QuuuuxRequestHandler', |
261
|
|
|
'middlewares' => [ |
262
|
|
|
'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
263
|
|
|
'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
264
|
|
|
], |
265
|
|
|
'attributes' => [ |
266
|
|
|
'foo' => 'bar', |
267
|
|
|
'source' => 'quuuux', |
268
|
|
|
], |
269
|
|
|
], |
270
|
|
|
[ |
271
|
|
|
'name' => 'quuux', |
272
|
|
|
'path' => '/quuux', |
273
|
|
|
'methods' => ['PUT', 'PATCH'], |
274
|
|
|
'requestHandler' => 'Sunrise\Http\Router\Tests\Fixture\Annotation' . |
275
|
|
|
'\AnnotationRouteValid\Subdirectory\QuuuxRequestHandler', |
276
|
|
|
'middlewares' => [ |
277
|
|
|
'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
278
|
|
|
'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
279
|
|
|
], |
280
|
|
|
'attributes' => [ |
281
|
|
|
'foo' => 'bar', |
282
|
|
|
'source' => 'quuux', |
283
|
|
|
], |
284
|
|
|
], |
285
|
|
|
[ |
286
|
|
|
'name' => 'quux', |
287
|
|
|
'path' => '/quux', |
288
|
|
|
'methods' => ['POST', 'PUT'], |
289
|
|
|
'requestHandler' => 'Sunrise\Http\Router\Tests\Fixture\Annotation' . |
290
|
|
|
'\AnnotationRouteValid\QuuxRequestHandler', |
291
|
|
|
'middlewares' => [ |
292
|
|
|
'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
293
|
|
|
'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
294
|
|
|
], |
295
|
|
|
'attributes' => [ |
296
|
|
|
'foo' => 'bar', |
297
|
|
|
'source' => 'quux', |
298
|
|
|
], |
299
|
|
|
], |
300
|
|
|
[ |
301
|
|
|
'name' => 'qux', |
302
|
|
|
'path' => '/qux', |
303
|
|
|
'methods' => ['GET', 'POST'], |
304
|
|
|
'requestHandler' => 'Sunrise\Http\Router\Tests\Fixture\Annotation' . |
305
|
|
|
'\AnnotationRouteValid\QuxRequestHandler', |
306
|
|
|
'middlewares' => [ |
307
|
|
|
'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
308
|
|
|
'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
309
|
|
|
], |
310
|
|
|
'attributes' => [ |
311
|
|
|
'foo' => 'bar', |
312
|
|
|
'source' => 'qux', |
313
|
|
|
], |
314
|
|
|
], |
315
|
|
|
]; |
316
|
|
|
|
317
|
|
|
$this->assertSame($expectedMap, $routesMap); |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|