|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace spec\Slick\Mvc\Http; |
|
4
|
|
|
|
|
5
|
|
|
use Aura\Router\Matcher; |
|
6
|
|
|
use Aura\Router\Route; |
|
7
|
|
|
use Aura\Router\RouterContainer; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
10
|
|
|
use Slick\Http\Server\MiddlewareInterface; |
|
11
|
|
|
use Slick\Mvc\Http\RouterMiddleware; |
|
12
|
|
|
use PhpSpec\ObjectBehavior; |
|
13
|
|
|
use Prophecy\Argument; |
|
14
|
|
|
|
|
15
|
|
|
class RouterMiddlewareSpec extends ObjectBehavior |
|
16
|
|
|
{ |
|
17
|
|
|
function let(RouterContainer $routerContainer) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->beConstructedWith($routerContainer); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
function it_is_initializable() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->shouldHaveType(RouterMiddleware::class); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
function its_an_http_middleware() |
|
28
|
|
|
{ |
|
29
|
|
|
$this->shouldBeAnInstanceOf(MiddlewareInterface::class); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
function it_matches_the_request_to_its_routes_container( |
|
33
|
|
|
ServerRequestInterface $request, |
|
34
|
|
|
ResponseInterface $response, |
|
35
|
|
|
RouterContainer $routerContainer, |
|
36
|
|
|
Matcher $matcher, |
|
37
|
|
|
Route $route |
|
38
|
|
|
) |
|
39
|
|
|
{ |
|
40
|
|
|
$routerContainer->getMatcher()->willReturn($matcher); |
|
41
|
|
|
$matcher->match($request)->willReturn($route); |
|
42
|
|
|
$request->withAttribute('route', $route)->willReturn($request); |
|
43
|
|
|
$this->beConstructedWith($routerContainer); |
|
44
|
|
|
$matcher->match($request)->shouldBeCalled(); |
|
45
|
|
|
$this->handle($request, $response)->shouldBe($response); |
|
46
|
|
|
$request->withAttribute('route', $route)->shouldHaveBeenCalled(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
function it_returns_a_405_response_if_method_not_allowed( |
|
50
|
|
|
ServerRequestInterface $request, |
|
51
|
|
|
ResponseInterface $response, |
|
52
|
|
|
RouterContainer $routerContainer, |
|
53
|
|
|
Matcher $matcher |
|
54
|
|
|
) { |
|
55
|
|
|
$routerContainer->getMatcher()->willReturn($matcher); |
|
56
|
|
|
$matcher->match($request)->willReturn(null); |
|
57
|
|
|
$failRoute = new Route(); |
|
58
|
|
|
$failRoute->allows(['POST']); |
|
59
|
|
|
$failRoute->failedRule('Aura\Router\Rule\Allows'); |
|
60
|
|
|
$matcher->getFailedRoute()->willReturn($failRoute); |
|
61
|
|
|
|
|
62
|
|
|
$request->withAttribute('route', null) |
|
63
|
|
|
->willReturn($request); |
|
64
|
|
|
|
|
65
|
|
|
$response->withStatus(405) |
|
66
|
|
|
->shouldBeCalled() |
|
67
|
|
|
->willReturn($response); |
|
68
|
|
|
|
|
69
|
|
|
$response->withHeader('allow', $failRoute->allows) |
|
70
|
|
|
->shouldBeCalled() |
|
71
|
|
|
->willReturn($response); |
|
72
|
|
|
|
|
73
|
|
|
$this->beConstructedWith($routerContainer); |
|
74
|
|
|
$this->handle($request, $response)->shouldBe($response); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
View Code Duplication |
function it_returns_a_406_response_if_request_not_accepted( |
|
|
|
|
|
|
78
|
|
|
ServerRequestInterface $request, |
|
79
|
|
|
ResponseInterface $response, |
|
80
|
|
|
RouterContainer $routerContainer, |
|
81
|
|
|
Matcher $matcher |
|
82
|
|
|
) { |
|
83
|
|
|
$routerContainer->getMatcher()->willReturn($matcher); |
|
84
|
|
|
$matcher->match($request)->willReturn(null); |
|
85
|
|
|
$failRoute = new Route(); |
|
86
|
|
|
$failRoute->failedRule('Aura\Router\Rule\Accepts'); |
|
87
|
|
|
$matcher->getFailedRoute()->willReturn($failRoute); |
|
88
|
|
|
|
|
89
|
|
|
$request->withAttribute('route', null) |
|
90
|
|
|
->willReturn($request); |
|
91
|
|
|
|
|
92
|
|
|
$response->withStatus(406) |
|
93
|
|
|
->shouldBeCalled() |
|
94
|
|
|
->willReturn($response); |
|
95
|
|
|
|
|
96
|
|
|
$this->beConstructedWith($routerContainer); |
|
97
|
|
|
$this->handle($request, $response)->shouldBe($response); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
View Code Duplication |
function it_returns_a_404_response_for_all_other_failed_routes( |
|
|
|
|
|
|
101
|
|
|
ServerRequestInterface $request, |
|
102
|
|
|
ResponseInterface $response, |
|
103
|
|
|
RouterContainer $routerContainer, |
|
104
|
|
|
Matcher $matcher |
|
105
|
|
|
) { |
|
106
|
|
|
$routerContainer->getMatcher()->willReturn($matcher); |
|
107
|
|
|
$matcher->match($request)->willReturn(null); |
|
108
|
|
|
$failRoute = new Route(); |
|
109
|
|
|
$failRoute->failedRule('other'); |
|
110
|
|
|
$matcher->getFailedRoute()->willReturn($failRoute); |
|
111
|
|
|
|
|
112
|
|
|
$request->withAttribute('route', null) |
|
113
|
|
|
->willReturn($request); |
|
114
|
|
|
|
|
115
|
|
|
$response->withStatus(404) |
|
116
|
|
|
->shouldBeCalled() |
|
117
|
|
|
->willReturn($response); |
|
118
|
|
|
|
|
119
|
|
|
$this->beConstructedWith($routerContainer); |
|
120
|
|
|
$this->handle($request, $response)->shouldBe($response); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.