1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2017 Gerrit Addiks. |
4
|
|
|
* This package (including this file) was released under the terms of the GPL-3.0. |
5
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
6
|
|
|
* If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy. |
7
|
|
|
* @license GPL-3.0 |
8
|
|
|
* @author Gerrit Addiks <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Addiks\SymfonyGenerics\Tests\Unit\Controllers\API; |
12
|
|
|
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use Psr\Container\ContainerInterface; |
15
|
|
|
use Addiks\SymfonyGenerics\Controllers\API\GenericServiceInvokeController; |
16
|
|
|
use Addiks\SymfonyGenerics\Controllers\ControllerHelperInterface; |
17
|
|
|
use Addiks\SymfonyGenerics\Services\ArgumentCompilerInterface; |
18
|
|
|
use Addiks\SymfonyGenerics\Tests\Unit\Controllers\SampleService; |
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
21
|
|
|
use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException; |
22
|
|
|
use InvalidArgumentException; |
23
|
|
|
use ReflectionMethod; |
24
|
|
|
use ErrorException; |
25
|
|
|
|
26
|
|
|
final class GenericServiceInvokeControllerTest extends TestCase |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ControllerHelperInterface |
31
|
|
|
*/ |
32
|
|
|
private $controllerHelper; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ArgumentCompilerInterface |
36
|
|
|
*/ |
37
|
|
|
private $argumentCompiler; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var ContainerInterface |
41
|
|
|
*/ |
42
|
|
|
private $container; |
43
|
|
|
|
44
|
|
|
public function setUp() |
45
|
|
|
{ |
46
|
|
|
$this->controllerHelper = $this->createMock(ControllerHelperInterface::class); |
|
|
|
|
47
|
|
|
$this->argumentCompiler = $this->createMock(ArgumentCompilerInterface::class); |
|
|
|
|
48
|
|
|
$this->container = $this->createMock(ContainerInterface::class); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @test |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public function shouldPreventConstructorCalledAgain() |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$this->expectException(InvalidArgumentException::class); |
57
|
|
|
|
58
|
|
|
$controller = new GenericServiceInvokeController( |
59
|
|
|
$this->controllerHelper, |
60
|
|
|
$this->argumentCompiler, |
61
|
|
|
$this->container, |
62
|
|
|
[ |
63
|
|
|
'service' => 'some_service', |
64
|
|
|
'method' => 'doFoo' |
65
|
|
|
] |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
$controller->__construct( |
69
|
|
|
$this->controllerHelper, |
70
|
|
|
$this->argumentCompiler, |
71
|
|
|
$this->container, |
72
|
|
|
[ |
73
|
|
|
'service' => 'some_service', |
74
|
|
|
'method' => 'doFoo' |
75
|
|
|
] |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @test |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
public function shouldFailIfServiceIsMissing() |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$this->expectException(InvalidArgumentException::class); |
85
|
|
|
|
86
|
|
|
$controller = new GenericServiceInvokeController( |
|
|
|
|
87
|
|
|
$this->controllerHelper, |
88
|
|
|
$this->argumentCompiler, |
89
|
|
|
$this->container, |
90
|
|
|
[ |
91
|
|
|
'method' => 'doFoo' |
92
|
|
|
] |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @test |
98
|
|
|
*/ |
99
|
|
View Code Duplication |
public function shouldFailIfMethodIsMissing() |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$this->expectException(InvalidArgumentException::class); |
102
|
|
|
|
103
|
|
|
$controller = new GenericServiceInvokeController( |
|
|
|
|
104
|
|
|
$this->controllerHelper, |
105
|
|
|
$this->argumentCompiler, |
106
|
|
|
$this->container, |
107
|
|
|
[ |
108
|
|
|
'service' => 'some_service', |
109
|
|
|
] |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @test |
115
|
|
|
*/ |
116
|
|
View Code Duplication |
public function shouldCallService() |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
/** @var Request $request */ |
119
|
|
|
$request = $this->createMock(Request::class); |
120
|
|
|
|
121
|
|
|
/** @var SampleService $service */ |
122
|
|
|
$service = $this->createMock(SampleService::class); |
123
|
|
|
|
124
|
|
|
$service->expects($this->once())->method('doFoo')->with( |
|
|
|
|
125
|
|
|
$this->equalTo('lorem'), |
126
|
|
|
$this->equalTo('ipsum') |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
$this->container->expects($this->once())->method('get')->with( |
|
|
|
|
130
|
|
|
$this->equalTo('some_service') |
131
|
|
|
)->willReturn($service); |
132
|
|
|
|
133
|
|
|
$this->argumentCompiler->expects($this->once())->method('buildCallArguments')->with( |
|
|
|
|
134
|
|
|
$this->equalTo(new ReflectionMethod($service, 'doFoo')), |
135
|
|
|
$this->equalTo(['lorem' => 'ipsum']), |
136
|
|
|
$this->identicalTo($request) |
137
|
|
|
)->willReturn(['lorem', 'ipsum']); |
138
|
|
|
|
139
|
|
|
/** @var string $expectedResponseContent */ |
140
|
|
|
$expectedResponseContent = "Service call completed"; |
141
|
|
|
|
142
|
|
|
$controller = new GenericServiceInvokeController( |
143
|
|
|
$this->controllerHelper, |
144
|
|
|
$this->argumentCompiler, |
145
|
|
|
$this->container, |
146
|
|
|
[ |
147
|
|
|
'service' => 'some_service', |
148
|
|
|
'method' => 'doFoo', |
149
|
|
|
'arguments' => ['lorem' => 'ipsum'] |
150
|
|
|
] |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
/** @var Response $actualResponse */ |
154
|
|
|
$actualResponse = $controller->callService($request); |
155
|
|
|
|
156
|
|
|
$this->assertEquals($expectedResponseContent, $actualResponse->getContent()); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @test |
161
|
|
|
*/ |
162
|
|
View Code Duplication |
public function shouldCheckIfAccessIsGranted() |
|
|
|
|
163
|
|
|
{ |
164
|
|
|
$this->expectException(AccessDeniedException::class); |
165
|
|
|
|
166
|
|
|
/** @var Request $request */ |
167
|
|
|
$request = $this->createMock(Request::class); |
168
|
|
|
|
169
|
|
|
$this->controllerHelper->expects($this->once())->method('denyAccessUnlessGranted')->with( |
|
|
|
|
170
|
|
|
$this->equalTo('bar'), |
171
|
|
|
$this->identicalTo($request) |
172
|
|
|
)->will($this->returnCallback( |
173
|
|
|
function () { |
174
|
|
|
throw new AccessDeniedException("Lorem ipsum"); |
175
|
|
|
} |
176
|
|
|
)); |
177
|
|
|
|
178
|
|
|
$controller = new GenericServiceInvokeController( |
179
|
|
|
$this->controllerHelper, |
180
|
|
|
$this->argumentCompiler, |
181
|
|
|
$this->container, |
182
|
|
|
[ |
183
|
|
|
'service' => 'some_service', |
184
|
|
|
'method' => 'doFoo', |
185
|
|
|
'authorization-attributes' => 'bar' |
186
|
|
|
] |
187
|
|
|
); |
188
|
|
|
|
189
|
|
|
$controller->callService($request); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @test |
194
|
|
|
*/ |
195
|
|
|
public function shouldThrowExceptionIfServiceNotFound() |
196
|
|
|
{ |
197
|
|
|
$this->expectException(ErrorException::class); |
198
|
|
|
|
199
|
|
|
/** @var Request $request */ |
200
|
|
|
$request = $this->createMock(Request::class); |
201
|
|
|
|
202
|
|
|
$this->container->expects($this->once())->method('get')->with( |
|
|
|
|
203
|
|
|
$this->equalTo('some_service') |
204
|
|
|
)->willReturn(null); |
205
|
|
|
|
206
|
|
|
$controller = new GenericServiceInvokeController( |
207
|
|
|
$this->controllerHelper, |
208
|
|
|
$this->argumentCompiler, |
209
|
|
|
$this->container, |
210
|
|
|
[ |
211
|
|
|
'service' => 'some_service', |
212
|
|
|
'method' => 'doFoo', |
213
|
|
|
'arguments' => ['lorem' => 'ipsum'] |
214
|
|
|
] |
215
|
|
|
); |
216
|
|
|
|
217
|
|
|
$controller->callService($request); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @test |
222
|
|
|
*/ |
223
|
|
View Code Duplication |
public function shouldBeCallableByInvokingController() |
|
|
|
|
224
|
|
|
{ |
225
|
|
|
/** @var Request $request */ |
226
|
|
|
$request = $this->createMock(Request::class); |
227
|
|
|
|
228
|
|
|
/** @var SampleService $service */ |
229
|
|
|
$service = $this->createMock(SampleService::class); |
230
|
|
|
|
231
|
|
|
$service->expects($this->once())->method('doFoo')->with( |
|
|
|
|
232
|
|
|
$this->equalTo('lorem'), |
233
|
|
|
$this->equalTo('ipsum') |
234
|
|
|
); |
235
|
|
|
|
236
|
|
|
$this->container->expects($this->once())->method('get')->with( |
|
|
|
|
237
|
|
|
$this->equalTo('some_service') |
238
|
|
|
)->willReturn($service); |
239
|
|
|
|
240
|
|
|
$this->argumentCompiler->expects($this->once())->method('buildCallArguments')->with( |
|
|
|
|
241
|
|
|
$this->equalTo(new ReflectionMethod($service, 'doFoo')), |
242
|
|
|
$this->equalTo(['lorem' => 'ipsum']), |
243
|
|
|
$this->identicalTo($request) |
244
|
|
|
)->willReturn(['lorem', 'ipsum']); |
245
|
|
|
|
246
|
|
|
/** @var string $expectedResponseContent */ |
247
|
|
|
$expectedResponseContent = "Service call completed"; |
248
|
|
|
|
249
|
|
|
$controller = new GenericServiceInvokeController( |
250
|
|
|
$this->controllerHelper, |
251
|
|
|
$this->argumentCompiler, |
252
|
|
|
$this->container, |
253
|
|
|
[ |
254
|
|
|
'service' => 'some_service', |
255
|
|
|
'method' => 'doFoo', |
256
|
|
|
'arguments' => ['lorem' => 'ipsum'] |
257
|
|
|
] |
258
|
|
|
); |
259
|
|
|
|
260
|
|
|
$this->controllerHelper->method('getCurrentRequest')->willReturn($request); |
|
|
|
|
261
|
|
|
|
262
|
|
|
/** @var Response $actualResponse */ |
263
|
|
|
$actualResponse = $controller(); |
264
|
|
|
|
265
|
|
|
$this->assertEquals($expectedResponseContent, $actualResponse->getContent()); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @test |
270
|
|
|
*/ |
271
|
|
View Code Duplication |
public function shouldRejectCallWithoutRequest() |
|
|
|
|
272
|
|
|
{ |
273
|
|
|
$this->expectException(InvalidArgumentException::class); |
274
|
|
|
|
275
|
|
|
$controller = new GenericServiceInvokeController( |
276
|
|
|
$this->controllerHelper, |
277
|
|
|
$this->argumentCompiler, |
278
|
|
|
$this->container, |
279
|
|
|
[ |
280
|
|
|
'service' => 'some_service', |
281
|
|
|
'method' => 'doFoo', |
282
|
|
|
] |
283
|
|
|
); |
284
|
|
|
|
285
|
|
|
$this->controllerHelper->method('getCurrentRequest')->willReturn(null); |
|
|
|
|
286
|
|
|
|
287
|
|
|
$controller(); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
} |
291
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..