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; |
12
|
|
|
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use Addiks\SymfonyGenerics\Controllers\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
|
|
|
|
27
|
|
|
final class GenericEntityCreateControllerTest extends TestCase |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ControllerHelperInterface |
32
|
|
|
*/ |
33
|
|
|
private $controllerHelper; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ArgumentCompilerInterface |
37
|
|
|
*/ |
38
|
|
|
private $argumentBuilder; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var ContainerInterface |
42
|
|
|
*/ |
43
|
|
|
private $container; |
44
|
|
|
|
45
|
|
|
public function setUp() |
46
|
|
|
{ |
47
|
|
|
$this->controllerHelper = $this->createMock(ControllerHelperInterface::class); |
|
|
|
|
48
|
|
|
$this->container = $this->createMock(ContainerInterface::class); |
|
|
|
|
49
|
|
|
$this->argumentBuilder = $this->createMock(ArgumentCompilerInterface::class); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @test |
54
|
|
|
*/ |
55
|
|
|
public function shouldCreateAnEntity() |
56
|
|
|
{ |
57
|
|
|
$controller = new GenericEntityCreateController( |
58
|
|
|
$this->controllerHelper, |
59
|
|
|
$this->argumentBuilder, |
60
|
|
|
$this->container, |
61
|
|
|
[ |
62
|
|
|
'entity-class' => SampleEntity::class |
63
|
|
|
] |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
$this->controllerHelper->expects($this->once())->method('flushORM'); |
|
|
|
|
67
|
|
|
$this->controllerHelper->expects($this->once())->method('persistEntity')->with( |
|
|
|
|
68
|
|
|
$this->equalTo(new SampleEntity()) |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
/** @var Request $request */ |
72
|
|
|
$request = $this->createMock(Request::class); |
73
|
|
|
|
74
|
|
|
/** @var Response $response */ |
75
|
|
|
$response = $controller->createEntity($request); |
76
|
|
|
|
77
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @test |
82
|
|
|
*/ |
83
|
|
|
public function shouldRejectNonExistingEntityClass() |
84
|
|
|
{ |
85
|
|
|
$this->expectException(InvalidArgumentException::class); |
86
|
|
|
|
87
|
|
|
$controller = new GenericEntityCreateController( |
|
|
|
|
88
|
|
|
$this->controllerHelper, |
89
|
|
|
$this->argumentBuilder, |
90
|
|
|
$this->container, |
91
|
|
|
[ |
92
|
|
|
'entity-class' => "\\ClassDoes\\NotExist" |
93
|
|
|
] |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @test |
99
|
|
|
*/ |
100
|
|
|
public function shouldRejectMissingEntityClass() |
101
|
|
|
{ |
102
|
|
|
$this->expectException(InvalidArgumentException::class); |
103
|
|
|
|
104
|
|
|
$controller = new GenericEntityCreateController( |
|
|
|
|
105
|
|
|
$this->controllerHelper, |
106
|
|
|
$this->argumentBuilder, |
107
|
|
|
$this->container, |
108
|
|
|
[ |
109
|
|
|
] |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @test |
115
|
|
|
*/ |
116
|
|
|
public function shouldRejectControllerCalledAgain() |
117
|
|
|
{ |
118
|
|
|
$controller = new GenericEntityCreateController( |
119
|
|
|
$this->controllerHelper, |
120
|
|
|
$this->argumentBuilder, |
121
|
|
|
$this->container, |
122
|
|
|
[ |
123
|
|
|
'entity-class' => SampleEntity::class |
124
|
|
|
] |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
$this->expectException(InvalidArgumentException::class); |
128
|
|
|
|
129
|
|
|
$controller->__construct( |
130
|
|
|
$this->controllerHelper, |
131
|
|
|
$this->argumentBuilder, |
132
|
|
|
$this->container, |
133
|
|
|
[ |
134
|
|
|
'entity-class' => SampleEntity::class |
135
|
|
|
] |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @test |
141
|
|
|
*/ |
142
|
|
|
public function shouldProvideConstructArguments() |
143
|
|
|
{ |
144
|
|
|
$controller = new GenericEntityCreateController( |
145
|
|
|
$this->controllerHelper, |
146
|
|
|
$this->argumentBuilder, |
147
|
|
|
$this->container, |
148
|
|
|
[ |
149
|
|
|
'entity-class' => SampleEntity::class, |
150
|
|
|
'calls' => [ |
151
|
|
|
'construct' => [ |
152
|
|
|
'foo' => 'bar' |
153
|
|
|
] |
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
|
|
375
|
|
|
$controller->createEntity($request); |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* @test |
380
|
|
|
*/ |
381
|
|
View Code Duplication |
public function shouldRejectInvalidFactory() |
|
|
|
|
382
|
|
|
{ |
383
|
|
|
$controller = new GenericEntityCreateController( |
384
|
|
|
$this->controllerHelper, |
385
|
|
|
$this->argumentBuilder, |
386
|
|
|
$this->container, |
387
|
|
|
[ |
388
|
|
|
'entity-class' => SampleEntity::class, |
389
|
|
|
'factory' => '::serialize' |
390
|
|
|
] |
391
|
|
|
); |
392
|
|
|
|
393
|
|
|
/** @var Request $request */ |
394
|
|
|
$request = $this->createMock(Request::class); |
395
|
|
|
|
396
|
|
|
$this->expectException(InvalidArgumentException::class); |
397
|
|
|
|
398
|
|
|
$controller->createEntity($request); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* @test |
403
|
|
|
*/ |
404
|
|
View Code Duplication |
public function shouldRejectNonExistingFactoryMethod() |
|
|
|
|
405
|
|
|
{ |
406
|
|
|
$controller = new GenericEntityCreateController( |
407
|
|
|
$this->controllerHelper, |
408
|
|
|
$this->argumentBuilder, |
409
|
|
|
$this->container, |
410
|
|
|
[ |
411
|
|
|
'entity-class' => SampleEntity::class, |
412
|
|
|
'factory' => '@some_factory_service::MethodDoesNotExist' |
413
|
|
|
] |
414
|
|
|
); |
415
|
|
|
|
416
|
|
|
/** @var Serializable $factoryMock */ |
417
|
|
|
$factoryMock = $this->createMock(Serializable::class); |
418
|
|
|
|
419
|
|
|
$this->container->expects($this->once())->method('get')->with( |
|
|
|
|
420
|
|
|
$this->equalTo('some_factory_service') |
421
|
|
|
)->willReturn($factoryMock); |
422
|
|
|
|
423
|
|
|
/** @var Request $request */ |
424
|
|
|
$request = $this->createMock(Request::class); |
425
|
|
|
|
426
|
|
|
$this->expectException(InvalidArgumentException::class); |
427
|
|
|
|
428
|
|
|
$controller->createEntity($request); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* @test |
433
|
|
|
*/ |
434
|
|
|
public function shouldCorrectlyDetectFactoryMethod() |
435
|
|
|
{ |
436
|
|
|
$controller = new GenericEntityCreateController( |
437
|
|
|
$this->controllerHelper, |
438
|
|
|
$this->argumentBuilder, |
439
|
|
|
$this->container, |
440
|
|
|
[ |
441
|
|
|
'entity-class' => SampleEntity::class, |
442
|
|
|
'factory' => '@some_factory_service::serialize::thisShouldCauseAnError' |
443
|
|
|
] |
444
|
|
|
); |
445
|
|
|
|
446
|
|
|
/** @var Serializable $factoryMock */ |
447
|
|
|
$factoryMock = $this->createMock(Serializable::class); |
448
|
|
|
$factoryMock->method("serialize")->willReturn(new SampleEntity()); |
449
|
|
|
|
450
|
|
|
$this->container->expects($this->once())->method('get')->with( |
|
|
|
|
451
|
|
|
$this->equalTo('some_factory_service') |
452
|
|
|
)->willReturn($factoryMock); |
453
|
|
|
|
454
|
|
|
/** @var Request $request */ |
455
|
|
|
$request = $this->createMock(Request::class); |
456
|
|
|
|
457
|
|
|
$this->expectException(InvalidArgumentException::class); |
458
|
|
|
|
459
|
|
|
$controller->createEntity($request); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* @test |
464
|
|
|
*/ |
465
|
|
|
public function shouldCheckIfAccessIsGranted() |
466
|
|
|
{ |
467
|
|
|
$this->expectException(AccessDeniedException::class); |
468
|
|
|
|
469
|
|
|
$this->controllerHelper->expects($this->once())->method('denyAccessUnlessGranted')->will($this->returnCallback( |
|
|
|
|
470
|
|
|
function () { |
471
|
|
|
throw new AccessDeniedException('Lorem ipsum!'); |
472
|
|
|
} |
473
|
|
|
)); |
474
|
|
|
|
475
|
|
|
/** @var Serializable $factoryMock */ |
476
|
|
|
$factoryMock = $this->createMock(Serializable::class); |
477
|
|
|
$factoryMock->method("serialize")->willReturn(new SampleEntity()); |
478
|
|
|
|
479
|
|
|
$this->container->expects($this->once())->method('get')->with( |
|
|
|
|
480
|
|
|
$this->equalTo('some_factory_service') |
481
|
|
|
)->willReturn($factoryMock); |
482
|
|
|
|
483
|
|
|
/** @var mixed $controller */ |
484
|
|
|
$controller = new GenericEntityCreateController( |
485
|
|
|
$this->controllerHelper, |
486
|
|
|
$this->argumentBuilder, |
487
|
|
|
$this->container, |
488
|
|
|
[ |
489
|
|
|
'entity-class' => SampleEntity::class, |
490
|
|
|
'factory' => '@some_factory_service::serialize', |
491
|
|
|
'authorization-attribute' => 'some-attribute', |
492
|
|
|
] |
493
|
|
|
); |
494
|
|
|
|
495
|
|
|
/** @var Request $request */ |
496
|
|
|
$request = $this->createMock(Request::class); |
497
|
|
|
|
498
|
|
|
$controller->createEntity($request); |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
} |
502
|
|
|
|
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..