|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of slick/web_stack package |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace spec\Slick\WebStack\Http\Server; |
|
11
|
|
|
|
|
12
|
|
|
use Aura\Router\Matcher; |
|
13
|
|
|
use Aura\Router\Route; |
|
14
|
|
|
use Aura\Router\RouterContainer; |
|
15
|
|
|
use Interop\Http\Server\MiddlewareInterface; |
|
16
|
|
|
use Interop\Http\Server\RequestHandlerInterface; |
|
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
18
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
19
|
|
|
use Slick\WebStack\Http\Server\RouterMiddleware; |
|
20
|
|
|
use PhpSpec\ObjectBehavior; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* RouterMiddlewareSpec specs |
|
24
|
|
|
* |
|
25
|
|
|
* @package spec\Slick\WebStack\Http\Server |
|
26
|
|
|
*/ |
|
27
|
|
|
class RouterMiddlewareSpec extends ObjectBehavior |
|
28
|
|
|
{ |
|
29
|
|
|
|
|
30
|
|
|
function let( |
|
31
|
|
|
RouterContainer $routerContainer, |
|
32
|
|
|
ServerRequestInterface $request, |
|
33
|
|
|
RequestHandlerInterface $handler, |
|
34
|
|
|
ResponseInterface $response, |
|
35
|
|
|
Matcher $matcher |
|
36
|
|
|
) |
|
37
|
|
|
{ |
|
38
|
|
|
$routerContainer->getMatcher()->willReturn($matcher); |
|
39
|
|
|
$handler->handle($request)->willReturn($response); |
|
40
|
|
|
$this->beConstructedWith($routerContainer); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
function its_a_middleware() |
|
44
|
|
|
{ |
|
45
|
|
|
$this->shouldBeAnInstanceOf(MiddlewareInterface::class); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
function it_is_initializable_with_a_router_container() |
|
49
|
|
|
{ |
|
50
|
|
|
$this->shouldHaveType(RouterMiddleware::class); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
View Code Duplication |
function it_matches_the_request_to_its_routes_container( |
|
|
|
|
|
|
54
|
|
|
ServerRequestInterface $request, |
|
55
|
|
|
RequestHandlerInterface $handler, |
|
56
|
|
|
ResponseInterface $response, |
|
57
|
|
|
Matcher $matcher, |
|
58
|
|
|
Route $route |
|
59
|
|
|
) |
|
60
|
|
|
{ |
|
61
|
|
|
|
|
62
|
|
|
$matcher->match($request)->shouldBeCalled()->willReturn($route); |
|
63
|
|
|
|
|
64
|
|
|
$request->withAttribute('route', $route)->shouldBeCalled()->willReturn($request); |
|
65
|
|
|
|
|
66
|
|
|
$this->process($request, $handler)->shouldBe($response); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
function it_returns_a_405_response_if_method_not_allowed( |
|
70
|
|
|
ServerRequestInterface $request, |
|
71
|
|
|
RequestHandlerInterface $handler, |
|
72
|
|
|
Matcher $matcher |
|
73
|
|
|
) { |
|
74
|
|
|
$matcher->match($request)->willReturn(null); |
|
75
|
|
|
|
|
76
|
|
|
$failRoute = new Route(); |
|
77
|
|
|
$failRoute->allows(['POST']); |
|
78
|
|
|
$failRoute->failedRule('Aura\Router\Rule\Allows'); |
|
79
|
|
|
|
|
80
|
|
|
$matcher->getFailedRoute()->willReturn($failRoute); |
|
81
|
|
|
|
|
82
|
|
|
$response = $this->process($request, $handler); |
|
83
|
|
|
$response->getStatusCode()->shouldBe(405); |
|
84
|
|
|
$response->getHeaderLine('Allow')->shouldBe('POST'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
View Code Duplication |
function it_returns_a_406_response_if_request_not_accepted( |
|
|
|
|
|
|
88
|
|
|
ServerRequestInterface $request, |
|
89
|
|
|
RequestHandlerInterface $handler, |
|
90
|
|
|
Matcher $matcher |
|
91
|
|
|
) { |
|
92
|
|
|
$matcher->match($request)->willReturn(null); |
|
93
|
|
|
|
|
94
|
|
|
$failRoute = new Route(); |
|
95
|
|
|
$failRoute->failedRule('Aura\Router\Rule\Accepts'); |
|
96
|
|
|
$matcher->getFailedRoute()->willReturn($failRoute); |
|
97
|
|
|
|
|
98
|
|
|
$response = $this->process($request, $handler); |
|
99
|
|
|
$response->getStatusCode()->shouldBe(406); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
View Code Duplication |
function it_returns_a_404_response_for_all_other_failed_routes( |
|
|
|
|
|
|
103
|
|
|
ServerRequestInterface $request, |
|
104
|
|
|
RequestHandlerInterface $handler, |
|
105
|
|
|
Matcher $matcher |
|
106
|
|
|
) { |
|
107
|
|
|
$matcher->match($request)->willReturn(null); |
|
108
|
|
|
|
|
109
|
|
|
$failRoute = new Route(); |
|
110
|
|
|
$failRoute->failedRule('other'); |
|
111
|
|
|
$matcher->getFailedRoute()->willReturn($failRoute); |
|
112
|
|
|
|
|
113
|
|
|
$response = $this->process($request, $handler); |
|
114
|
|
|
$response->getStatusCode()->shouldBe(404); |
|
115
|
|
|
} |
|
116
|
|
|
} |
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.