Completed
Push — master ( bd4c23...b341c0 )
by Filipe
10:29
created

BasePathTransformerSpec   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 115
Duplicated Lines 38.26 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 44
loc 115
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A it_is_initializable() 0 4 1
A it_implements_location_transformer_interface() 0 4 1
A it_may_have_an_http_request_as_a_context() 0 6 1
A it_only_generates_an_uri_if_it_has_an_http_request() 0 4 1
A it_adds_the_base_path_to_the_location() 0 9 1
A it_accepts_query_params_in_options() 8 8 1
A it_can_reuse_host_name_from_context_request() 0 11 1
A it_can_reuse_the_request_query_params() 14 14 1
A prepareRequest() 0 16 1
A getMatchers() 22 22 3

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
namespace spec\Slick\Mvc\Service\UriGenerator\Transformer;
4
5
use PhpSpec\Exception\Example\FailureException;
6
use PhpSpec\Wrapper\Collaborator;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Message\UriInterface;
9
use Slick\Mvc\Service\UriGenerator\LocationTransformerInterface;
10
use Slick\Mvc\Service\UriGenerator\Transformer\BasePathTransformer;
11
use PhpSpec\ObjectBehavior;
12
13
/**
14
 * BasePathTransformerSpec
15
 *
16
 * @package spec\Slick\Mvc\Service\UriGenerator\Transformer
17
 * @author  Filipe Silva <[email protected]>
18
 *
19
 * @method boolean shouldBeAnUriWithPath()
20
 */
21
class BasePathTransformerSpec extends ObjectBehavior
22
{
23
    function it_is_initializable()
24
    {
25
        $this->shouldHaveType(BasePathTransformer::class);
26
    }
27
28
    function it_implements_location_transformer_interface()
29
    {
30
        $this->shouldImplement(LocationTransformerInterface::class);
31
    }
32
33
    function it_may_have_an_http_request_as_a_context(
34
        ServerRequestInterface $request
35
    ) {
36
        $this->setRequest($request)
37
            ->shouldReturn($this->getWrappedObject());
38
    }
39
40
    function it_only_generates_an_uri_if_it_has_an_http_request()
41
    {
42
        $this->transform('home')->shouldBeNull();
43
    }
44
45
    function it_adds_the_base_path_to_the_location(
46
        ServerRequestInterface $request
47
    )
48
    {
49
        $this->prepareRequest($request);
50
51
        $this->transform('controller/action')
52
            ->shouldBeAnUriWithPath('/base/controller/action');
53
    }
54
55 View Code Duplication
    function it_accepts_query_params_in_options(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
56
        ServerRequestInterface $request
57
    )
58
    {
59
        $this->prepareRequest($request);
60
        $this->transform('controller/action', ['query' => ['foo' => 'bar']])
61
            ->shouldBeAnUriWithPath('/base/controller/action?foo=bar');
62
    }
63
64
    function it_can_reuse_host_name_from_context_request(
65
        ServerRequestInterface $request
66
    )
67
    {
68
        $this->prepareRequest($request);
69
70
        $this->transform('controller/action', ['reuseHostName' => 1])
71
            ->shouldBeAnUriWithPath(
72
                'https://localhost:12541/base/controller/action'
73
            );
74
    }
75
76 View Code Duplication
    function it_can_reuse_the_request_query_params(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
77
        ServerRequestInterface $request
78
    )
79
    {
80
        $this->prepareRequest($request);
81
82
        $this->transform('controller/action', [
83
            'reuseParams' => 1,
84
            'query' => ['foo' => 'bar']
85
        ])
86
            ->shouldBeAnUriWithPath(
87
                '/base/controller/action?foo=bar&baz=bar'
88
            );
89
    }
90
91
    /**
92
     * Prepares the request collaborator
93
     *
94
     * @param ServerRequestInterface|Collaborator $request
95
     */
96
    private function prepareRequest(ServerRequestInterface $request)
97
    {
98
        $serverData = [
99
            'SCRIPT_NAME' => '/base/test.php',
100
            'HTTPS' => 'not-empty',
101
            'SERVER_PORT' => '12541',
102
            'SERVER_NAME' => 'localhost',
103
        ];
104
        $request->getServerParams()
105
            ->shouldBeCalled()
106
            ->willReturn($serverData);
107
        $request->getQueryParams()->willReturn(
108
            ['foo' => 'bar', 'baz' => 'bar']
109
        );
110
        $this->setRequest($request);
111
    }
112
113 View Code Duplication
    public function getMatchers()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
114
    {
115
        return [
116
            'beAnUriWithPath' => function ($uri, $path)
117
            {
118
                if (!$uri instanceof UriInterface) {
119
                    $class = UriInterface::class;
120
                    $type = gettype($uri);
121
                    throw new FailureException(
122
                        "Expected {$class} instance, but got '{$type}'"
123
                    );
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
}
136