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->controllerHelper->expects($this->once())->method('flushORM'); |
|
|
|
|
134
|
|
|
|
135
|
|
|
$this->argumentCompiler->expects($this->once())->method('buildCallArguments')->with( |
|
|
|
|
136
|
|
|
$this->equalTo(new ReflectionMethod($service, 'doFoo')), |
137
|
|
|
$this->equalTo(['lorem' => 'ipsum']), |
138
|
|
|
$this->identicalTo($request) |
139
|
|
|
)->willReturn(['lorem', 'ipsum']); |
140
|
|
|
|
141
|
|
|
/** @var string $expectedResponseContent */ |
142
|
|
|
$expectedResponseContent = "Service call completed"; |
143
|
|
|
|
144
|
|
|
$controller = new GenericServiceInvokeController( |
145
|
|
|
$this->controllerHelper, |
146
|
|
|
$this->argumentCompiler, |
147
|
|
|
$this->container, |
148
|
|
|
[ |
149
|
|
|
'service' => 'some_service', |
150
|
|
|
'method' => 'doFoo', |
151
|
|
|
'arguments' => ['lorem' => 'ipsum'] |
152
|
|
|
] |
153
|
|
|
); |
154
|
|
|
|
155
|
|
|
/** @var Response $actualResponse */ |
156
|
|
|
$actualResponse = $controller->callService($request); |
157
|
|
|
|
158
|
|
|
$this->assertEquals($expectedResponseContent, $actualResponse->getContent()); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @test |
163
|
|
|
*/ |
164
|
|
|
public function shouldRedirectWhenNeeded() |
165
|
|
|
{ |
166
|
|
|
/** @var Request $request */ |
167
|
|
|
$request = $this->createMock(Request::class); |
168
|
|
|
|
169
|
|
|
/** @var SampleService $service */ |
170
|
|
|
$service = $this->createMock(SampleService::class); |
171
|
|
|
|
172
|
|
|
$this->container->expects($this->once())->method('get')->with( |
|
|
|
|
173
|
|
|
$this->equalTo('some_service') |
174
|
|
|
)->willReturn($service); |
175
|
|
|
|
176
|
|
|
$this->argumentCompiler->expects($this->once())->method('buildCallArguments')->with( |
|
|
|
|
177
|
|
|
$this->equalTo(new ReflectionMethod($service, 'doFoo')), |
178
|
|
|
$this->equalTo(['lorem' => 'ipsum']), |
179
|
|
|
$this->identicalTo($request) |
180
|
|
|
)->willReturn(['lorem', 'ipsum']); |
181
|
|
|
|
182
|
|
|
$controller = new GenericServiceInvokeController( |
183
|
|
|
$this->controllerHelper, |
184
|
|
|
$this->argumentCompiler, |
185
|
|
|
$this->container, |
186
|
|
|
[ |
187
|
|
|
'service' => 'some_service', |
188
|
|
|
'method' => 'doFoo', |
189
|
|
|
'arguments' => ['lorem' => 'ipsum'], |
190
|
|
|
'success-redirect' => 'some_redirect_route' |
191
|
|
|
] |
192
|
|
|
); |
193
|
|
|
|
194
|
|
|
$this->controllerHelper->expects($this->once())->method('redirectToRoute')->with( |
|
|
|
|
195
|
|
|
$this->equalTo("some_redirect_route"), |
196
|
|
|
$this->equalTo([]), |
197
|
|
|
$this->equalTo(303) |
198
|
|
|
); |
199
|
|
|
|
200
|
|
|
$controller->callService($request); |
201
|
|
|
|
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @test |
206
|
|
|
*/ |
207
|
|
View Code Duplication |
public function shouldCheckIfAccessIsGranted() |
|
|
|
|
208
|
|
|
{ |
209
|
|
|
$this->expectException(AccessDeniedException::class); |
210
|
|
|
|
211
|
|
|
/** @var Request $request */ |
212
|
|
|
$request = $this->createMock(Request::class); |
213
|
|
|
|
214
|
|
|
$this->controllerHelper->expects($this->once())->method('denyAccessUnlessGranted')->with( |
|
|
|
|
215
|
|
|
$this->equalTo('bar'), |
216
|
|
|
$this->identicalTo($request) |
217
|
|
|
)->will($this->returnCallback( |
218
|
|
|
function () { |
219
|
|
|
throw new AccessDeniedException("Lorem ipsum"); |
220
|
|
|
} |
221
|
|
|
)); |
222
|
|
|
|
223
|
|
|
$controller = new GenericServiceInvokeController( |
224
|
|
|
$this->controllerHelper, |
225
|
|
|
$this->argumentCompiler, |
226
|
|
|
$this->container, |
227
|
|
|
[ |
228
|
|
|
'service' => 'some_service', |
229
|
|
|
'method' => 'doFoo', |
230
|
|
|
'authorization-attributes' => 'bar' |
231
|
|
|
] |
232
|
|
|
); |
233
|
|
|
|
234
|
|
|
$controller->callService($request); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @test |
239
|
|
|
*/ |
240
|
|
|
public function shouldThrowExceptionIfServiceNotFound() |
241
|
|
|
{ |
242
|
|
|
$this->expectException(ErrorException::class); |
243
|
|
|
|
244
|
|
|
/** @var Request $request */ |
245
|
|
|
$request = $this->createMock(Request::class); |
246
|
|
|
|
247
|
|
|
$this->container->expects($this->once())->method('get')->with( |
|
|
|
|
248
|
|
|
$this->equalTo('some_service') |
249
|
|
|
)->willReturn(null); |
250
|
|
|
|
251
|
|
|
$controller = new GenericServiceInvokeController( |
252
|
|
|
$this->controllerHelper, |
253
|
|
|
$this->argumentCompiler, |
254
|
|
|
$this->container, |
255
|
|
|
[ |
256
|
|
|
'service' => 'some_service', |
257
|
|
|
'method' => 'doFoo', |
258
|
|
|
'arguments' => ['lorem' => 'ipsum'] |
259
|
|
|
] |
260
|
|
|
); |
261
|
|
|
|
262
|
|
|
$controller->callService($request); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @test |
267
|
|
|
*/ |
268
|
|
View Code Duplication |
public function shouldBeCallableByInvokingController() |
|
|
|
|
269
|
|
|
{ |
270
|
|
|
/** @var Request $request */ |
271
|
|
|
$request = $this->createMock(Request::class); |
272
|
|
|
|
273
|
|
|
/** @var SampleService $service */ |
274
|
|
|
$service = $this->createMock(SampleService::class); |
275
|
|
|
|
276
|
|
|
$service->expects($this->once())->method('doFoo')->with( |
|
|
|
|
277
|
|
|
$this->equalTo('lorem'), |
278
|
|
|
$this->equalTo('ipsum') |
279
|
|
|
); |
280
|
|
|
|
281
|
|
|
$this->container->expects($this->once())->method('get')->with( |
|
|
|
|
282
|
|
|
$this->equalTo('some_service') |
283
|
|
|
)->willReturn($service); |
284
|
|
|
|
285
|
|
|
$this->argumentCompiler->expects($this->once())->method('buildCallArguments')->with( |
|
|
|
|
286
|
|
|
$this->equalTo(new ReflectionMethod($service, 'doFoo')), |
287
|
|
|
$this->equalTo(['lorem' => 'ipsum']), |
288
|
|
|
$this->identicalTo($request) |
289
|
|
|
)->willReturn(['lorem', 'ipsum']); |
290
|
|
|
|
291
|
|
|
/** @var string $expectedResponseContent */ |
292
|
|
|
$expectedResponseContent = "Service call completed"; |
293
|
|
|
|
294
|
|
|
$controller = new GenericServiceInvokeController( |
295
|
|
|
$this->controllerHelper, |
296
|
|
|
$this->argumentCompiler, |
297
|
|
|
$this->container, |
298
|
|
|
[ |
299
|
|
|
'service' => 'some_service', |
300
|
|
|
'method' => 'doFoo', |
301
|
|
|
'arguments' => ['lorem' => 'ipsum'] |
302
|
|
|
] |
303
|
|
|
); |
304
|
|
|
|
305
|
|
|
$this->controllerHelper->method('getCurrentRequest')->willReturn($request); |
|
|
|
|
306
|
|
|
|
307
|
|
|
/** @var Response $actualResponse */ |
308
|
|
|
$actualResponse = $controller(); |
309
|
|
|
|
310
|
|
|
$this->assertEquals($expectedResponseContent, $actualResponse->getContent()); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @test |
315
|
|
|
*/ |
316
|
|
View Code Duplication |
public function shouldRejectCallWithoutRequest() |
|
|
|
|
317
|
|
|
{ |
318
|
|
|
$this->expectException(InvalidArgumentException::class); |
319
|
|
|
|
320
|
|
|
$controller = new GenericServiceInvokeController( |
321
|
|
|
$this->controllerHelper, |
322
|
|
|
$this->argumentCompiler, |
323
|
|
|
$this->container, |
324
|
|
|
[ |
325
|
|
|
'service' => 'some_service', |
326
|
|
|
'method' => 'doFoo', |
327
|
|
|
] |
328
|
|
|
); |
329
|
|
|
|
330
|
|
|
$this->controllerHelper->method('getCurrentRequest')->willReturn(null); |
|
|
|
|
331
|
|
|
|
332
|
|
|
$controller(); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
} |
336
|
|
|
|
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..