Completed
Branch master (5571d6)
by Filipe
01:34
created

RouterMiddlewareSpec   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 90
Duplicated Lines 47.78 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 6
dl 43
loc 90
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 Psr\Http\Server\MiddlewareInterface;
16
use Psr\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
    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
    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
    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
}
117