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