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

RouterPathTransformerSpec::prepareTest()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 19
nc 1
nop 3
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 Aura\Router\Exception\RouteNotFound;
13
use Aura\Router\Generator;
14
use Aura\Router\RouterContainer;
15
use PhpSpec\Exception\Example\FailureException;
16
use PhpSpec\Wrapper\Collaborator;
17
use Prophecy\Argument;
18
use Psr\Http\Message\ServerRequestInterface;
19
use Psr\Http\Message\UriInterface;
20
use Slick\WebStack\Service\UriGenerator\LocationTransformerInterface;
21
use Slick\WebStack\Service\UriGenerator\Transformer\RouterPathTransformer;
22
use PhpSpec\ObjectBehavior;
23
24
/**
25
 * RouterPathTransformerSpec specs
26
 *
27
 * @package spec\Slick\WebStack\Service\UriGenerator\Transformer
28
 */
29
class RouterPathTransformerSpec extends ObjectBehavior
30
{
31
    function let(RouterContainer $router)
32
    {
33
        $this->beConstructedWith($router);
34
    }
35
36
    function it_is_initializable()
37
    {
38
        $this->shouldHaveType(RouterPathTransformer::class);
39
    }
40
41
    function it_implements_location_transformer_interface()
42
    {
43
        $this->shouldImplement(LocationTransformerInterface::class);
44
    }
45
46
    function it_will_return_null_for_unknown_routes(
47
        RouterContainer $router,
48
        Generator $generator
49
    )
50
    {
51
        $router->getGenerator()->willReturn($generator);
52
        $this->beConstructedWith($router);
53
54
        $generator->generate('test', [])
55
            ->shouldBeCalled()
56
            ->willThrow(new RouteNotFound());
57
58
        $this->transform('test')->shouldBeNull();
59
    }
60
61
    function it_sets_the_uri_path_form_router_container(
62
        RouterContainer $router,
63
        Generator $generator
64
    )
65
    {
66
        $router->getGenerator()->willReturn($generator);
67
        $this->beConstructedWith($router);
68
69
        $generator->generate('about', [])
70
            ->shouldBeCalled()
71
            ->willReturn('/pages/about');
72
73
        $this->transform('about')->shouldBeAnUriWithPath('/pages/about');
74
    }
75
76
    function it_accepts_query_params_in_options(
77
        ServerRequestInterface $request,
78
        RouterContainer $router,
79
        Generator $generator
80
    )
81
    {
82
        $this->prepareTest($request, $router, $generator);
83
84
        $this->transform('about', ['query' => ['foo' => 'bar']])
85
            ->shouldBeAnUriWithPath('/pages/about?foo=bar');
86
    }
87
88
    function it_can_reuse_host_name_from_context_request(
89
        ServerRequestInterface $request,
90
        RouterContainer $router,
91
        Generator $generator
92
    )
93
    {
94
        $this->prepareTest($request, $router, $generator);
95
        $this->transform('about', ['reuseHostName' => 1])
96
            ->shouldBeAnUriWithPath('https://localhost:12541/pages/about');
97
    }
98
99
    function it_can_reuse_the_request_query_params(
100
        ServerRequestInterface $request,
101
        RouterContainer $router,
102
        Generator $generator
103
    )
104
    {
105
        $this->prepareTest($request, $router, $generator);
106
        $this->transform('about', [
107
            'reuseParams' => 1,
108
            'query' => ['foo' => 'bar']
109
        ])
110
            ->shouldBeAnUriWithPath(
111
                '/pages/about?foo=bar&baz=bar'
112
            );
113
    }
114
115
    /**
116
     * Prepares the test
117
     *
118
     * @param ServerRequestInterface|Collaborator $request
119
     * @param RouterContainer|Collaborator $router
120
     * @param Generator|Collaborator $generator
121
     */
122
    private function prepareTest(
123
        ServerRequestInterface $request,
124
        RouterContainer $router,
125
        Generator $generator
126
    )
127
    {
128
        $this->beConstructedWith($router);
129
        $router->getGenerator()->willReturn($generator);
130
        $generator->generate('about', Argument::type('array'))
131
            ->shouldBeCalled()
132
            ->willReturn('/pages/about');
133
        $serverData = [
134
            'SCRIPT_NAME' => '/base/test.php',
135
            'HTTPS' => 'not-empty',
136
            'SERVER_PORT' => '12541',
137
            'SERVER_NAME' => 'localhost',
138
        ];
139
        $request->getServerParams()
140
            ->willReturn($serverData);
141
        $request->getQueryParams()->willReturn(
142
            ['foo' => 'bar', 'baz' => 'bar']
143
        );
144
        $this->setRequest($request);
145
    }
146
147
    public function getMatchers()
148
    {
149
        return [
150
            'beAnUriWithPath' => function ($uri, $path)
151
            {
152
                if (!$uri instanceof UriInterface) {
153
                    $class = UriInterface::class;
154
                    $type = gettype($uri);
155
                    throw new FailureException(
156
                        "Expected {$class} instance, but got '{$type}'"
157
                    );
158
                }
159
160
                if ($uri->__toString() !== $path) {
161
                    throw new FailureException(
162
                        "Expected URI with path '{$path}', but got '{$uri}'"
163
                    );
164
                }
165
                return true;
166
            }
167
        ];
168
    }
169
}