1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of coisa/http. |
4
|
|
|
* |
5
|
|
|
* (c) Felipe Sayão Lobato Abreu <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the license that is bundled |
8
|
|
|
* with this source code in the file LICENSE. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace CoiSA\Http\Test\Handler; |
12
|
|
|
|
13
|
|
|
use CoiSA\Http\Handler\RouterHandler; |
14
|
|
|
use CoiSA\Http\Middleware\PregMatchRequestTargetMiddleware; |
15
|
|
|
use Fig\Http\Message\RequestMethodInterface; |
16
|
|
|
use Fig\Http\Message\StatusCodeInterface; |
17
|
|
|
use PHPUnit\Framework\TestCase; |
18
|
|
|
use Prophecy\Argument; |
19
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
20
|
|
|
use Psr\Http\Message\ResponseInterface; |
21
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
22
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class RouterHandlerTest |
26
|
|
|
* |
27
|
|
|
* @package CoiSA\Http\Test\Handler |
28
|
|
|
*/ |
29
|
|
|
final class RouterHandlerTest extends TestCase |
30
|
|
|
{ |
31
|
|
|
/** @var RouterHandler */ |
32
|
|
|
private $handler; |
33
|
|
|
|
34
|
|
|
/** @var ObjectProphecy|RequestHandlerInterface */ |
35
|
|
|
private $notFoundHandler; |
36
|
|
|
|
37
|
|
|
/** @var ObjectProphecy|RequestHandlerInterface */ |
38
|
|
|
private $routeHandler; |
39
|
|
|
|
40
|
|
|
/** @var ObjectProphecy|ServerRequestInterface */ |
41
|
|
|
private $serverRequest; |
42
|
|
|
|
43
|
|
|
/** @var ObjectProphecy|ResponseInterface */ |
44
|
|
|
private $response; |
45
|
|
|
|
46
|
|
|
public function setUp(): void |
47
|
|
|
{ |
48
|
|
|
$this->notFoundHandler = $this->prophesize(RequestHandlerInterface::class); |
49
|
|
|
$this->routeHandler = $this->prophesize(RequestHandlerInterface::class); |
50
|
|
|
$this->serverRequest = $this->prophesize(ServerRequestInterface::class); |
51
|
|
|
$this->response = $this->prophesize(ResponseInterface::class); |
52
|
|
|
$this->handler = new RouterHandler($this->notFoundHandler->reveal()); |
53
|
|
|
|
54
|
|
|
$routeResponse = $this->prophesize(ResponseInterface::class); |
55
|
|
|
|
56
|
|
|
$this->notFoundHandler->handle($this->serverRequest->reveal())->will([$this->response, 'reveal']); |
57
|
|
|
$this->response->withStatus(StatusCodeInterface::STATUS_NOT_FOUND)->will([$this->response, 'reveal']); |
58
|
|
|
|
59
|
|
|
$this->routeHandler->handle($this->serverRequest->reveal())->will([$routeResponse, 'reveal']); |
60
|
|
|
|
61
|
|
|
$this->serverRequest->getMethod()->willReturn(RequestMethodInterface::METHOD_GET); |
62
|
|
|
$this->serverRequest->getRequestTarget()->willReturn('/users/(?<id>\d+)'); |
63
|
|
|
$this->serverRequest->withAttribute(PregMatchRequestTargetMiddleware::class, Argument::type('array'))->will([$this->serverRequest, 'reveal']); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testEmptyRouterReturnNotFoundHandlerResponse(): void |
67
|
|
|
{ |
68
|
|
|
$response = $this->handler->handle($this->serverRequest->reveal()); |
69
|
|
|
$this->assertSame($this->response->reveal(), $response); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testNotMatchedRouteReturnNotFoundHandlerResponse(): void |
73
|
|
|
{ |
74
|
|
|
$this->handler->addRoute( |
75
|
|
|
RequestMethodInterface::METHOD_GET, |
76
|
|
|
\uniqid('test', true), |
77
|
|
|
$this->routeHandler->reveal() |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
$response = $this->handler->handle($this->serverRequest->reveal()); |
81
|
|
|
$this->assertSame($this->response->reveal(), $response); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testMatchedRouteReturnNewResponse(): void |
85
|
|
|
{ |
86
|
|
|
$this->markTestIncomplete(); |
87
|
|
|
|
88
|
|
|
$notFoundResponse = $this->response->reveal(); |
89
|
|
|
|
90
|
|
|
$this->handler->addRoute( |
91
|
|
|
RequestMethodInterface::METHOD_GET, |
92
|
|
|
'/users/1', |
93
|
|
|
$this->routeHandler->reveal() |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
$response = $this->handler->handle($this->serverRequest->reveal()); |
97
|
|
|
$this->assertNotSame($notFoundResponse, $response); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|