1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace spec\Slick\Mvc\Controller; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
7
|
|
|
use Slick\Di\ContainerInjectionInterface; |
8
|
|
|
use Slick\Mvc\Controller\Context; |
9
|
|
|
use PhpSpec\ObjectBehavior; |
10
|
|
|
use Prophecy\Argument; |
11
|
|
|
use Slick\Mvc\Controller\ControllerContextInterface; |
12
|
|
|
use Slick\Mvc\Service\UriGeneratorInterface; |
13
|
|
|
|
14
|
|
|
class ContextSpec extends ObjectBehavior |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
function let(UriGeneratorInterface $uriGenerator) |
18
|
|
|
{ |
19
|
|
|
$this->beConstructedWith($uriGenerator); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
function it_is_initializable() |
23
|
|
|
{ |
24
|
|
|
$this->shouldHaveType(Context::class); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
function its_a_controller_context() |
28
|
|
|
{ |
29
|
|
|
$this->shouldBeAnInstanceOf(ControllerContextInterface::class); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
function it_implements_a_container_injection_for_initialization() |
33
|
|
|
{ |
34
|
|
|
$this->shouldImplement(ContainerInjectionInterface::class); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
function it_uses_an_http_request_and_response( |
38
|
|
|
ServerRequestInterface $request, |
39
|
|
|
ResponseInterface $response |
40
|
|
|
) |
41
|
|
|
{ |
42
|
|
|
$this->register($request, $response)->shouldBe($this->getWrappedObject()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
View Code Duplication |
function it_retrieves_the_server_parameters_on_post( |
|
|
|
|
46
|
|
|
ServerRequestInterface $request, |
47
|
|
|
ResponseInterface $response |
48
|
|
|
) |
49
|
|
|
{ |
50
|
|
|
$request->getServerParams()->willReturn( |
51
|
|
|
[ |
52
|
|
|
'foo' => 'bar', |
53
|
|
|
'bar' => 'baz' |
54
|
|
|
] |
55
|
|
|
); |
56
|
|
|
$this->register($request, $response); |
57
|
|
|
$this->getPost()->shouldBe([ |
58
|
|
|
'foo' => 'bar', |
59
|
|
|
'bar' => 'baz' |
60
|
|
|
]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
View Code Duplication |
function it_can_retrieve_a_single_named_parameter( |
|
|
|
|
64
|
|
|
ServerRequestInterface $request, |
65
|
|
|
ResponseInterface $response |
66
|
|
|
) |
67
|
|
|
{ |
68
|
|
|
$request->getServerParams()->willReturn( |
69
|
|
|
[ |
70
|
|
|
'foo' => 'bar', |
71
|
|
|
'bar' => 'baz' |
72
|
|
|
] |
73
|
|
|
); |
74
|
|
|
$this->register($request, $response); |
75
|
|
|
$this->getPost('bar')->shouldBe('baz'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
View Code Duplication |
function it_returns_a_default_value_if_param_is_not_set( |
|
|
|
|
79
|
|
|
ServerRequestInterface $request, |
80
|
|
|
ResponseInterface $response |
81
|
|
|
) |
82
|
|
|
{ |
83
|
|
|
$request->getServerParams()->willReturn( |
84
|
|
|
[ |
85
|
|
|
'foo' => 'bar', |
86
|
|
|
'bar' => 'baz' |
87
|
|
|
] |
88
|
|
|
); |
89
|
|
|
$this->register($request, $response); |
90
|
|
|
$this->getPost('boo', 'test')->shouldBe('test'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
function it_can_get_URL_parameters( |
94
|
|
|
ServerRequestInterface $request, |
95
|
|
|
ResponseInterface $response |
96
|
|
|
) |
97
|
|
|
{ |
98
|
|
|
$request->getQueryParams()->willReturn( |
99
|
|
|
[ |
100
|
|
|
'foo' => 'bar', |
101
|
|
|
'bar' => 'baz' |
102
|
|
|
] |
103
|
|
|
); |
104
|
|
|
$this->register($request, $response); |
105
|
|
|
$this->getQuery('boo', 'test')->shouldBe('test'); |
106
|
|
|
$this->getQuery('bar')->shouldBe('baz'); |
107
|
|
|
$this->getQuery()->shouldBe([ |
108
|
|
|
'foo' => 'bar', |
109
|
|
|
'bar' => 'baz' |
110
|
|
|
]); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
function it_can_set_a_redirecting_http_response( |
114
|
|
|
ServerRequestInterface $request, |
115
|
|
|
ResponseInterface $response, |
116
|
|
|
UriGeneratorInterface $uriGenerator |
117
|
|
|
) |
118
|
|
|
{ |
119
|
|
|
$this->beConstructedWith($uriGenerator); |
120
|
|
|
|
121
|
|
|
$location = 'home'; |
122
|
|
|
$response->withStatus(302)->willReturn($response); |
123
|
|
|
$response->withHeader('location', '/pages/index') |
124
|
|
|
->shouldBeCalled() |
125
|
|
|
->willReturn($response); |
126
|
|
|
|
127
|
|
|
$uriGenerator->generate($location, []) |
128
|
|
|
->shouldBeCalled() |
129
|
|
|
->willReturn('/pages/index'); |
130
|
|
|
|
131
|
|
|
$this->register($request, $response); |
132
|
|
|
$this->redirect($location); |
133
|
|
|
|
134
|
|
|
$response->withStatus(302)->shouldHaveBeenCalled(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
function it_can_set_the_disable_rendering_flag_on_request( |
138
|
|
|
ServerRequestInterface $request, |
139
|
|
|
ResponseInterface $response |
140
|
|
|
) |
141
|
|
|
{ |
142
|
|
|
$request->withAttribute('rendering', false)->willReturn($request); |
143
|
|
|
$this->register($request, $response); |
144
|
|
|
$this->disableRendering()->shouldBe($this->getWrappedObject()); |
145
|
|
|
$request->withAttribute('rendering', false)->shouldHaveBeenCalled(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
function it_can_set_the_rendering_view( |
149
|
|
|
ServerRequestInterface $request, |
150
|
|
|
ResponseInterface $response |
151
|
|
|
) |
152
|
|
|
{ |
153
|
|
|
$this->register($request, $response); |
154
|
|
|
|
155
|
|
|
$request->withAttribute('view', 'path/to/view.twig') |
156
|
|
|
->shouldBeCalled() |
157
|
|
|
->willReturn($request); |
158
|
|
|
$this->setView('path/to/view.twig')->shouldReturn($this->getWrappedObject()); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
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.