-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathRouteTest.php
382 lines (353 loc) · 12.2 KB
/
RouteTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php
/*
* This file is part of Aplus Framework Routing Library.
*
* (c) Natan Felles <natanfelles@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tests\Routing;
use Framework\HTTP\Request;
use Framework\HTTP\Response;
use Framework\Routing\Route;
use Framework\Routing\RouteActions;
use Framework\Routing\Router;
use Framework\Routing\RoutingException;
use PHPUnit\Framework\TestCase;
use Tests\Routing\Support\WithoutRouteActions;
use Tests\Routing\Support\WithRouteActions;
final class RouteTest extends TestCase
{
protected Response $response;
protected Route $route;
protected Router $router;
protected function setUp() : void
{
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['HTTP_HOST'] = 'domain.tld';
$_SERVER['REQUEST_URI'] = '/users/25';
$this->response = new Response(new Request());
$this->router = new Router($this->response);
$this->route = new Route(
$this->router,
'{scheme}://domain.tld',
'/users/{int}',
static function (array $params, mixed ...$construct) {
return \implode(', ', [...$params, ...$construct]);
}
);
}
public function testOrigin() : void
{
self::assertSame('{scheme}://domain.tld', $this->route->getOrigin());
self::assertSame('https://domain.tld', $this->route->getOrigin('https'));
}
public function testUrl() : void
{
self::assertSame(
'{scheme}://domain.tld/users/{int}',
$this->route->getUrl()
);
self::assertSame(
'http://domain.tld/users/{int}',
$this->route->getUrl(['http'])
);
self::assertSame(
'{scheme}://domain.tld/users/25',
$this->route->getUrl([], [25])
);
self::assertSame(
'http://domain.tld/users/25',
$this->route->getUrl(['http'], ['25'])
);
}
public function testOptions() : void
{
self::assertSame([], $this->route->getOptions());
self::assertInstanceOf(Route::class, $this->route->setOptions(['foo' => 'bar']));
self::assertSame(['foo' => 'bar'], $this->route->getOptions());
}
public function testName() : void
{
self::assertNull($this->route->getName());
self::assertInstanceOf(Route::class, $this->route->setName('users.show'));
self::assertSame('users.show', $this->route->getName());
}
public function testPath() : void
{
self::assertSame('/users/{int}', $this->route->getPath());
self::assertSame('/users/10', $this->route->getPath(10)); // @phpstan-ignore-line
self::assertInstanceOf(Route::class, $this->route->setPath('/u/{uuid}/show/{int}'));
self::assertSame('/u/{uuid}/show/{int}', $this->route->getPath());
self::assertSame(
'/u/123e4567-e89b-12d3-a456-42661417400a/show/10',
// @phpstan-ignore-next-line
$this->route->getPath('123e4567-e89b-12d3-a456-42661417400a', 10)
);
}
public function testAction() : void
{
self::assertInstanceOf(\Closure::class, $this->route->getAction());
$action = static function () : void {
};
self::assertInstanceOf(Route::class, $this->route->setAction($action));
self::assertSame($action, $this->route->getAction());
self::assertInstanceOf(Route::class, $this->route->setAction('\Users::show/0'));
self::assertSame('Users::show/0', $this->route->getAction());
}
public function testActionArguments() : void
{
self::assertSame([], $this->route->getActionArguments());
self::assertInstanceOf(
Route::class,
$this->route->setActionArguments([
0 => '10',
2 => 'hello-world',
1 => 'foo',
])
);
self::assertSame([
0 => '10',
1 => 'foo',
2 => 'hello-world',
], $this->route->getActionArguments());
}
public function testActionArgumentsWithRandomValues() : void
{
$this->route->setAction(
'\Tests\Routing\Support\WithRouteActions::index/abc/$1/$0/cde'
)->setActionArguments([
'zero',
'one',
]);
$response = $this->route->run();
self::assertSame('abc, one, zero, cde', $response->getBody());
}
protected function assertsForRunWithAction(Route $route) : void
{
self::assertInstanceOf(Route::class, $route->setActionArguments(['foo', 'bar']));
self::assertInstanceOf(Response::class, $route->run());
self::assertSame('foo, bar', $this->response->getBody());
$this->response->setBody('');
self::assertInstanceOf(Route::class, $route->setActionArguments(['param1', 'param2']));
self::assertInstanceOf(Response::class, $route->run('construct1', 'construct2'));
self::assertSame('param1, param2, construct1, construct2', $this->response->getBody());
}
public function testRunWithClosureAsAction() : void
{
$this->assertsForRunWithAction($this->route);
}
public function testRunWithStringAsAction() : void
{
$route = new Route(
$this->router,
'http://domain.tld',
'/',
'\Tests\Routing\Support\WithRouteActions::index/$0/$1'
);
$this->assertsForRunWithAction($route);
}
public function testRunWithStringAsActionWithAsteriskWildcard() : void
{
$route = new Route(
$this->router,
'http://domain.tld',
'/',
'\Tests\Routing\Support\WithRouteActions::index/*'
);
$this->assertsForRunWithAction($route);
}
public function testRunWithClassNotExists() : void
{
$route = new Route($this->router, 'http://domain.tld', '/', 'UnknownClass');
$this->expectException(RoutingException::class);
$this->expectExceptionMessage('Class does not exist: UnknownClass');
$route->run();
}
public function testRunWithClassNotInstanceOfRouteActions() : void
{
$route = new Route($this->router, 'http://domain.tld', '/', WithoutRouteActions::class);
$this->expectException(RoutingException::class);
$this->expectExceptionMessage(
'Class ' . WithoutRouteActions::class . ' is not an instance of ' . RouteActions::class
);
$route->run();
}
public function testRunWithClassActionMethodNotExists() : void
{
$route = new Route(
$this->router,
'http://domain.tld',
'/',
'\Tests\Routing\Support\WithRouteActions::foo'
);
$this->expectException(RoutingException::class);
$this->expectExceptionMessage(
'Class action method does not exist: Tests\Routing\Support\WithRouteActions::foo'
);
$route->run();
}
public function testRunWithActionArgumentAsteriskNotAlone() : void
{
$route = new Route(
$this->router,
'http://domain.tld',
'/',
'\Tests\Routing\Support\WithRouteActions::foo/$0/*'
);
$route->setActionArguments(['arg1', 'arg2']);
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(
'Action arguments can only contain an asterisk wildcard and must be passed alone, on unnamed route'
);
$route->run();
}
public function testRunWithActionArgumentIsNotNumeric() : void
{
$route = new Route(
$this->router,
'http://domain.tld',
'/',
'\Tests\Routing\Support\WithRouteActions::foo/$0/$a'
);
$route->setActionArguments(['arg1', 'arg2']);
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(
'Invalid action argument: $a, on unnamed route'
);
$route->run();
}
public function testRunWithUndefinedActionArgument() : void
{
$route = new Route(
$this->router,
'http://domain.tld',
'/',
'\Tests\Routing\Support\WithRouteActions::foo/$0/$1/$2'
);
$route->setActionArguments(['arg1', 'arg2']);
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(
'Undefined action argument: $2, on unnamed route'
);
$route->run();
}
public function testResponseBodyPartWithNull() : void
{
$route = new Route(
$this->router,
'http://domain.tld',
'/',
static function () {
return null;
}
);
self::assertInstanceOf(Response::class, $route->run());
self::assertSame('', $this->response->getBody());
}
public function testResponseBodyPartWithResponse() : void
{
$route = new Route(
$this->router,
'http://domain.tld',
'/',
static function ($params, $response) {
return $response;
}
);
self::assertInstanceOf(Response::class, $route->run($this->response));
self::assertSame('', $this->response->getBody());
}
public function testResponseBodyPartWithScalar() : void
{
$route = new Route(
$this->router,
'http://domain.tld',
'/',
static function () {
return 1.5;
}
);
self::assertInstanceOf(Response::class, $route->run());
self::assertSame('1.5', $this->response->getBody());
}
public function testResponseBodyPartWithStringable() : void
{
$route = new Route(
$this->router,
'http://domain.tld',
'/',
static function () {
return new class() {
public function __toString() : string
{
return '__toString';
}
};
}
);
self::assertInstanceOf(Response::class, $route->run());
self::assertSame('__toString', $this->response->getBody());
}
public function testResponseBodyPartWithJsonSerializable() : void
{
$data = ['id' => 1, 'name' => 'Natan'];
$route = new Route(
$this->router,
'http://domain.tld',
'/',
static function () use ($data) {
return $data;
}
);
self::assertInstanceOf(Response::class, $route->run());
self::assertSame(\json_encode($data), $this->response->getBody());
}
public function testResponseBodyPartWithInvalidResult() : void
{
$route = new Route(
$this->router,
'http://domain.tld',
'/',
static function () {
return new \DateTime();
}
);
$route->setName('result-error');
$this->expectException(RoutingException::class);
$this->expectExceptionMessage(
"Invalid action return type 'DateTime', on named route 'result-error'"
);
$route->run();
}
public function testScalarTypesCoercion() : void
{
$route = new Route(
$this->router,
'http://domain.tld',
'/',
WithRouteActions::class . '::noStrictTypes/*'
);
$route->setActionArguments(['1.1', '1.1', '1.1', '1.1']);
// We will suppress the error issued by calling the RouteActions class
// and the action method `$class->{$method}(...$arguments)` within
// Route::run().
self::assertSame(
'{"bool":true,"float":1.1,"int":1,"string":"1.1"}',
@$route->run()->getBody()
);
self::assertSame(
'Implicit conversion from float-string "1.1" to int loses precision',
\error_get_last()['message']
);
}
public function testJsonSerialize() : void
{
self::assertSame(
'"{scheme}:\/\/domain.tld\/users\/{int}"',
\json_encode($this->route)
);
}
}