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 Serializable; |
22
|
|
|
use stdClass; |
23
|
|
|
use InvalidArgumentException; |
24
|
|
|
use Symfony\Component\Finder\Exception\AccessDeniedException; |
25
|
|
|
use ReflectionException; |
26
|
|
|
use ErrorException; |
27
|
|
|
use Addiks\SymfonyGenerics\Events\EntityInteractionEvent; |
28
|
|
|
use Addiks\SymfonyGenerics\Tests\Unit\Controllers\SampleEntityWithoutConstructor; |
29
|
|
|
|
30
|
|
|
final class GenericEntityCreateControllerTest extends TestCase |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ControllerHelperInterface |
35
|
|
|
*/ |
36
|
|
|
private $controllerHelper; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ArgumentCompilerInterface |
40
|
|
|
*/ |
41
|
|
|
private $argumentBuilder; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var ContainerInterface |
45
|
|
|
*/ |
46
|
|
|
private $container; |
47
|
|
|
|
48
|
|
|
public function setUp() |
49
|
|
|
{ |
50
|
|
|
$this->controllerHelper = $this->createMock(ControllerHelperInterface::class); |
|
|
|
|
51
|
|
|
$this->container = $this->createMock(ContainerInterface::class); |
|
|
|
|
52
|
|
|
$this->argumentBuilder = $this->createMock(ArgumentCompilerInterface::class); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @test |
57
|
|
|
*/ |
58
|
|
|
public function shouldCreateAnEntity() |
59
|
|
|
{ |
60
|
|
|
$controller = new GenericEntityCreateController( |
61
|
|
|
$this->controllerHelper, |
62
|
|
|
$this->argumentBuilder, |
63
|
|
|
$this->container, |
64
|
|
|
[ |
65
|
|
|
'entity-class' => SampleEntity::class |
66
|
|
|
] |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$this->controllerHelper->expects($this->once())->method('flushORM'); |
|
|
|
|
70
|
|
|
$this->controllerHelper->expects($this->once())->method('dispatchEvent')->with( |
|
|
|
|
71
|
|
|
$this->equalTo('symfony_generics.entity_interaction'), |
72
|
|
|
$this->equalTo(new EntityInteractionEvent( |
73
|
|
|
SampleEntity::class, |
74
|
|
|
null, # TODO: get id via reflection |
75
|
|
|
new SampleEntity(), |
76
|
|
|
"__construct", |
77
|
|
|
[] |
78
|
|
|
)) |
79
|
|
|
); |
80
|
|
|
$this->controllerHelper->expects($this->once())->method('persistEntity')->with( |
|
|
|
|
81
|
|
|
$this->equalTo(new SampleEntity()) |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
/** @var Request $request */ |
85
|
|
|
$request = $this->createMock(Request::class); |
86
|
|
|
|
87
|
|
|
/** @var Response $response */ |
88
|
|
|
$response = $controller->createEntity($request); |
89
|
|
|
|
90
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @test |
95
|
|
|
*/ |
96
|
|
|
public function shouldRedirectWhenNeeded() |
97
|
|
|
{ |
98
|
|
|
$controller = new GenericEntityCreateController( |
99
|
|
|
$this->controllerHelper, |
100
|
|
|
$this->argumentBuilder, |
101
|
|
|
$this->container, |
102
|
|
|
[ |
103
|
|
|
'entity-class' => SampleEntity::class, |
104
|
|
|
'success-redirect' => 'some_route' |
105
|
|
|
] |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
$this->controllerHelper->expects($this->once())->method('flushORM'); |
|
|
|
|
109
|
|
|
$this->controllerHelper->expects($this->once())->method('persistEntity')->with( |
|
|
|
|
110
|
|
|
$this->equalTo(new SampleEntity()) |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
$expectedResponse = new Response(); |
114
|
|
|
|
115
|
|
|
$this->controllerHelper->expects($this->once())->method('redirectToRoute')->with( |
|
|
|
|
116
|
|
|
$this->equalTo('some_route'), |
117
|
|
|
$this->equalTo([ |
118
|
|
|
'entityId' => 'some_id' |
119
|
|
|
]), |
120
|
|
|
$this->equalTo(303) |
121
|
|
|
)->willReturn($expectedResponse); |
122
|
|
|
|
123
|
|
|
/** @var Request $request */ |
124
|
|
|
$request = $this->createMock(Request::class); |
125
|
|
|
|
126
|
|
|
/** @var Response $actualResponse */ |
127
|
|
|
$actualResponse = $controller->createEntity($request); |
128
|
|
|
|
129
|
|
|
$this->assertSame($expectedResponse, $actualResponse); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @test |
134
|
|
|
*/ |
135
|
|
|
public function shouldRejectMissingEntityClass() |
136
|
|
|
{ |
137
|
|
|
$this->expectException(InvalidArgumentException::class); |
138
|
|
|
|
139
|
|
|
$controller = new GenericEntityCreateController( |
|
|
|
|
140
|
|
|
$this->controllerHelper, |
141
|
|
|
$this->argumentBuilder, |
142
|
|
|
$this->container, |
143
|
|
|
[ |
144
|
|
|
] |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @test |
150
|
|
|
*/ |
151
|
|
|
public function shouldRejectControllerCalledAgain() |
152
|
|
|
{ |
153
|
|
|
$controller = new GenericEntityCreateController( |
154
|
|
|
$this->controllerHelper, |
155
|
|
|
$this->argumentBuilder, |
156
|
|
|
$this->container, |
157
|
|
|
[ |
158
|
|
|
'entity-class' => SampleEntity::class |
159
|
|
|
] |
160
|
|
|
); |
161
|
|
|
|
162
|
|
|
$this->expectException(InvalidArgumentException::class); |
163
|
|
|
|
164
|
|
|
$controller->__construct( |
165
|
|
|
$this->controllerHelper, |
166
|
|
|
$this->argumentBuilder, |
167
|
|
|
$this->container, |
168
|
|
|
[ |
169
|
|
|
'entity-class' => SampleEntity::class |
170
|
|
|
] |
171
|
|
|
); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @test |
176
|
|
|
*/ |
177
|
|
|
public function shouldProvideConstructArguments() |
178
|
|
|
{ |
179
|
|
|
$controller = new GenericEntityCreateController( |
180
|
|
|
$this->controllerHelper, |
181
|
|
|
$this->argumentBuilder, |
182
|
|
|
$this->container, |
183
|
|
|
[ |
184
|
|
|
'entity-class' => SampleEntity::class, |
185
|
|
|
'arguments' => [ |
186
|
|
|
'foo' => 'bar' |
187
|
|
|
] |
188
|
|
|
] |
189
|
|
|
); |
190
|
|
|
|
191
|
|
|
/** @var SampleEntity|null $persistedEntity */ |
192
|
|
|
$persistedEntity = null; |
193
|
|
|
|
194
|
|
|
$this->controllerHelper->expects($this->once())->method('flushORM'); |
|
|
|
|
195
|
|
|
$this->controllerHelper->method('persistEntity')->will($this->returnCallback( |
|
|
|
|
196
|
|
|
function (SampleEntity $entity) use (&$persistedEntity) { |
197
|
|
|
$persistedEntity = $entity; |
198
|
|
|
} |
199
|
|
|
)); |
200
|
|
|
|
201
|
|
|
$this->argumentBuilder->method('buildCallArguments')->willReturn([ |
|
|
|
|
202
|
|
|
'bar' |
203
|
|
|
]); |
204
|
|
|
|
205
|
|
|
/** @var Request $request */ |
206
|
|
|
$request = $this->createMock(Request::class); |
207
|
|
|
|
208
|
|
|
/** @var Response $response */ |
209
|
|
|
$response = $controller->createEntity($request); |
|
|
|
|
210
|
|
|
|
211
|
|
|
$this->assertEquals('bar', $persistedEntity->constructArgument); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @test |
216
|
|
|
*/ |
217
|
|
|
public function shouldExecuteACall() |
218
|
|
|
{ |
219
|
|
|
$controller = new GenericEntityCreateController( |
220
|
|
|
$this->controllerHelper, |
221
|
|
|
$this->argumentBuilder, |
222
|
|
|
$this->container, |
223
|
|
|
[ |
224
|
|
|
'entity-class' => SampleEntity::class, |
225
|
|
|
'calls' => [ |
226
|
|
|
'doFoo' => [] |
227
|
|
|
] |
228
|
|
|
] |
229
|
|
|
); |
230
|
|
|
|
231
|
|
|
/** @var SampleEntity|null $persistedEntity */ |
232
|
|
|
$persistedEntity = null; |
233
|
|
|
|
234
|
|
|
$this->controllerHelper->method('persistEntity')->will($this->returnCallback( |
|
|
|
|
235
|
|
|
function (SampleEntity $entity) use (&$persistedEntity) { |
236
|
|
|
$persistedEntity = $entity; |
237
|
|
|
} |
238
|
|
|
)); |
239
|
|
|
|
240
|
|
|
/** @var Request $request */ |
241
|
|
|
$request = $this->createMock(Request::class); |
242
|
|
|
|
243
|
|
|
$this->assertNull($persistedEntity); |
244
|
|
|
|
245
|
|
|
$controller->createEntity($request); |
246
|
|
|
|
247
|
|
|
$this->assertTrue($persistedEntity instanceof SampleEntity); |
248
|
|
|
$this->assertTrue($persistedEntity->fooCalled); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @test |
253
|
|
|
*/ |
254
|
|
|
public function shouldRejectCallToNonExistingMethod() |
255
|
|
|
{ |
256
|
|
|
$this->expectException(InvalidArgumentException::class); |
257
|
|
|
|
258
|
|
|
$controller = new GenericEntityCreateController( |
|
|
|
|
259
|
|
|
$this->controllerHelper, |
260
|
|
|
$this->argumentBuilder, |
261
|
|
|
$this->container, |
262
|
|
|
[ |
263
|
|
|
'entity-class' => SampleEntity::class, |
264
|
|
|
'calls' => [ |
265
|
|
|
'doNonExistingThing' => [] |
266
|
|
|
] |
267
|
|
|
] |
268
|
|
|
); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @test |
273
|
|
|
*/ |
274
|
|
|
public function shouldRejectCallWithNonArrayParameters() |
275
|
|
|
{ |
276
|
|
|
$this->expectException(InvalidArgumentException::class); |
277
|
|
|
|
278
|
|
|
$controller = new GenericEntityCreateController( |
|
|
|
|
279
|
|
|
$this->controllerHelper, |
280
|
|
|
$this->argumentBuilder, |
281
|
|
|
$this->container, |
282
|
|
|
[ |
283
|
|
|
'entity-class' => SampleEntity::class, |
284
|
|
|
'calls' => [ |
285
|
|
|
'doFoo' => "notAnArray" |
286
|
|
|
] |
287
|
|
|
] |
288
|
|
|
); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @test |
293
|
|
|
*/ |
294
|
|
|
public function shouldRejectCallWithIntegerMethod() |
295
|
|
|
{ |
296
|
|
|
$this->expectException(InvalidArgumentException::class); |
297
|
|
|
|
298
|
|
|
$controller = new GenericEntityCreateController( |
|
|
|
|
299
|
|
|
$this->controllerHelper, |
300
|
|
|
$this->argumentBuilder, |
301
|
|
|
$this->container, |
302
|
|
|
[ |
303
|
|
|
'entity-class' => SampleEntity::class, |
304
|
|
|
'calls' => [ |
305
|
|
|
0 => [] |
306
|
|
|
] |
307
|
|
|
] |
308
|
|
|
); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @test |
313
|
|
|
*/ |
314
|
|
|
public function shouldUseFactoryObject() |
315
|
|
|
{ |
316
|
|
|
$controller = new GenericEntityCreateController( |
317
|
|
|
$this->controllerHelper, |
318
|
|
|
$this->argumentBuilder, |
319
|
|
|
$this->container, |
320
|
|
|
[ |
321
|
|
|
'entity-class' => SampleEntity::class, |
322
|
|
|
'factory' => '@some_factory_service::serialize' |
323
|
|
|
] |
324
|
|
|
); |
325
|
|
|
|
326
|
|
|
$expectedEntity = new SampleEntity(); |
327
|
|
|
|
328
|
|
|
/** @var SampleEntity|null $actualEntity */ |
329
|
|
|
$actualEntity = null; |
330
|
|
|
|
331
|
|
|
/** @var Serializable $factoryMock */ |
332
|
|
|
$factoryMock = $this->createMock(Serializable::class); |
333
|
|
|
$factoryMock->method("serialize")->willReturn($expectedEntity); |
334
|
|
|
|
335
|
|
|
$this->container->expects($this->once())->method('get')->with( |
|
|
|
|
336
|
|
|
$this->equalTo('some_factory_service') |
337
|
|
|
)->willReturn($factoryMock); |
338
|
|
|
|
339
|
|
|
$this->controllerHelper->method('persistEntity')->will($this->returnCallback( |
|
|
|
|
340
|
|
|
function (SampleEntity $entity) use (&$actualEntity) { |
341
|
|
|
$actualEntity = $entity; |
342
|
|
|
} |
343
|
|
|
)); |
344
|
|
|
|
345
|
|
|
/** @var Request $request */ |
346
|
|
|
$request = $this->createMock(Request::class); |
347
|
|
|
|
348
|
|
|
$controller->createEntity($request); |
349
|
|
|
|
350
|
|
|
$this->assertSame($expectedEntity, $actualEntity); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* @test |
355
|
|
|
*/ |
356
|
|
|
public function shouldRejectWrongEntityCreated() |
357
|
|
|
{ |
358
|
|
|
$controller = new GenericEntityCreateController( |
359
|
|
|
$this->controllerHelper, |
360
|
|
|
$this->argumentBuilder, |
361
|
|
|
$this->container, |
362
|
|
|
[ |
363
|
|
|
'entity-class' => SampleEntity::class, |
364
|
|
|
'factory' => '@some_factory_service::serialize' |
365
|
|
|
] |
366
|
|
|
); |
367
|
|
|
|
368
|
|
|
/** @var Serializable $factoryMock */ |
369
|
|
|
$factoryMock = $this->createMock(Serializable::class); |
370
|
|
|
$factoryMock->method("serialize")->willReturn(new stdClass()); |
371
|
|
|
|
372
|
|
|
$this->container->expects($this->once())->method('get')->with( |
|
|
|
|
373
|
|
|
$this->equalTo('some_factory_service') |
374
|
|
|
)->willReturn($factoryMock); |
375
|
|
|
|
376
|
|
|
/** @var Request $request */ |
377
|
|
|
$request = $this->createMock(Request::class); |
378
|
|
|
|
379
|
|
|
$this->expectException(InvalidArgumentException::class); |
380
|
|
|
|
381
|
|
|
$controller->createEntity($request); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* @test |
386
|
|
|
*/ |
387
|
|
View Code Duplication |
public function shouldRejectNonExistingFactory() |
|
|
|
|
388
|
|
|
{ |
389
|
|
|
$controller = new GenericEntityCreateController( |
390
|
|
|
$this->controllerHelper, |
391
|
|
|
$this->argumentBuilder, |
392
|
|
|
$this->container, |
393
|
|
|
[ |
394
|
|
|
'entity-class' => SampleEntity::class, |
395
|
|
|
'factory' => '@some_factory_service::serialize' |
396
|
|
|
] |
397
|
|
|
); |
398
|
|
|
|
399
|
|
|
$this->container->expects($this->once())->method('get')->with( |
|
|
|
|
400
|
|
|
$this->equalTo('some_factory_service') |
401
|
|
|
)->willReturn(null); |
402
|
|
|
|
403
|
|
|
/** @var Request $request */ |
404
|
|
|
$request = $this->createMock(Request::class); |
405
|
|
|
|
406
|
|
|
$this->expectException(InvalidArgumentException::class); |
407
|
|
|
$this->expectExceptionMessage("Did not find service 'some_factory_service'!"); |
408
|
|
|
|
409
|
|
|
$controller->createEntity($request); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* @test |
414
|
|
|
*/ |
415
|
|
|
public function shouldRejectInvalidFactory() |
416
|
|
|
{ |
417
|
|
|
$controller = new GenericEntityCreateController( |
418
|
|
|
$this->controllerHelper, |
419
|
|
|
$this->argumentBuilder, |
420
|
|
|
$this->container, |
421
|
|
|
[ |
422
|
|
|
'entity-class' => SampleEntity::class, |
423
|
|
|
'factory' => '::serialize' |
424
|
|
|
] |
425
|
|
|
); |
426
|
|
|
|
427
|
|
|
/** @var Request $request */ |
428
|
|
|
$request = $this->createMock(Request::class); |
429
|
|
|
|
430
|
|
|
$this->expectException(ErrorException::class); |
431
|
|
|
|
432
|
|
|
$controller->createEntity($request); |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* @test |
437
|
|
|
*/ |
438
|
|
View Code Duplication |
public function shouldRejectNonExistingFactoryMethod() |
|
|
|
|
439
|
|
|
{ |
440
|
|
|
$controller = new GenericEntityCreateController( |
441
|
|
|
$this->controllerHelper, |
442
|
|
|
$this->argumentBuilder, |
443
|
|
|
$this->container, |
444
|
|
|
[ |
445
|
|
|
'entity-class' => SampleEntity::class, |
446
|
|
|
'factory' => '@some_factory_service::MethodDoesNotExist' |
447
|
|
|
] |
448
|
|
|
); |
449
|
|
|
|
450
|
|
|
/** @var Serializable $factoryMock */ |
451
|
|
|
$factoryMock = $this->createMock(Serializable::class); |
452
|
|
|
|
453
|
|
|
$this->container->expects($this->once())->method('get')->with( |
|
|
|
|
454
|
|
|
$this->equalTo('some_factory_service') |
455
|
|
|
)->willReturn($factoryMock); |
456
|
|
|
|
457
|
|
|
/** @var Request $request */ |
458
|
|
|
$request = $this->createMock(Request::class); |
459
|
|
|
|
460
|
|
|
$this->expectException(ReflectionException::class); |
461
|
|
|
|
462
|
|
|
$controller->createEntity($request); |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
/** |
466
|
|
|
* @test |
467
|
|
|
*/ |
468
|
|
|
public function shouldCorrectlyDetectFactoryMethod() |
469
|
|
|
{ |
470
|
|
|
$controller = new GenericEntityCreateController( |
471
|
|
|
$this->controllerHelper, |
472
|
|
|
$this->argumentBuilder, |
473
|
|
|
$this->container, |
474
|
|
|
[ |
475
|
|
|
'entity-class' => SampleEntity::class, |
476
|
|
|
'factory' => '@some_factory_service::serialize::thisShouldCauseAnError' |
477
|
|
|
] |
478
|
|
|
); |
479
|
|
|
|
480
|
|
|
/** @var Serializable $factoryMock */ |
481
|
|
|
$factoryMock = $this->createMock(Serializable::class); |
482
|
|
|
$factoryMock->method("serialize")->willReturn(new SampleEntity()); |
483
|
|
|
|
484
|
|
|
$this->container->expects($this->once())->method('get')->with( |
|
|
|
|
485
|
|
|
$this->equalTo('some_factory_service') |
486
|
|
|
)->willReturn($factoryMock); |
487
|
|
|
|
488
|
|
|
/** @var Request $request */ |
489
|
|
|
$request = $this->createMock(Request::class); |
490
|
|
|
|
491
|
|
|
$this->expectException(ReflectionException::class); |
492
|
|
|
|
493
|
|
|
$controller->createEntity($request); |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
/** |
497
|
|
|
* @test |
498
|
|
|
*/ |
499
|
|
|
public function shouldCheckIfAccessIsGranted() |
500
|
|
|
{ |
501
|
|
|
$this->expectException(AccessDeniedException::class); |
502
|
|
|
|
503
|
|
|
$this->controllerHelper->expects($this->exactly(2))->method('denyAccessUnlessGranted')->will($this->returnCallback( |
|
|
|
|
504
|
|
|
function ($attribute, $object) { |
505
|
|
|
if ($object instanceof SampleEntity) { |
506
|
|
|
throw new AccessDeniedException('Lorem ipsum!'); |
507
|
|
|
} |
508
|
|
|
} |
509
|
|
|
)); |
510
|
|
|
|
511
|
|
|
/** @var Serializable $factoryMock */ |
512
|
|
|
$factoryMock = $this->createMock(Serializable::class); |
513
|
|
|
$factoryMock->method("serialize")->willReturn(new SampleEntity()); |
514
|
|
|
|
515
|
|
|
$this->container->expects($this->once())->method('get')->with( |
|
|
|
|
516
|
|
|
$this->equalTo('some_factory_service') |
517
|
|
|
)->willReturn($factoryMock); |
518
|
|
|
|
519
|
|
|
$controller = new GenericEntityCreateController( |
520
|
|
|
$this->controllerHelper, |
521
|
|
|
$this->argumentBuilder, |
522
|
|
|
$this->container, |
523
|
|
|
[ |
524
|
|
|
'entity-class' => SampleEntity::class, |
525
|
|
|
'factory' => '@some_factory_service::serialize', |
526
|
|
|
'authorization-attribute' => 'some-attribute', |
527
|
|
|
] |
528
|
|
|
); |
529
|
|
|
|
530
|
|
|
/** @var Request $request */ |
531
|
|
|
$request = $this->createMock(Request::class); |
532
|
|
|
|
533
|
|
|
$controller->createEntity($request); |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
/** |
537
|
|
|
* @test |
538
|
|
|
*/ |
539
|
|
|
public function shouldBeCallableByInvokingController() |
540
|
|
|
{ |
541
|
|
|
$controller = new GenericEntityCreateController( |
542
|
|
|
$this->controllerHelper, |
543
|
|
|
$this->argumentBuilder, |
544
|
|
|
$this->container, |
545
|
|
|
[ |
546
|
|
|
'entity-class' => SampleEntity::class |
547
|
|
|
] |
548
|
|
|
); |
549
|
|
|
|
550
|
|
|
$this->controllerHelper->expects($this->once())->method('flushORM'); |
|
|
|
|
551
|
|
|
$this->controllerHelper->expects($this->once())->method('persistEntity')->with( |
|
|
|
|
552
|
|
|
$this->equalTo(new SampleEntity()) |
553
|
|
|
); |
554
|
|
|
|
555
|
|
|
/** @var Request $request */ |
556
|
|
|
$request = $this->createMock(Request::class); |
557
|
|
|
|
558
|
|
|
$this->controllerHelper->method('getCurrentRequest')->willReturn($request); |
|
|
|
|
559
|
|
|
|
560
|
|
|
/** @var Response $response */ |
561
|
|
|
$response = $controller(); |
562
|
|
|
|
563
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
/** |
567
|
|
|
* @test |
568
|
|
|
*/ |
569
|
|
|
public function shouldRejectCallWithoutRequest() |
570
|
|
|
{ |
571
|
|
|
$this->expectException(InvalidArgumentException::class); |
572
|
|
|
|
573
|
|
|
$controller = new GenericEntityCreateController( |
574
|
|
|
$this->controllerHelper, |
575
|
|
|
$this->argumentBuilder, |
576
|
|
|
$this->container, |
577
|
|
|
[ |
578
|
|
|
'entity-class' => SampleEntity::class, |
579
|
|
|
] |
580
|
|
|
); |
581
|
|
|
|
582
|
|
|
$this->controllerHelper->method('getCurrentRequest')->willReturn(null); |
|
|
|
|
583
|
|
|
|
584
|
|
|
$controller(); |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
/** |
588
|
|
|
* @test |
589
|
|
|
*/ |
590
|
|
|
public function shouldCreateAnEntityThatHasNoConstructor() |
591
|
|
|
{ |
592
|
|
|
$controller = new GenericEntityCreateController( |
593
|
|
|
$this->controllerHelper, |
594
|
|
|
$this->argumentBuilder, |
595
|
|
|
$this->container, |
596
|
|
|
[ |
597
|
|
|
'entity-class' => SampleEntityWithoutConstructor::class |
598
|
|
|
] |
599
|
|
|
); |
600
|
|
|
|
601
|
|
|
$this->controllerHelper->expects($this->once())->method('persistEntity')->with( |
|
|
|
|
602
|
|
|
$this->equalTo(new SampleEntityWithoutConstructor()) |
603
|
|
|
); |
604
|
|
|
|
605
|
|
|
/** @var Request $request */ |
606
|
|
|
$request = $this->createMock(Request::class); |
607
|
|
|
|
608
|
|
|
/** @var Response $response */ |
609
|
|
|
$response = $controller->createEntity($request); |
610
|
|
|
|
611
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
612
|
|
|
} |
613
|
|
|
|
614
|
|
|
/** |
615
|
|
|
* @test |
616
|
|
|
*/ |
617
|
|
View Code Duplication |
public function shouldCreateAnEntityUsingAStaticFactory() |
|
|
|
|
618
|
|
|
{ |
619
|
|
|
$controller = new GenericEntityCreateController( |
620
|
|
|
$this->controllerHelper, |
621
|
|
|
$this->argumentBuilder, |
622
|
|
|
$this->container, |
623
|
|
|
[ |
624
|
|
|
'entity-class' => SampleEntity::class, |
625
|
|
|
'factory' => 'someStaticFactory' |
626
|
|
|
] |
627
|
|
|
); |
628
|
|
|
|
629
|
|
|
$this->controllerHelper->expects($this->once())->method('persistEntity')->with( |
|
|
|
|
630
|
|
|
$this->equalTo(new SampleEntity("static foo")) |
631
|
|
|
); |
632
|
|
|
|
633
|
|
|
/** @var Request $request */ |
634
|
|
|
$request = $this->createMock(Request::class); |
635
|
|
|
|
636
|
|
|
/** @var Response $response */ |
637
|
|
|
$response = $controller->createEntity($request); |
638
|
|
|
|
639
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
640
|
|
|
} |
641
|
|
|
|
642
|
|
|
/** |
643
|
|
|
* @test |
644
|
|
|
*/ |
645
|
|
View Code Duplication |
public function shouldCreateAnEntityUsingRegularFunction() |
|
|
|
|
646
|
|
|
{ |
647
|
|
|
$controller = new GenericEntityCreateController( |
648
|
|
|
$this->controllerHelper, |
649
|
|
|
$this->argumentBuilder, |
650
|
|
|
$this->container, |
651
|
|
|
[ |
652
|
|
|
'entity-class' => SampleEntity::class, |
653
|
|
|
'factory' => 'someStaticFactory' |
654
|
|
|
] |
655
|
|
|
); |
656
|
|
|
|
657
|
|
|
$this->controllerHelper->expects($this->once())->method('persistEntity')->with( |
|
|
|
|
658
|
|
|
$this->equalTo(new SampleEntity("static foo")) |
659
|
|
|
); |
660
|
|
|
|
661
|
|
|
/** @var Request $request */ |
662
|
|
|
$request = $this->createMock(Request::class); |
663
|
|
|
|
664
|
|
|
/** @var Response $response */ |
665
|
|
|
$response = $controller->createEntity($request); |
666
|
|
|
|
667
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
668
|
|
|
} |
669
|
|
|
|
670
|
|
|
} |
671
|
|
|
|
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..