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 Addiks\SymfonyGenerics\Controllers\API\GenericEntityCreateController; |
15
|
|
|
use Addiks\SymfonyGenerics\Services\ArgumentCompilerInterface; |
16
|
|
|
use Addiks\SymfonyGenerics\Controllers\ControllerHelperInterface; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use Psr\Container\ContainerInterface; |
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
20
|
|
|
use Addiks\SymfonyGenerics\Tests\Unit\Controllers\SampleEntity; |
21
|
|
|
use Webmozart\Assert\Assert; |
22
|
|
|
use Serializable; |
23
|
|
|
use stdClass; |
24
|
|
|
use InvalidArgumentException; |
25
|
|
|
use Symfony\Component\Finder\Exception\AccessDeniedException; |
26
|
|
|
use ReflectionException; |
27
|
|
|
use ErrorException; |
28
|
|
|
|
29
|
|
|
final class GenericEntityCreateControllerTest extends TestCase |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ControllerHelperInterface |
34
|
|
|
*/ |
35
|
|
|
private $controllerHelper; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var ArgumentCompilerInterface |
39
|
|
|
*/ |
40
|
|
|
private $argumentBuilder; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var ContainerInterface |
44
|
|
|
*/ |
45
|
|
|
private $container; |
46
|
|
|
|
47
|
|
|
public function setUp() |
48
|
|
|
{ |
49
|
|
|
$this->controllerHelper = $this->createMock(ControllerHelperInterface::class); |
|
|
|
|
50
|
|
|
$this->container = $this->createMock(ContainerInterface::class); |
|
|
|
|
51
|
|
|
$this->argumentBuilder = $this->createMock(ArgumentCompilerInterface::class); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @test |
56
|
|
|
*/ |
57
|
|
|
public function shouldCreateAnEntity() |
58
|
|
|
{ |
59
|
|
|
$controller = new GenericEntityCreateController( |
60
|
|
|
$this->controllerHelper, |
61
|
|
|
$this->argumentBuilder, |
62
|
|
|
$this->container, |
63
|
|
|
[ |
64
|
|
|
'entity-class' => SampleEntity::class |
65
|
|
|
] |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
$this->controllerHelper->expects($this->once())->method('flushORM'); |
|
|
|
|
69
|
|
|
$this->controllerHelper->expects($this->once())->method('persistEntity')->with( |
|
|
|
|
70
|
|
|
$this->equalTo(new SampleEntity()) |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
/** @var Request $request */ |
74
|
|
|
$request = $this->createMock(Request::class); |
75
|
|
|
|
76
|
|
|
/** @var Response $response */ |
77
|
|
|
$response = $controller->createEntity($request); |
78
|
|
|
|
79
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @test |
84
|
|
|
*/ |
85
|
|
|
public function shouldRejectNonExistingEntityClass() |
86
|
|
|
{ |
87
|
|
|
$this->expectException(InvalidArgumentException::class); |
88
|
|
|
|
89
|
|
|
$controller = new GenericEntityCreateController( |
|
|
|
|
90
|
|
|
$this->controllerHelper, |
91
|
|
|
$this->argumentBuilder, |
92
|
|
|
$this->container, |
93
|
|
|
[ |
94
|
|
|
'entity-class' => "\\ClassDoes\\NotExist" |
95
|
|
|
] |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @test |
101
|
|
|
*/ |
102
|
|
|
public function shouldRejectMissingEntityClass() |
103
|
|
|
{ |
104
|
|
|
$this->expectException(InvalidArgumentException::class); |
105
|
|
|
|
106
|
|
|
$controller = new GenericEntityCreateController( |
|
|
|
|
107
|
|
|
$this->controllerHelper, |
108
|
|
|
$this->argumentBuilder, |
109
|
|
|
$this->container, |
110
|
|
|
[ |
111
|
|
|
] |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @test |
117
|
|
|
*/ |
118
|
|
|
public function shouldRejectControllerCalledAgain() |
119
|
|
|
{ |
120
|
|
|
$controller = new GenericEntityCreateController( |
121
|
|
|
$this->controllerHelper, |
122
|
|
|
$this->argumentBuilder, |
123
|
|
|
$this->container, |
124
|
|
|
[ |
125
|
|
|
'entity-class' => SampleEntity::class |
126
|
|
|
] |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
$this->expectException(InvalidArgumentException::class); |
130
|
|
|
|
131
|
|
|
$controller->__construct( |
132
|
|
|
$this->controllerHelper, |
133
|
|
|
$this->argumentBuilder, |
134
|
|
|
$this->container, |
135
|
|
|
[ |
136
|
|
|
'entity-class' => SampleEntity::class |
137
|
|
|
] |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @test |
143
|
|
|
*/ |
144
|
|
|
public function shouldProvideConstructArguments() |
145
|
|
|
{ |
146
|
|
|
$controller = new GenericEntityCreateController( |
147
|
|
|
$this->controllerHelper, |
148
|
|
|
$this->argumentBuilder, |
149
|
|
|
$this->container, |
150
|
|
|
[ |
151
|
|
|
'entity-class' => SampleEntity::class, |
152
|
|
|
'arguments' => [ |
153
|
|
|
'foo' => 'bar' |
154
|
|
|
] |
155
|
|
|
] |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
/** @var SampleEntity|null $persistedEntity */ |
159
|
|
|
$persistedEntity = null; |
160
|
|
|
|
161
|
|
|
$this->controllerHelper->expects($this->once())->method('flushORM'); |
|
|
|
|
162
|
|
|
$this->controllerHelper->method('persistEntity')->will($this->returnCallback( |
|
|
|
|
163
|
|
|
function (SampleEntity $entity) use (&$persistedEntity) { |
164
|
|
|
$persistedEntity = $entity; |
165
|
|
|
} |
166
|
|
|
)); |
167
|
|
|
|
168
|
|
|
$this->argumentBuilder->method('buildCallArguments')->willReturn([ |
|
|
|
|
169
|
|
|
'bar' |
170
|
|
|
]); |
171
|
|
|
|
172
|
|
|
/** @var Request $request */ |
173
|
|
|
$request = $this->createMock(Request::class); |
174
|
|
|
|
175
|
|
|
/** @var Response $response */ |
176
|
|
|
$response = $controller->createEntity($request); |
|
|
|
|
177
|
|
|
|
178
|
|
|
$this->assertEquals('bar', $persistedEntity->constructArgument); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @test |
183
|
|
|
*/ |
184
|
|
|
public function shouldExecuteACall() |
185
|
|
|
{ |
186
|
|
|
$controller = new GenericEntityCreateController( |
187
|
|
|
$this->controllerHelper, |
188
|
|
|
$this->argumentBuilder, |
189
|
|
|
$this->container, |
190
|
|
|
[ |
191
|
|
|
'entity-class' => SampleEntity::class, |
192
|
|
|
'calls' => [ |
193
|
|
|
'doFoo' => [] |
194
|
|
|
] |
195
|
|
|
] |
196
|
|
|
); |
197
|
|
|
|
198
|
|
|
/** @var SampleEntity|null $persistedEntity */ |
199
|
|
|
$persistedEntity = null; |
200
|
|
|
|
201
|
|
|
$this->controllerHelper->method('persistEntity')->will($this->returnCallback( |
|
|
|
|
202
|
|
|
function (SampleEntity $entity) use (&$persistedEntity) { |
203
|
|
|
$persistedEntity = $entity; |
204
|
|
|
} |
205
|
|
|
)); |
206
|
|
|
|
207
|
|
|
/** @var Request $request */ |
208
|
|
|
$request = $this->createMock(Request::class); |
209
|
|
|
|
210
|
|
|
$this->assertNull($persistedEntity); |
211
|
|
|
|
212
|
|
|
$controller->createEntity($request); |
213
|
|
|
|
214
|
|
|
$this->assertTrue($persistedEntity instanceof SampleEntity); |
215
|
|
|
$this->assertTrue($persistedEntity->fooCalled); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @test |
220
|
|
|
*/ |
221
|
|
|
public function shouldRejectCallToNonExistingMethod() |
222
|
|
|
{ |
223
|
|
|
$this->expectException(InvalidArgumentException::class); |
224
|
|
|
|
225
|
|
|
$controller = new GenericEntityCreateController( |
|
|
|
|
226
|
|
|
$this->controllerHelper, |
227
|
|
|
$this->argumentBuilder, |
228
|
|
|
$this->container, |
229
|
|
|
[ |
230
|
|
|
'entity-class' => SampleEntity::class, |
231
|
|
|
'calls' => [ |
232
|
|
|
'doNonExistingThing' => [] |
233
|
|
|
] |
234
|
|
|
] |
235
|
|
|
); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @test |
240
|
|
|
*/ |
241
|
|
|
public function shouldRejectCallWithNonArrayParameters() |
242
|
|
|
{ |
243
|
|
|
$this->expectException(InvalidArgumentException::class); |
244
|
|
|
|
245
|
|
|
$controller = new GenericEntityCreateController( |
|
|
|
|
246
|
|
|
$this->controllerHelper, |
247
|
|
|
$this->argumentBuilder, |
248
|
|
|
$this->container, |
249
|
|
|
[ |
250
|
|
|
'entity-class' => SampleEntity::class, |
251
|
|
|
'calls' => [ |
252
|
|
|
'doFoo' => "notAnArray" |
253
|
|
|
] |
254
|
|
|
] |
255
|
|
|
); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @test |
260
|
|
|
*/ |
261
|
|
|
public function shouldRejectCallWithIntegerMethod() |
262
|
|
|
{ |
263
|
|
|
$this->expectException(InvalidArgumentException::class); |
264
|
|
|
|
265
|
|
|
$controller = new GenericEntityCreateController( |
|
|
|
|
266
|
|
|
$this->controllerHelper, |
267
|
|
|
$this->argumentBuilder, |
268
|
|
|
$this->container, |
269
|
|
|
[ |
270
|
|
|
'entity-class' => SampleEntity::class, |
271
|
|
|
'calls' => [ |
272
|
|
|
0 => [] |
273
|
|
|
] |
274
|
|
|
] |
275
|
|
|
); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @test |
280
|
|
|
*/ |
281
|
|
|
public function shouldUseFactoryObject() |
282
|
|
|
{ |
283
|
|
|
$controller = new GenericEntityCreateController( |
284
|
|
|
$this->controllerHelper, |
285
|
|
|
$this->argumentBuilder, |
286
|
|
|
$this->container, |
287
|
|
|
[ |
288
|
|
|
'entity-class' => SampleEntity::class, |
289
|
|
|
'factory' => '@some_factory_service::serialize' |
290
|
|
|
] |
291
|
|
|
); |
292
|
|
|
|
293
|
|
|
$expectedEntity = new SampleEntity(); |
294
|
|
|
|
295
|
|
|
/** @var SampleEntity|null $actualEntity */ |
296
|
|
|
$actualEntity = null; |
297
|
|
|
|
298
|
|
|
/** @var Serializable $factoryMock */ |
299
|
|
|
$factoryMock = $this->createMock(Serializable::class); |
300
|
|
|
$factoryMock->method("serialize")->willReturn($expectedEntity); |
301
|
|
|
|
302
|
|
|
$this->container->expects($this->once())->method('get')->with( |
|
|
|
|
303
|
|
|
$this->equalTo('some_factory_service') |
304
|
|
|
)->willReturn($factoryMock); |
305
|
|
|
|
306
|
|
|
$this->controllerHelper->method('persistEntity')->will($this->returnCallback( |
|
|
|
|
307
|
|
|
function (SampleEntity $entity) use (&$actualEntity) { |
308
|
|
|
$actualEntity = $entity; |
309
|
|
|
} |
310
|
|
|
)); |
311
|
|
|
|
312
|
|
|
/** @var Request $request */ |
313
|
|
|
$request = $this->createMock(Request::class); |
314
|
|
|
|
315
|
|
|
$controller->createEntity($request); |
316
|
|
|
|
317
|
|
|
$this->assertSame($expectedEntity, $actualEntity); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* @test |
322
|
|
|
*/ |
323
|
|
|
public function shouldRejectWrongEntityCreated() |
324
|
|
|
{ |
325
|
|
|
$controller = new GenericEntityCreateController( |
326
|
|
|
$this->controllerHelper, |
327
|
|
|
$this->argumentBuilder, |
328
|
|
|
$this->container, |
329
|
|
|
[ |
330
|
|
|
'entity-class' => SampleEntity::class, |
331
|
|
|
'factory' => '@some_factory_service::serialize' |
332
|
|
|
] |
333
|
|
|
); |
334
|
|
|
|
335
|
|
|
/** @var Serializable $factoryMock */ |
336
|
|
|
$factoryMock = $this->createMock(Serializable::class); |
337
|
|
|
$factoryMock->method("serialize")->willReturn(new stdClass()); |
338
|
|
|
|
339
|
|
|
$this->container->expects($this->once())->method('get')->with( |
|
|
|
|
340
|
|
|
$this->equalTo('some_factory_service') |
341
|
|
|
)->willReturn($factoryMock); |
342
|
|
|
|
343
|
|
|
/** @var Request $request */ |
344
|
|
|
$request = $this->createMock(Request::class); |
345
|
|
|
|
346
|
|
|
$this->expectException(InvalidArgumentException::class); |
347
|
|
|
|
348
|
|
|
$controller->createEntity($request); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @test |
353
|
|
|
*/ |
354
|
|
View Code Duplication |
public function shouldRejectNonExistingFactory() |
|
|
|
|
355
|
|
|
{ |
356
|
|
|
$controller = new GenericEntityCreateController( |
357
|
|
|
$this->controllerHelper, |
358
|
|
|
$this->argumentBuilder, |
359
|
|
|
$this->container, |
360
|
|
|
[ |
361
|
|
|
'entity-class' => SampleEntity::class, |
362
|
|
|
'factory' => '@some_factory_service::serialize' |
363
|
|
|
] |
364
|
|
|
); |
365
|
|
|
|
366
|
|
|
$this->container->expects($this->once())->method('get')->with( |
|
|
|
|
367
|
|
|
$this->equalTo('some_factory_service') |
368
|
|
|
)->willReturn(null); |
369
|
|
|
|
370
|
|
|
/** @var Request $request */ |
371
|
|
|
$request = $this->createMock(Request::class); |
372
|
|
|
|
373
|
|
|
$this->expectException(InvalidArgumentException::class); |
374
|
|
|
$this->expectExceptionMessage("Did not find service 'some_factory_service'!"); |
375
|
|
|
|
376
|
|
|
$controller->createEntity($request); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* @test |
381
|
|
|
*/ |
382
|
|
|
public function shouldRejectInvalidFactory() |
383
|
|
|
{ |
384
|
|
|
$controller = new GenericEntityCreateController( |
385
|
|
|
$this->controllerHelper, |
386
|
|
|
$this->argumentBuilder, |
387
|
|
|
$this->container, |
388
|
|
|
[ |
389
|
|
|
'entity-class' => SampleEntity::class, |
390
|
|
|
'factory' => '::serialize' |
391
|
|
|
] |
392
|
|
|
); |
393
|
|
|
|
394
|
|
|
/** @var Request $request */ |
395
|
|
|
$request = $this->createMock(Request::class); |
396
|
|
|
|
397
|
|
|
$this->expectException(ErrorException::class); |
398
|
|
|
|
399
|
|
|
$controller->createEntity($request); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* @test |
404
|
|
|
*/ |
405
|
|
View Code Duplication |
public function shouldRejectNonExistingFactoryMethod() |
|
|
|
|
406
|
|
|
{ |
407
|
|
|
$controller = new GenericEntityCreateController( |
408
|
|
|
$this->controllerHelper, |
409
|
|
|
$this->argumentBuilder, |
410
|
|
|
$this->container, |
411
|
|
|
[ |
412
|
|
|
'entity-class' => SampleEntity::class, |
413
|
|
|
'factory' => '@some_factory_service::MethodDoesNotExist' |
414
|
|
|
] |
415
|
|
|
); |
416
|
|
|
|
417
|
|
|
/** @var Serializable $factoryMock */ |
418
|
|
|
$factoryMock = $this->createMock(Serializable::class); |
419
|
|
|
|
420
|
|
|
$this->container->expects($this->once())->method('get')->with( |
|
|
|
|
421
|
|
|
$this->equalTo('some_factory_service') |
422
|
|
|
)->willReturn($factoryMock); |
423
|
|
|
|
424
|
|
|
/** @var Request $request */ |
425
|
|
|
$request = $this->createMock(Request::class); |
426
|
|
|
|
427
|
|
|
$this->expectException(ReflectionException::class); |
428
|
|
|
|
429
|
|
|
$controller->createEntity($request); |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* @test |
434
|
|
|
*/ |
435
|
|
|
public function shouldCorrectlyDetectFactoryMethod() |
436
|
|
|
{ |
437
|
|
|
$controller = new GenericEntityCreateController( |
438
|
|
|
$this->controllerHelper, |
439
|
|
|
$this->argumentBuilder, |
440
|
|
|
$this->container, |
441
|
|
|
[ |
442
|
|
|
'entity-class' => SampleEntity::class, |
443
|
|
|
'factory' => '@some_factory_service::serialize::thisShouldCauseAnError' |
444
|
|
|
] |
445
|
|
|
); |
446
|
|
|
|
447
|
|
|
/** @var Serializable $factoryMock */ |
448
|
|
|
$factoryMock = $this->createMock(Serializable::class); |
449
|
|
|
$factoryMock->method("serialize")->willReturn(new SampleEntity()); |
450
|
|
|
|
451
|
|
|
$this->container->expects($this->once())->method('get')->with( |
|
|
|
|
452
|
|
|
$this->equalTo('some_factory_service') |
453
|
|
|
)->willReturn($factoryMock); |
454
|
|
|
|
455
|
|
|
/** @var Request $request */ |
456
|
|
|
$request = $this->createMock(Request::class); |
457
|
|
|
|
458
|
|
|
$this->expectException(ReflectionException::class); |
459
|
|
|
|
460
|
|
|
$controller->createEntity($request); |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
/** |
464
|
|
|
* @test |
465
|
|
|
*/ |
466
|
|
|
public function shouldCheckIfAccessIsGranted() |
467
|
|
|
{ |
468
|
|
|
$this->expectException(AccessDeniedException::class); |
469
|
|
|
|
470
|
|
|
$this->controllerHelper->expects($this->exactly(2))->method('denyAccessUnlessGranted')->will($this->returnCallback( |
|
|
|
|
471
|
|
|
function ($attribute, $object) { |
472
|
|
|
if ($object instanceof SampleEntity) { |
473
|
|
|
throw new AccessDeniedException('Lorem ipsum!'); |
474
|
|
|
} |
475
|
|
|
} |
476
|
|
|
)); |
477
|
|
|
|
478
|
|
|
/** @var Serializable $factoryMock */ |
479
|
|
|
$factoryMock = $this->createMock(Serializable::class); |
480
|
|
|
$factoryMock->method("serialize")->willReturn(new SampleEntity()); |
481
|
|
|
|
482
|
|
|
$this->container->expects($this->once())->method('get')->with( |
|
|
|
|
483
|
|
|
$this->equalTo('some_factory_service') |
484
|
|
|
)->willReturn($factoryMock); |
485
|
|
|
|
486
|
|
|
$controller = new GenericEntityCreateController( |
487
|
|
|
$this->controllerHelper, |
488
|
|
|
$this->argumentBuilder, |
489
|
|
|
$this->container, |
490
|
|
|
[ |
491
|
|
|
'entity-class' => SampleEntity::class, |
492
|
|
|
'factory' => '@some_factory_service::serialize', |
493
|
|
|
'authorization-attribute' => 'some-attribute', |
494
|
|
|
] |
495
|
|
|
); |
496
|
|
|
|
497
|
|
|
/** @var Request $request */ |
498
|
|
|
$request = $this->createMock(Request::class); |
499
|
|
|
|
500
|
|
|
$controller->createEntity($request); |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
} |
504
|
|
|
|
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..