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

BasePathTransformerSpec   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 111
Duplicated Lines 39.64 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 44
loc 111
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\Service\UriGenerator\Transformer;
11
12
use PhpSpec\Exception\Example\FailureException;
13
use PhpSpec\Wrapper\Collaborator;
14
use Psr\Http\Message\ServerRequestInterface;
15
use Psr\Http\Message\UriInterface;
16
use Slick\WebStack\Service\UriGenerator\LocationTransformerInterface;
17
use Slick\WebStack\Service\UriGenerator\Transformer\BasePathTransformer;
18
use PhpSpec\ObjectBehavior;
19
20
/**
21
 * BasePathTransformerSpec specs
22
 *
23
 * @package spec\Slick\WebStack\Service\UriGenerator\Transformer
24
 */
25
class BasePathTransformerSpec extends ObjectBehavior
26
{
27
    function it_is_initializable()
28
    {
29
        $this->shouldHaveType(BasePathTransformer::class);
30
    }
31
32
    function it_implements_location_transformer_interface()
33
    {
34
        $this->shouldImplement(LocationTransformerInterface::class);
35
    }
36
37
    function it_may_have_an_http_request_as_a_context(
38
        ServerRequestInterface $request
39
    ) {
40
        $this->setRequest($request)
41
            ->shouldReturn($this->getWrappedObject());
42
    }
43
44
    function it_only_generates_an_uri_if_it_has_an_http_request()
45
    {
46
        $this->transform('home')->shouldBeNull();
47
    }
48
49
    function it_adds_the_base_path_to_the_location(
50
        ServerRequestInterface $request
51
    )
52
    {
53
        $this->prepareRequest($request);
54
        $this->transform('controller/action')
55
            ->shouldBeAnUriWithPath('/base/controller/action');
56
    }
57
58
    function it_accepts_query_params_in_options(
59
        ServerRequestInterface $request
60
    )
61
    {
62
        $this->prepareRequest($request);
63
        $this->transform('controller/action', ['query' => ['foo' => 'bar']])
64
            ->shouldBeAnUriWithPath('/base/controller/action?foo=bar');
65
    }
66
67
    function it_can_reuse_host_name_from_context_request(
68
        ServerRequestInterface $request
69
    )
70
    {
71
        $this->prepareRequest($request);
72
        $this->transform('controller/action', ['reuseHostName' => 1])
73
            ->shouldBeAnUriWithPath(
74
                'https://localhost:12541/base/controller/action'
75
            );
76
    }
77
78
    function it_can_reuse_the_request_query_params(
79
        ServerRequestInterface $request
80
    )
81
    {
82
        $this->prepareRequest($request);
83
        $this->transform('controller/action', [
84
            'reuseParams' => 1,
85
            'query' => ['foo' => 'bar']
86
        ])
87
            ->shouldBeAnUriWithPath(
88
                '/base/controller/action?foo=bar&baz=bar'
89
            );
90
    }
91
92
    /**
93
     * Prepares the request collaborator
94
     *
95
     * @param ServerRequestInterface|Collaborator $request
96
     */
97
    private function prepareRequest(ServerRequestInterface $request)
98
    {
99
        $serverData = [
100
            'SCRIPT_NAME' => '/base/test.php',
101
            'HTTPS' => 'not-empty',
102
            'SERVER_PORT' => '12541',
103
            'SERVER_NAME' => 'localhost',
104
        ];
105
        $request->getServerParams()
106
            ->shouldBeCalled()
107
            ->willReturn($serverData);
108
        $request->getQueryParams()->willReturn(
109
            ['foo' => 'bar', 'baz' => 'bar']
110
        );
111
        $this->setRequest($request);
112
    }
113
114
    public function getMatchers()
115
    {
116
        return [
117
            'beAnUriWithPath' => function ($uri, $path)
118
            {
119
                if (!$uri instanceof UriInterface) {
120
                    $class = UriInterface::class;
121
                    $type = gettype($uri);
122
                    throw new FailureException(
123
                        "Expected {$class} instance, but got '{$type}'"
124
                    );
125
                }
126
                if ($uri->__toString() !== $path) {
127
                    throw new FailureException(
128
                        "Expected URI with path '{$path}', but got '{$uri}'"
129
                    );
130
                }
131
                return true;
132
            }
133
        ];
134
    }
135
}