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