Completed
Push — master ( c4d705...5571d6 )
by Filipe
03:31
created

ContextSpec   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 108
Duplicated Lines 14.81 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 5
dl 16
loc 108
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\Controller;
11
12
use Aura\Router\Route;
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
use Slick\Http\Message\Uri;
16
use Slick\WebStack\Controller\Context;
17
use PhpSpec\ObjectBehavior;
18
use Slick\WebStack\Controller\ControllerContextInterface;
19
use Slick\WebStack\Service\UriGeneratorInterface;
20
21
/**
22
 * ContextSpec specs
23
 *
24
 * @package spec\Slick\WebStack\Controller
25
 */
26
class ContextSpec extends ObjectBehavior
27
{
28
29
    function let(ServerRequestInterface $request, UriGeneratorInterface $uriGenerator)
30
    {
31
        $route = (new Route())->attributes(['foo' =>'bar', 'bar' => 'baz']);
32
        $this->beConstructedWith($request, $route, $uriGenerator);
33
    }
34
35
    function its_a_controller_context()
36
    {
37
        $this->shouldBeAnInstanceOf(ControllerContextInterface::class);
38
    }
39
40
    function it_is_initializable()
41
    {
42
        $this->shouldHaveType(Context::class);
43
    }
44
45
    function it_can_have_a_list_of_query_params(
46
        ServerRequestInterface $request
47
    )
48
    {
49
        $data = ['foo' =>'bar', 'bar' => 'baz'];
50
        $request->getQueryParams()->willReturn($data);
51
        $this->queryParam()->shouldBe($data);
52
    }
53
54
    function it_can_retrieve_a_single_query_param_value(
55
        ServerRequestInterface $request
56
    )
57
    {
58
        $data = ['foo' =>'bar', 'bar' => 'baz'];
59
        $request->getQueryParams()->willReturn($data);
60
        $this->queryParam('foo')->shouldBe('bar');
61
    }
62
63
    function it_can_have_a_list_of_post_parameters(
64
        ServerRequestInterface $request
65
    )
66
    {
67
        $data = ['foo' =>'bar', 'bar' => 'baz'];
68
        $request->getParsedBody()->willReturn($data);
69
        $this->postParam()->shouldBe($data);
70
    }
71
72
    function it_will_return_a_default_value_for_missing_parameters(
73
        ServerRequestInterface $request
74
    )
75
    {
76
        $data = ['foo' =>'bar', 'bar' => 'baz'];
77
        $request->getParsedBody()->willReturn($data);
78
        $this->postParam('baz', 'foo')->shouldBe('foo');
79
    }
80
81
    function it_can_retrieve_a_route_parameter()
82
    {
83
        $this->routeParam('foo')->shouldBe('bar');
84
    }
85
86
    function it_has_a_server_request(ServerRequestInterface $request)
87
    {
88
        $this->request()->shouldBe($request);
89
    }
90
91
    function it_can_check_if_current_request_has_a_given_method(
92
        ServerRequestInterface $request
93
    )
94
    {
95
        $request->getMethod()->willReturn('PUT');
96
        $this->requestIs('put')->shouldBe(true);
97
    }
98
99
    function it_can_be_set_with_a_response(ResponseInterface $response)
100
    {
101
        $this->setResponse($response)->shouldBe($this->getWrappedObject());
102
        $this->response()->shouldBe($response);
103
    }
104
105
    function it_can_set_a_redirect_response(UriGeneratorInterface $uriGenerator)
106
    {
107
        $uriGenerator->generate('/some/page', ['foo' => 'bar'])
108
            ->shouldBeCalled()
109
            ->willReturn(new Uri('http://example.com/some/page?foo=bar'));
110
111
        $this->redirect('/some/page', ['foo' => 'bar']);
112
        $response = $this->response();
113
        $response->shouldBeAnInstanceOf(ResponseInterface::class);
114
        $response->getStatusCode()->shouldBe(302);
115
        $response->getHeaderLine('Location')->shouldBe('http://example.com/some/page?foo=bar');
116
        $this->handlesResponse()->shouldBe(true);
117
    }
118
119
    function it_can_set_the_template_to_be_used(ServerRequestInterface $request)
120
    {
121
        $request->withAttribute('template', 'some/path')
122
            ->shouldBeCalled()
123
            ->willReturn($request);
124
        $this->useTemplate('some/path')->shouldBe($this->getWrappedObject());
125
    }
126
127
    function it_can_change_the_request(ServerRequestInterface $otherRequest, ServerRequestInterface $request)
128
    {
129
        $this->changeRequest($otherRequest)->shouldBe($this->getWrappedObject());
130
        $this->request()->shouldBe($otherRequest);
131
        $this->request()->shouldNotBe($request);
132
    }
133
}