Completed
Push — master ( c0f250...321e24 )
by Filipe
15:10
created

DispatcherMiddlewareSpec::let()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 21
nc 1
nop 9

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\Route;
13
use Interop\Http\Server\MiddlewareInterface;
14
use Interop\Http\Server\RequestHandlerInterface;
15
use Prophecy\Argument;
16
use Psr\Http\Message\ResponseInterface;
17
use Psr\Http\Message\ServerRequestInterface;
18
use Slick\WebStack\Controller\ContextCreator;
19
use Slick\WebStack\Controller\ControllerContextInterface;
20
use Slick\WebStack\Dispatcher\ControllerDispatch;
21
use Slick\WebStack\Dispatcher\ControllerDispatchInflectorInterface;
22
use Slick\WebStack\Dispatcher\ControllerInvokerInterface;
23
use Slick\WebStack\Exception\BadHttpStackConfigurationException;
24
use Slick\WebStack\Exception\MissingResponseException;
25
use Slick\WebStack\Http\Server\DispatcherMiddleware;
26
use PhpSpec\ObjectBehavior;
27
28
/**
29
 * DispatcherMiddlewareSpec specs
30
 *
31
 * @package spec\Slick\WebStack\Http\Server
32
 */
33
class DispatcherMiddlewareSpec extends ObjectBehavior
34
{
35
36
    private $controllerData = ['foo' => 'bar'];
37
    private $assignData = ['bar' => 'baz', 'foo' => 'bar'];
38
39
    function let(
40
        ServerRequestInterface $request,
41
        Route $route,
42
        RequestHandlerInterface $handler,
43
        ResponseInterface $response,
44
        ControllerDispatchInflectorInterface $inflector,
45
        ControllerInvokerInterface $invoker,
46
        ControllerDispatch $controllerDispatch,
47
        ContextCreator $contextCreator,
48
        ControllerContextInterface $context
49
    )
50
    {
51
        $request->getAttribute('route', false)
52
            ->willReturn($route);
53
        $request->withAttribute('viewData', Argument::any())->willReturn($request);
54
        $request->getAttribute('viewData', [])->willReturn(['bar' => 'baz']);
55
        $handler->handle($request)->willReturn($response);
56
57
        $inflector->inflect($route)->willReturn($controllerDispatch);
58
59
        $contextCreator->create($request, $route)->willReturn($context);
60
        $context->handlesResponse()->willReturn(false);
61
62
        $invoker->invoke($context, $controllerDispatch)
63
            ->willReturn($this->controllerData);
64
65
        $this->beConstructedWith($inflector, $invoker, $contextCreator);
66
    }
67
68
    function its_an_http_middleware()
69
    {
70
        $this->shouldBeAnInstanceOf(MiddlewareInterface::class);
71
    }
72
73
    function it_is_initializable()
74
    {
75
        $this->shouldHaveType(DispatcherMiddleware::class);
76
    }
77
78
    function it_process_the_request_if_it_has_a_route(
79
        ServerRequestInterface $request,
80
        ResponseInterface $response,
81
        ControllerDispatchInflectorInterface $inflector,
82
        Route $route,
83
        ControllerInvokerInterface $invoker,
84
        ControllerDispatch $controllerDispatch,
85
        RequestHandlerInterface $handler,
86
        ControllerContextInterface $context
87
    )
88
    {
89
        $this->process($request, $handler)->shouldBe($response);
90
        $inflector->inflect($route)->shouldHaveBeenCalled();
91
        $invoker->invoke(
92
            $context,
93
            $controllerDispatch
94
        )
95
            ->shouldHaveBeenCalled();
96
    }
97
98
    function it_throws_an_exception_for_missing_route_in_request(
99
        ServerRequestInterface $request,
100
        RequestHandlerInterface $handler
101
    )
102
    {
103
        $request->getAttribute('route', false)
104
            ->willReturn(false);
105
        $this->shouldThrow(BadHttpStackConfigurationException::class)
106
            ->during('process', [$request, $handler]);
107
    }
108
109
    function it_stores_the_dispatch_data_into_request_for_next_handler(
110
        ServerRequestInterface $request,
111
        RequestHandlerInterface $handler
112
    )
113
    {
114
        $request->withAttribute('viewData', $this->assignData)
115
            ->shouldBeCalled()
116
            ->willReturn($request);
117
        $this->process($request, $handler);
118
    }
119
120
    function it_returns_the_context_response_when_available(
121
        ServerRequestInterface $request,
122
        ResponseInterface $controlledResponse,
123
        RequestHandlerInterface $handler,
124
        ControllerContextInterface $context
125
    )
126
    {
127
        $context->handlesResponse()->willReturn(true);
128
        $context->response()->shouldBeCalled()->willReturn($controlledResponse);
129
        $this->process($request, $handler)->shouldBe($controlledResponse);
130
    }
131
132
    function it_throws_an_exception_if_render_is_disabled_and_no_response_is_given(
133
        ServerRequestInterface $request,
134
        RequestHandlerInterface $handler,
135
        ControllerContextInterface $context
136
    )
137
    {
138
        $context->handlesResponse()->willReturn(true);
139
        $context->response()->shouldBeCalled()->willReturn(null);
140
        $this->shouldThrow(MissingResponseException::class)
141
            ->during('process', [$request, $handler]);
142
    }
143
}