1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Flight Routing. |
5
|
|
|
* |
6
|
|
|
* PHP version 8.0 and above required |
7
|
|
|
* |
8
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
9
|
|
|
* @copyright 2019 Divine Niiquaye Ibok (https://divinenii.com/) |
10
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
11
|
|
|
* |
12
|
|
|
* For the full copyright and license information, please view the LICENSE |
13
|
|
|
* file that was distributed with this source code. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
use Flight\Routing\Exceptions\InvalidControllerException; |
17
|
|
|
use Flight\Routing\Handlers\{CallbackHandler, FileHandler, ResourceHandler, RouteHandler, RouteInvoker}; |
18
|
|
|
use Flight\Routing\RouteCollection; |
19
|
|
|
use Flight\Routing\Router; |
20
|
|
|
use Flight\Routing\Tests\Fixtures; |
21
|
|
|
use Nyholm\Psr7\Factory\Psr17Factory; |
22
|
|
|
use Nyholm\Psr7\Response; |
23
|
|
|
use Nyholm\Psr7\ServerRequest; |
24
|
|
|
use PHPUnit\Framework as t; |
25
|
|
|
use Psr\Container\ContainerInterface; |
26
|
|
|
use Psr\Http\Message\ResponseInterface; |
27
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
28
|
|
|
|
29
|
|
|
test('if route callback handler will return a response', function (): void { |
30
|
|
|
$callback = new CallbackHandler(function (ServerRequestInterface $req): ResponseInterface { |
31
|
|
|
($res = new Response())->getBody()->write($req->getMethod()); |
32
|
|
|
|
33
|
|
|
return $res; |
34
|
|
|
}); |
35
|
|
|
t\assertSame('GET', (string) $callback->handle(new ServerRequest('GET', '/hello'))->getBody()); |
|
|
|
|
36
|
|
|
}); |
37
|
|
|
|
38
|
|
|
test('if route resource handler does not contain valid data', function (): void { |
39
|
|
|
new ResourceHandler(new ResourceHandler('phpinfo')); |
40
|
|
|
})->throws( |
41
|
|
|
InvalidControllerException::class, |
42
|
|
|
'Expected a class string or class object, got a type of "callable" instead' |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
test('if route handler does return a plain valid response', function (): void { |
46
|
|
|
$collection = new RouteCollection(); |
47
|
|
|
$collection->add('/a', handler: fn (): ResponseInterface => new Response()); |
48
|
|
|
$collection->add('/b', handler: fn (): string => 'Hello World'); |
49
|
|
|
$handler = new RouteHandler(new Psr17Factory()); |
50
|
|
|
|
51
|
|
|
$req = (new ServerRequest('GET', '/a'))->withAttribute(Router::class, $collection->offsetGet(0)); |
52
|
|
|
t\assertInstanceOf(ResponseInterface::class, $res = $handler->handle($req)); |
|
|
|
|
53
|
|
|
t\assertSame('text/plain; charset=utf-8', $res->getHeaderLine('Content-Type')); |
|
|
|
|
54
|
|
|
t\assertSame(204, $res->getStatusCode()); |
55
|
|
|
|
56
|
|
|
$req = (new ServerRequest('GET', '/b'))->withAttribute(Router::class, $collection->offsetGet(1)); |
57
|
|
|
t\assertInstanceOf(ResponseInterface::class, $res = $handler->handle($req)); |
58
|
|
|
t\assertSame('text/plain', $res->getHeaderLine('Content-Type')); |
59
|
|
|
t\assertSame(200, $res->getStatusCode()); |
60
|
|
|
|
61
|
|
|
$req = (new ServerRequest('GET', '/c'))->withAttribute(Router::class, []); |
62
|
|
|
t\assertInstanceOf(ResponseInterface::class, $res = $handler->handle($req)); |
63
|
|
|
t\assertSame('text/plain; charset=utf-8', $res->getHeaderLine('Content-Type')); |
64
|
|
|
t\assertSame(204, $res->getStatusCode()); |
65
|
|
|
}); |
66
|
|
|
|
67
|
|
|
test('if route handler does return a html valid response', function (): void { |
68
|
|
|
$handler = new RouteHandler(new Psr17Factory()); |
69
|
|
|
$req = (new ServerRequest('GET', '/a'))->withAttribute(Router::class, ['handler' => function (): string { |
70
|
|
|
return <<<'HTML' |
71
|
|
|
<html lang="en"> |
72
|
|
|
<head><meta charset="UTF-8"></head> |
73
|
|
|
<body><h1>Hello World</h1></body> |
74
|
|
|
</html> |
75
|
|
|
HTML; |
76
|
|
|
}]); |
77
|
|
|
t\assertInstanceOf(ResponseInterface::class, $res = $handler->handle($req)); |
|
|
|
|
78
|
|
|
t\assertSame('text/html', $res->getHeaderLine('Content-Type')); |
|
|
|
|
79
|
|
|
t\assertSame(200, $res->getStatusCode()); |
80
|
|
|
}); |
81
|
|
|
|
82
|
|
|
test('if route handler does return a xml valid response', function (): void { |
83
|
|
|
$handler = new RouteHandler(new Psr17Factory()); |
84
|
|
|
$req = (new ServerRequest('GET', '/a'))->withAttribute(Router::class, ['handler' => function (): string { |
85
|
|
|
return <<<'XML' |
86
|
|
|
<?xml version="1.0" encoding="UTF-8"?> |
87
|
|
|
<project version="4"> |
88
|
|
|
<component name="PHPUnit"> |
89
|
|
|
<option name="directories"> |
90
|
|
|
<list><option value="$PROJECT_DIR$/tests" /></list> |
91
|
|
|
</option> |
92
|
|
|
</component> |
93
|
|
|
</project> |
94
|
|
|
XML; |
95
|
|
|
}]); |
96
|
|
|
t\assertInstanceOf(ResponseInterface::class, $res = $handler->handle($req)); |
|
|
|
|
97
|
|
|
t\assertSame('text/xml; charset=utf-8', $res->getHeaderLine('Content-Type')); |
|
|
|
|
98
|
|
|
t\assertSame(200, $res->getStatusCode()); |
99
|
|
|
}); |
100
|
|
|
test('if route handler does return a rss valid response', function (): void { |
101
|
|
|
$handler = new RouteHandler(new Psr17Factory()); |
102
|
|
|
$req = (new ServerRequest('GET', '/a'))->withAttribute(Router::class, ['handler' => function (): string { |
103
|
|
|
return <<<'XML' |
104
|
|
|
<?xml version="1.0" encoding="UTF-8"?> |
105
|
|
|
<rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"></rss> |
106
|
|
|
XML; |
107
|
|
|
}]); |
108
|
|
|
t\assertInstanceOf(ResponseInterface::class, $res = $handler->handle($req)); |
|
|
|
|
109
|
|
|
t\assertSame('application/rss+xml; charset=utf-8', $res->getHeaderLine('Content-Type')); |
|
|
|
|
110
|
|
|
t\assertSame(200, $res->getStatusCode()); |
111
|
|
|
}); |
112
|
|
|
|
113
|
|
|
test('if route handler does return a svg valid response', function (): void { |
114
|
|
|
$handler = new RouteHandler(new Psr17Factory()); |
115
|
|
|
$req = (new ServerRequest('GET', '/a'))->withAttribute(Router::class, [ |
116
|
|
|
'handler' => fn (): string => '<?xml version="1.0" encoding="UTF-8"?><svg><metadata>Hello World</metadata></svg>', |
117
|
|
|
]); |
118
|
|
|
t\assertInstanceOf(ResponseInterface::class, $res = $handler->handle($req)); |
|
|
|
|
119
|
|
|
t\assertSame('image/svg+xml', $res->getHeaderLine('Content-Type')); |
|
|
|
|
120
|
|
|
t\assertSame(200, $res->getStatusCode()); |
121
|
|
|
}); |
122
|
|
|
|
123
|
|
|
test('if route handler does return a csv valid response', function (): void { |
124
|
|
|
$handler = new RouteHandler(new Psr17Factory()); |
125
|
|
|
$req = (new ServerRequest('GET', '/a'))->withAttribute(Router::class, ['handler' => function (): string { |
126
|
|
|
return <<<'CSV' |
127
|
|
|
a,b,c |
128
|
|
|
d,e,f |
129
|
|
|
g,h,i |
130
|
|
|
|
131
|
|
|
CSV; |
132
|
|
|
}]); |
133
|
|
|
t\assertInstanceOf(ResponseInterface::class, $res = $handler->handle($req)); |
|
|
|
|
134
|
|
|
t\assertTrue(\in_array($res->getHeaderLine('Content-Type'), ['text/csv', 'application/csv'], true)); |
|
|
|
|
135
|
|
|
t\assertSame(200, $res->getStatusCode()); |
|
|
|
|
136
|
|
|
}); |
137
|
|
|
|
138
|
|
|
test('if route handler does return a json valid response', function (): void { |
139
|
|
|
$collection = new RouteCollection(); |
140
|
|
|
$collection->add('/a', handler: fn () => \json_encode(['Hello', 'World', 'Cool' => 'Yeah', 2022])); |
141
|
|
|
$handler = new RouteHandler(new Psr17Factory()); |
142
|
|
|
$req = (new ServerRequest('GET', '/a'))->withAttribute(Router::class, $collection->offsetGet(0)); |
143
|
|
|
t\assertInstanceOf(ResponseInterface::class, $res = $handler->handle($req)); |
|
|
|
|
144
|
|
|
t\assertSame('application/json', $res->getHeaderLine('Content-Type')); |
|
|
|
|
145
|
|
|
t\assertSame(200, $res->getStatusCode()); |
146
|
|
|
}); |
147
|
|
|
|
148
|
|
|
test('if route handler cannot detect content type', function (): void { |
149
|
|
|
$handler = new RouteHandler(new Psr17Factory()); |
150
|
|
|
$req = (new ServerRequest('GET', '/a'))->withAttribute(Router::class, ['handler' => function () { |
151
|
|
|
return <<<'CSS' |
152
|
|
|
body { |
153
|
|
|
padding-top: 10px; |
154
|
|
|
} |
155
|
|
|
svg text { |
156
|
|
|
font-family: "Lucida Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif; |
157
|
|
|
font-size: 11px; |
158
|
|
|
color: #666; |
159
|
|
|
fill: #666; |
160
|
|
|
} |
161
|
|
|
CSS; |
162
|
|
|
}]); |
163
|
|
|
t\assertInstanceOf(ResponseInterface::class, $res = $handler->handle($req)); |
|
|
|
|
164
|
|
|
t\assertSame('text/plain', $res->getHeaderLine('Content-Type')); |
|
|
|
|
165
|
|
|
t\assertSame(200, $res->getStatusCode()); |
166
|
|
|
}); |
167
|
|
|
|
168
|
|
|
test('if route handler does return a echoed xml valid response', function (): void { |
169
|
|
|
$handler = new RouteHandler(new Psr17Factory()); |
170
|
|
|
$req = (new ServerRequest('GET', '/a'))->withAttribute(Router::class, ['handler' => function (): void { |
171
|
|
|
echo <<<'XML' |
172
|
|
|
<?xml version="1.0" encoding="UTF-8"?> |
173
|
|
|
<rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"></rss> |
174
|
|
|
XML; |
175
|
|
|
}]); |
176
|
|
|
t\assertInstanceOf(ResponseInterface::class, $res = $handler->handle($req)); |
|
|
|
|
177
|
|
|
t\assertSame('application/rss+xml; charset=utf-8', $res->getHeaderLine('Content-Type')); |
|
|
|
|
178
|
|
|
t\assertSame(200, $res->getStatusCode()); |
179
|
|
|
}); |
180
|
|
|
|
181
|
|
|
test('if route handler can handle exception from route', function (): void { |
182
|
|
|
$handler = new RouteHandler(new Psr17Factory()); |
183
|
|
|
$req = (new ServerRequest('GET', '/a'))->withAttribute(Router::class, ['handler' => function (): void { |
184
|
|
|
throw new \RuntimeException('Testing error'); |
185
|
|
|
}]); |
186
|
|
|
$handler->handle($req); |
187
|
|
|
$this->fail('Expected an invalid controller exception as route handler is invalid'); |
|
|
|
|
188
|
|
|
})->throws(\RuntimeException::class, 'Testing error'); |
189
|
|
|
|
190
|
|
|
test('if route handler does return an invalid response', function (): void { |
191
|
|
|
$handler = new RouteHandler(new Psr17Factory()); |
192
|
|
|
$req = (new ServerRequest('GET', '/a'))->withAttribute(Router::class, ['handler' => fn () => new \finfo()]); |
193
|
|
|
$handler->handle($req); |
194
|
|
|
$this->fail('Expected an invalid controller exception as route handler is invalid'); |
|
|
|
|
195
|
|
|
})->throws( |
196
|
|
|
InvalidControllerException::class, |
197
|
|
|
'The route handler\'s content is not a valid PSR7 response body stream.' |
198
|
|
|
); |
199
|
|
|
|
200
|
|
|
test('if route handler is a request handler type', function (): void { |
201
|
|
|
$handler = new RouteHandler(new Psr17Factory()); |
202
|
|
|
$collection = new RouteCollection(); |
203
|
|
|
$collection->add('/a', handler: new CallbackHandler(function (ServerRequestInterface $req): ResponseInterface { |
204
|
|
|
$res = new Response(); |
205
|
|
|
$method = $req->getMethod(); |
206
|
|
|
$res->getBody()->write(<<<HTML |
207
|
|
|
<html lang="en"> |
208
|
|
|
<head><meta charset="UTF-8"></head> |
209
|
|
|
<body><h1>Hello World in {$method} REQUEST</h1></body> |
210
|
|
|
</html> |
211
|
|
|
HTML); |
212
|
|
|
|
213
|
|
|
return $res; |
214
|
|
|
})); |
215
|
|
|
$collection->add('/b', handler: Fixtures\BlankRequestHandler::class); |
216
|
|
|
|
217
|
|
|
$req = (new ServerRequest('GET', '/a'))->withAttribute(Router::class, $collection->offsetGet(0)); |
218
|
|
|
t\assertInstanceOf(ResponseInterface::class, $res = $handler->handle($req)); |
|
|
|
|
219
|
|
|
t\assertStringContainsString('GET REQUEST', (string) $res->getBody()); |
|
|
|
|
220
|
|
|
t\assertSame('text/html', $res->getHeaderLine('Content-Type')); |
|
|
|
|
221
|
|
|
t\assertSame(200, $res->getStatusCode()); |
222
|
|
|
|
223
|
|
|
$req = (new ServerRequest('GET', '/b'))->withAttribute(Router::class, $collection->offsetGet(1)); |
224
|
|
|
t\assertInstanceOf(ResponseInterface::class, $res = $handler->handle($req)); |
225
|
|
|
t\assertEmpty((string) $res->getBody()); |
|
|
|
|
226
|
|
|
t\assertSame('text/plain; charset=utf-8', $res->getHeaderLine('Content-Type')); |
227
|
|
|
t\assertSame(204, $res->getStatusCode()); |
228
|
|
|
}); |
229
|
|
|
|
230
|
|
|
test('if route handler is an array like type', function (): void { |
231
|
|
|
$handler = new RouteHandler(new Psr17Factory()); |
232
|
|
|
$collection = new RouteCollection(); |
233
|
|
|
$collection->add('/a', handler: [Fixtures\BlankRequestHandler::class, 'handle']); |
234
|
|
|
$collection->add('/b', handler: [$h = new Fixtures\BlankRequestHandler(), 'handle']); |
235
|
|
|
$collection->add('/c', handler: fn (): array => [1, 2, 3, 4, 5]); |
236
|
|
|
$collection->add('/d', handler: [1, 2, 3, 'Error']); |
237
|
|
|
|
238
|
|
|
$req = (new ServerRequest('GET', '/a'))->withAttribute(Router::class, $collection->offsetGet(0)); |
239
|
|
|
t\assertInstanceOf(ResponseInterface::class, $res = $handler->handle($req)); |
|
|
|
|
240
|
|
|
t\assertEmpty((string) $res->getBody()); |
|
|
|
|
241
|
|
|
t\assertSame('text/plain; charset=utf-8', $res->getHeaderLine('Content-Type')); |
|
|
|
|
242
|
|
|
t\assertSame(204, $res->getStatusCode()); |
243
|
|
|
|
244
|
|
|
$req = (new ServerRequest('GET', '/b'))->withAttribute(Router::class, $collection->offsetGet(1)); |
245
|
|
|
t\assertInstanceOf(ResponseInterface::class, $res = $handler->handle($req)); |
246
|
|
|
t\assertEmpty((string) $res->getBody()); |
247
|
|
|
t\assertSame('text/plain; charset=utf-8', $res->getHeaderLine('Content-Type')); |
248
|
|
|
t\assertSame(204, $res->getStatusCode()); |
249
|
|
|
t\assertTrue($h->isDone()); |
|
|
|
|
250
|
|
|
|
251
|
|
|
$req = (new ServerRequest('GET', '/c'))->withAttribute(Router::class, $collection->offsetGet(2)); |
252
|
|
|
t\assertInstanceOf(ResponseInterface::class, $res = $handler->handle($req)); |
253
|
|
|
t\assertSame('[1,2,3,4,5]', (string) $res->getBody()); |
254
|
|
|
t\assertSame('application/json', $res->getHeaderLine('Content-Type')); |
255
|
|
|
t\assertSame(200, $res->getStatusCode()); |
256
|
|
|
|
257
|
|
|
$req = (new ServerRequest('GET', '/d'))->withAttribute(Router::class, $collection->offsetGet(3)); |
258
|
|
|
$handler->handle($req); |
259
|
|
|
$this->fail('Expected an invalid controller exception as route handler is invalid'); |
|
|
|
|
260
|
|
|
})->throws(InvalidControllerException::class, 'Route has an invalid handler type of "array".'); |
261
|
|
|
|
262
|
|
|
test('if route handler is a stringable type', function (): void { |
263
|
|
|
$handler = new RouteHandler(new Psr17Factory()); |
264
|
|
|
$req = (new ServerRequest('GET', '/a'))->withAttribute(Router::class, ['handler' => new \RuntimeException()]); |
265
|
|
|
t\assertInstanceOf(ResponseInterface::class, $res = $handler->handle($req)); |
|
|
|
|
266
|
|
|
t\assertSame('RuntimeException', \substr((string) $res->getBody(), 0, 16)); |
|
|
|
|
267
|
|
|
t\assertSame('text/plain', $res->getHeaderLine('Content-Type')); |
268
|
|
|
t\assertSame(200, $res->getStatusCode()); |
269
|
|
|
}); |
270
|
|
|
|
271
|
|
|
test('if route handler is a file handler type', function (): void { |
272
|
|
|
$handler = new RouteHandler(new Psr17Factory()); |
273
|
|
|
$collection = new RouteCollection(); |
274
|
|
|
$collection->add('/a', handler: new FileHandler(__DIR__.'/../tests/Fixtures/template.html')); |
275
|
|
|
$collection->add('/b', handler: fn () => new FileHandler(__DIR__.'/../tests/Fixtures/style.css')); |
276
|
|
|
|
277
|
|
|
$req = (new ServerRequest('GET', '/a'))->withAttribute(Router::class, $collection->offsetGet(0)); |
278
|
|
|
t\assertInstanceOf(ResponseInterface::class, $res = $handler->handle($req)); |
|
|
|
|
279
|
|
|
t\assertSame('text/html', $res->getHeaderLine('Content-Type')); |
|
|
|
|
280
|
|
|
t\assertSame(200, $res->getStatusCode()); |
281
|
|
|
|
282
|
|
|
$req = (new ServerRequest('GET', '/b'))->withAttribute(Router::class, $collection->offsetGet(1)); |
283
|
|
|
t\assertInstanceOf(ResponseInterface::class, $res = $handler->handle($req)); |
284
|
|
|
t\assertSame('text/css', $res->getHeaderLine('Content-Type')); |
285
|
|
|
t\assertSame(200, $res->getStatusCode()); |
286
|
|
|
|
287
|
|
|
$req = (new ServerRequest('GET', '/b'))->withAttribute(Router::class, ['handler' => new FileHandler('hello')]); |
288
|
|
|
$handler->handle($req); |
289
|
|
|
$this->fail('Expected an invalid controller exception as route handler is invalid'); |
|
|
|
|
290
|
|
|
})->throws(InvalidControllerException::class, 'Failed to fetch contents from file "hello"'); |
291
|
|
|
|
292
|
|
|
test('if route invoker can support a number of parameters binding', function (): void { |
293
|
|
|
$handler = new RouteInvoker(); |
294
|
|
|
$container = new RouteInvoker(new class () implements ContainerInterface { |
295
|
|
|
public function get(string $id) |
296
|
|
|
{ |
297
|
|
|
return match ($id) { |
298
|
|
|
\Countable::class => new \ArrayObject(), |
299
|
|
|
\Iterator::class => new \ArrayIterator([1, 2, 3]), |
300
|
|
|
'func' => fn (int $a): ContainerInterface => $this, |
|
|
|
|
301
|
|
|
}; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
public function has(string $id): bool |
305
|
|
|
{ |
306
|
|
|
return \Countable::class === $id || \Iterator::class === $id || 'func' === $id; |
307
|
|
|
} |
308
|
|
|
}); |
309
|
|
|
$h0 = $handler(fn ($var): bool => true, []); |
|
|
|
|
310
|
|
|
$h1 = $handler(fn (string $name = '', array $values = []): bool => true, []); |
|
|
|
|
311
|
|
|
$h2 = $handler(fn (?string $a, string $b = 'iiv'): string => $a.$b, []); |
312
|
|
|
$h3 = $handler(fn (string $a): array => \unpack('C*', $a), ['a' => '🚀']); |
313
|
|
|
$h4 = $handler(fn (string $a, string $b, string $c): string => $a.$b.$c, ['a&b' => 'i', 'c' => 'v']); |
314
|
|
|
$h5 = $handler(fn (int|string $a, int|bool $b = 3): string => $a.$b, ['a' => 1]); |
315
|
|
|
$h6 = $handler(fn (int|string|null $a): string|bool => null == $a ? '13' : false, []); |
|
|
|
|
316
|
|
|
$h7 = $handler(fn (string|RouteCollection $a, RouteCollection $b): bool => $a === $b, ['a&b' => new RouteCollection()]); |
317
|
|
|
$h8 = $handler($u = fn (RouteCollection|\Countable $a): bool => $a instanceof \Countable, [RouteCollection::class => new RouteCollection()]); |
318
|
|
|
$h9 = $container(fn (\Iterator $a): bool => [1, 2, 3] === \iterator_to_array($a), []); |
319
|
|
|
$h10 = $container(fn (mixed $a): bool => '🚀' === $a, ['a' => \pack('C*', 0xF0, 0x9F, 0x9A, 0x80)]); |
320
|
|
|
$h11 = $container(fn (?string $a, ?string $b): bool => $a === $b, []); |
321
|
|
|
|
322
|
|
|
t\assertSame($h0, $h1); |
|
|
|
|
323
|
|
|
t\assertSame($h2, $h4); |
324
|
|
|
t\assertSame($h5, $h6); |
325
|
|
|
t\assertSame($h7, $h8); |
326
|
|
|
t\assertSame($h9, $h10); |
327
|
|
|
t\assertSame($h11, $container($u, [])); |
328
|
|
|
t\assertSame([1 => 0xF0, 2 => 0x9F, 3 => 0x9A, 4 => 0x80], $h3); |
329
|
|
|
t\assertSame($container('func', ['a' => 0]), $container->getContainer()); |
330
|
|
|
}); |
331
|
|
|
|
332
|
|
|
test('if route invoker can execute handlers which is prefixed with a \\', function (): void { |
333
|
|
|
$handler = new RouteInvoker(new class () implements ContainerInterface { |
334
|
|
|
public function get(string $id) |
335
|
|
|
{ |
336
|
|
|
return new Fixtures\BlankRequestHandler(); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
public function has(string $id): bool |
340
|
|
|
{ |
341
|
|
|
return Fixtures\BlankRequestHandler::class === $id; |
342
|
|
|
} |
343
|
|
|
}); |
344
|
|
|
t\assertSame('123', $handler('\\debugFormat', ['value' => 123])); |
|
|
|
|
345
|
|
|
t\assertInstanceOf(ResponseInterface::class, $handler( |
|
|
|
|
346
|
|
|
'\\'.Fixtures\BlankRequestHandler::class.'@handle', |
347
|
|
|
[ServerRequestInterface::class => new ServerRequest('GET', '/a')] |
348
|
|
|
)); |
349
|
|
|
}); |
350
|
|
|
|
351
|
|
|
test('if route invoker can parse a resource handler with parameters', function (string $method): void { |
352
|
|
|
$handler = new RouteHandler(new Psr17Factory()); |
353
|
|
|
$resource = new ResourceHandler(new class () { |
354
|
|
|
public function getHandler(string $method): string |
355
|
|
|
{ |
356
|
|
|
return 'I am in a '.$method.' request'; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
public function postHandler(string $method): string |
360
|
|
|
{ |
361
|
|
|
return 'I am in a '.$method.' request'; |
362
|
|
|
} |
363
|
|
|
}, 'handler'); |
364
|
|
|
|
365
|
|
|
$req = (new ServerRequest($method, '/a'))->withAttribute( |
366
|
|
|
Router::class, |
367
|
|
|
['handler' => $resource, 'arguments' => \compact('method')] |
368
|
|
|
); |
369
|
|
|
|
370
|
|
|
if ('PUT' === $method) { |
371
|
|
|
$this->expectExceptionObject(new InvalidControllerException('Method putHandler() for resource route ')); |
|
|
|
|
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
t\assertInstanceOf(ResponseInterface::class, $res = $handler->handle($req)); |
|
|
|
|
375
|
|
|
t\assertSame('I am in a '.$method.' request', (string) $res->getBody()); |
|
|
|
|
376
|
|
|
t\assertSame('text/plain', $res->getHeaderLine('Content-Type')); |
377
|
|
|
t\assertSame(200, $res->getStatusCode()); |
378
|
|
|
})->with(['GET', 'POST', 'PUT']); |
379
|
|
|
|