Completed
Push — master ( 27d676...dee7dc )
by Gerrit
03:35
created

shouldRedirectWhenNeeded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 9.36
c 0
b 0
f 0
cc 1
nc 1
nop 0
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);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Addik...HelperInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Addiks\SymfonyGen...trollerHelperInterface> of property $controllerHelper.

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..

Loading history...
50
        $this->container = $this->createMock(ContainerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Psr\C...tainerInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Psr\Container\ContainerInterface> of property $container.

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..

Loading history...
51
        $this->argumentBuilder = $this->createMock(ArgumentCompilerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Addik...mpilerInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Addiks\SymfonyGen...umentCompilerInterface> of property $argumentBuilder.

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..

Loading history...
52
    }
53
54
    /**
55
     * @test
56
     */
57 View Code Duplication
    public function shouldCreateAnEntity()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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');
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
69
        $this->controllerHelper->expects($this->once())->method('persistEntity')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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 shouldRedirectWhenNeeded()
86
    {
87
        $controller = new GenericEntityCreateController(
88
            $this->controllerHelper,
89
            $this->argumentBuilder,
90
            $this->container,
91
            [
92
                'entity-class' => SampleEntity::class,
93
                'success-redirect' => 'some_route'
94
            ]
95
        );
96
97
        $this->controllerHelper->expects($this->once())->method('flushORM');
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
98
        $this->controllerHelper->expects($this->once())->method('persistEntity')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
99
            $this->equalTo(new SampleEntity())
100
        );
101
102
        $expectedResponse = new Response();
103
104
        $this->controllerHelper->expects($this->once())->method('redirectToRoute')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
105
            $this->equalTo('some_route'),
106
            $this->equalTo([
107
                'entityId' => 'some_id'
108
            ]),
109
            $this->equalTo(303)
110
        )->willReturn($expectedResponse);
111
112
        /** @var Request $request */
113
        $request = $this->createMock(Request::class);
114
115
        /** @var Response $actualResponse */
116
        $actualResponse = $controller->createEntity($request);
117
118
        $this->assertSame($expectedResponse, $actualResponse);
119
    }
120
121
    /**
122
     * @test
123
     */
124
    public function shouldRejectMissingEntityClass()
125
    {
126
        $this->expectException(InvalidArgumentException::class);
127
128
        $controller = new GenericEntityCreateController(
0 ignored issues
show
Unused Code introduced by
$controller is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
129
            $this->controllerHelper,
130
            $this->argumentBuilder,
131
            $this->container,
132
            [
133
            ]
134
        );
135
    }
136
137
    /**
138
     * @test
139
     */
140
    public function shouldRejectControllerCalledAgain()
141
    {
142
        $controller = new GenericEntityCreateController(
143
            $this->controllerHelper,
144
            $this->argumentBuilder,
145
            $this->container,
146
            [
147
                'entity-class' => SampleEntity::class
148
            ]
149
        );
150
151
        $this->expectException(InvalidArgumentException::class);
152
153
        $controller->__construct(
154
            $this->controllerHelper,
155
            $this->argumentBuilder,
156
            $this->container,
157
            [
158
                'entity-class' => SampleEntity::class
159
            ]
160
        );
161
    }
162
163
    /**
164
     * @test
165
     */
166
    public function shouldProvideConstructArguments()
167
    {
168
        $controller = new GenericEntityCreateController(
169
            $this->controllerHelper,
170
            $this->argumentBuilder,
171
            $this->container,
172
            [
173
                'entity-class' => SampleEntity::class,
174
                'arguments' => [
175
                    'foo' => 'bar'
176
                ]
177
            ]
178
        );
179
180
        /** @var SampleEntity|null $persistedEntity */
181
        $persistedEntity = null;
182
183
        $this->controllerHelper->expects($this->once())->method('flushORM');
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
184
        $this->controllerHelper->method('persistEntity')->will($this->returnCallback(
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
185
            function (SampleEntity $entity) use (&$persistedEntity) {
186
                $persistedEntity = $entity;
187
            }
188
        ));
189
190
        $this->argumentBuilder->method('buildCallArguments')->willReturn([
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\SymfonyGen...umentCompilerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
191
            'bar'
192
        ]);
193
194
        /** @var Request $request */
195
        $request = $this->createMock(Request::class);
196
197
        /** @var Response $response */
198
        $response = $controller->createEntity($request);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
199
200
        $this->assertEquals('bar', $persistedEntity->constructArgument);
201
    }
202
203
    /**
204
     * @test
205
     */
206
    public function shouldExecuteACall()
207
    {
208
        $controller = new GenericEntityCreateController(
209
            $this->controllerHelper,
210
            $this->argumentBuilder,
211
            $this->container,
212
            [
213
                'entity-class' => SampleEntity::class,
214
                'calls' => [
215
                    'doFoo' => []
216
                ]
217
            ]
218
        );
219
220
        /** @var SampleEntity|null $persistedEntity */
221
        $persistedEntity = null;
222
223
        $this->controllerHelper->method('persistEntity')->will($this->returnCallback(
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
224
            function (SampleEntity $entity) use (&$persistedEntity) {
225
                $persistedEntity = $entity;
226
            }
227
        ));
228
229
        /** @var Request $request */
230
        $request = $this->createMock(Request::class);
231
232
        $this->assertNull($persistedEntity);
233
234
        $controller->createEntity($request);
235
236
        $this->assertTrue($persistedEntity instanceof SampleEntity);
237
        $this->assertTrue($persistedEntity->fooCalled);
238
    }
239
240
    /**
241
     * @test
242
     */
243
    public function shouldRejectCallToNonExistingMethod()
244
    {
245
        $this->expectException(InvalidArgumentException::class);
246
247
        $controller = new GenericEntityCreateController(
0 ignored issues
show
Unused Code introduced by
$controller is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
248
            $this->controllerHelper,
249
            $this->argumentBuilder,
250
            $this->container,
251
            [
252
                'entity-class' => SampleEntity::class,
253
                'calls' => [
254
                    'doNonExistingThing' => []
255
                ]
256
            ]
257
        );
258
    }
259
260
    /**
261
     * @test
262
     */
263
    public function shouldRejectCallWithNonArrayParameters()
264
    {
265
        $this->expectException(InvalidArgumentException::class);
266
267
        $controller = new GenericEntityCreateController(
0 ignored issues
show
Unused Code introduced by
$controller is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
268
            $this->controllerHelper,
269
            $this->argumentBuilder,
270
            $this->container,
271
            [
272
                'entity-class' => SampleEntity::class,
273
                'calls' => [
274
                    'doFoo' => "notAnArray"
275
                ]
276
            ]
277
        );
278
    }
279
280
    /**
281
     * @test
282
     */
283
    public function shouldRejectCallWithIntegerMethod()
284
    {
285
        $this->expectException(InvalidArgumentException::class);
286
287
        $controller = new GenericEntityCreateController(
0 ignored issues
show
Unused Code introduced by
$controller is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
288
            $this->controllerHelper,
289
            $this->argumentBuilder,
290
            $this->container,
291
            [
292
                'entity-class' => SampleEntity::class,
293
                'calls' => [
294
                    0 => []
295
                ]
296
            ]
297
        );
298
    }
299
300
    /**
301
     * @test
302
     */
303
    public function shouldUseFactoryObject()
304
    {
305
        $controller = new GenericEntityCreateController(
306
            $this->controllerHelper,
307
            $this->argumentBuilder,
308
            $this->container,
309
            [
310
                'entity-class' => SampleEntity::class,
311
                'factory' => '@some_factory_service::serialize'
312
            ]
313
        );
314
315
        $expectedEntity = new SampleEntity();
316
317
        /** @var SampleEntity|null $actualEntity */
318
        $actualEntity = null;
319
320
        /** @var Serializable $factoryMock */
321
        $factoryMock = $this->createMock(Serializable::class);
322
        $factoryMock->method("serialize")->willReturn($expectedEntity);
323
324
        $this->container->expects($this->once())->method('get')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Container\ContainerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
325
            $this->equalTo('some_factory_service')
326
        )->willReturn($factoryMock);
327
328
        $this->controllerHelper->method('persistEntity')->will($this->returnCallback(
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
329
            function (SampleEntity $entity) use (&$actualEntity) {
330
                $actualEntity = $entity;
331
            }
332
        ));
333
334
        /** @var Request $request */
335
        $request = $this->createMock(Request::class);
336
337
        $controller->createEntity($request);
338
339
        $this->assertSame($expectedEntity, $actualEntity);
340
    }
341
342
    /**
343
     * @test
344
     */
345
    public function shouldRejectWrongEntityCreated()
346
    {
347
        $controller = new GenericEntityCreateController(
348
            $this->controllerHelper,
349
            $this->argumentBuilder,
350
            $this->container,
351
            [
352
                'entity-class' => SampleEntity::class,
353
                'factory' => '@some_factory_service::serialize'
354
            ]
355
        );
356
357
        /** @var Serializable $factoryMock */
358
        $factoryMock = $this->createMock(Serializable::class);
359
        $factoryMock->method("serialize")->willReturn(new stdClass());
360
361
        $this->container->expects($this->once())->method('get')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Container\ContainerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
362
            $this->equalTo('some_factory_service')
363
        )->willReturn($factoryMock);
364
365
        /** @var Request $request */
366
        $request = $this->createMock(Request::class);
367
368
        $this->expectException(InvalidArgumentException::class);
369
370
        $controller->createEntity($request);
371
    }
372
373
    /**
374
     * @test
375
     */
376 View Code Duplication
    public function shouldRejectNonExistingFactory()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
377
    {
378
        $controller = new GenericEntityCreateController(
379
            $this->controllerHelper,
380
            $this->argumentBuilder,
381
            $this->container,
382
            [
383
                'entity-class' => SampleEntity::class,
384
                'factory' => '@some_factory_service::serialize'
385
            ]
386
        );
387
388
        $this->container->expects($this->once())->method('get')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Container\ContainerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
389
            $this->equalTo('some_factory_service')
390
        )->willReturn(null);
391
392
        /** @var Request $request */
393
        $request = $this->createMock(Request::class);
394
395
        $this->expectException(InvalidArgumentException::class);
396
        $this->expectExceptionMessage("Did not find service 'some_factory_service'!");
397
398
        $controller->createEntity($request);
399
    }
400
401
    /**
402
     * @test
403
     */
404
    public function shouldRejectInvalidFactory()
405
    {
406
        $controller = new GenericEntityCreateController(
407
            $this->controllerHelper,
408
            $this->argumentBuilder,
409
            $this->container,
410
            [
411
                'entity-class' => SampleEntity::class,
412
                'factory' => '::serialize'
413
            ]
414
        );
415
416
        /** @var Request $request */
417
        $request = $this->createMock(Request::class);
418
419
        $this->expectException(ErrorException::class);
420
421
        $controller->createEntity($request);
422
    }
423
424
    /**
425
     * @test
426
     */
427 View Code Duplication
    public function shouldRejectNonExistingFactoryMethod()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
428
    {
429
        $controller = new GenericEntityCreateController(
430
            $this->controllerHelper,
431
            $this->argumentBuilder,
432
            $this->container,
433
            [
434
                'entity-class' => SampleEntity::class,
435
                'factory' => '@some_factory_service::MethodDoesNotExist'
436
            ]
437
        );
438
439
        /** @var Serializable $factoryMock */
440
        $factoryMock = $this->createMock(Serializable::class);
441
442
        $this->container->expects($this->once())->method('get')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Container\ContainerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
443
            $this->equalTo('some_factory_service')
444
        )->willReturn($factoryMock);
445
446
        /** @var Request $request */
447
        $request = $this->createMock(Request::class);
448
449
        $this->expectException(ReflectionException::class);
450
451
        $controller->createEntity($request);
452
    }
453
454
    /**
455
     * @test
456
     */
457
    public function shouldCorrectlyDetectFactoryMethod()
458
    {
459
        $controller = new GenericEntityCreateController(
460
            $this->controllerHelper,
461
            $this->argumentBuilder,
462
            $this->container,
463
            [
464
                'entity-class' => SampleEntity::class,
465
                'factory' => '@some_factory_service::serialize::thisShouldCauseAnError'
466
            ]
467
        );
468
469
        /** @var Serializable $factoryMock */
470
        $factoryMock = $this->createMock(Serializable::class);
471
        $factoryMock->method("serialize")->willReturn(new SampleEntity());
472
473
        $this->container->expects($this->once())->method('get')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Container\ContainerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
474
            $this->equalTo('some_factory_service')
475
        )->willReturn($factoryMock);
476
477
        /** @var Request $request */
478
        $request = $this->createMock(Request::class);
479
480
        $this->expectException(ReflectionException::class);
481
482
        $controller->createEntity($request);
483
    }
484
485
    /**
486
     * @test
487
     */
488
    public function shouldCheckIfAccessIsGranted()
489
    {
490
        $this->expectException(AccessDeniedException::class);
491
492
        $this->controllerHelper->expects($this->exactly(2))->method('denyAccessUnlessGranted')->will($this->returnCallback(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
493
            function ($attribute, $object) {
494
                if ($object instanceof SampleEntity) {
495
                    throw new AccessDeniedException('Lorem ipsum!');
496
                }
497
            }
498
        ));
499
500
        /** @var Serializable $factoryMock */
501
        $factoryMock = $this->createMock(Serializable::class);
502
        $factoryMock->method("serialize")->willReturn(new SampleEntity());
503
504
        $this->container->expects($this->once())->method('get')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Container\ContainerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
505
            $this->equalTo('some_factory_service')
506
        )->willReturn($factoryMock);
507
508
        $controller = new GenericEntityCreateController(
509
            $this->controllerHelper,
510
            $this->argumentBuilder,
511
            $this->container,
512
            [
513
                'entity-class' => SampleEntity::class,
514
                'factory' => '@some_factory_service::serialize',
515
                'authorization-attribute' => 'some-attribute',
516
            ]
517
        );
518
519
        /** @var Request $request */
520
        $request = $this->createMock(Request::class);
521
522
        $controller->createEntity($request);
523
    }
524
525
    /**
526
     * @test
527
     */
528 View Code Duplication
    public function shouldBeCallableByInvokingController()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
529
    {
530
        $controller = new GenericEntityCreateController(
531
            $this->controllerHelper,
532
            $this->argumentBuilder,
533
            $this->container,
534
            [
535
                'entity-class' => SampleEntity::class
536
            ]
537
        );
538
539
        $this->controllerHelper->expects($this->once())->method('flushORM');
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
540
        $this->controllerHelper->expects($this->once())->method('persistEntity')->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
541
            $this->equalTo(new SampleEntity())
542
        );
543
544
        /** @var Request $request */
545
        $request = $this->createMock(Request::class);
546
547
        $this->controllerHelper->method('getCurrentRequest')->willReturn($request);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
548
549
        /** @var Response $response */
550
        $response = $controller();
551
552
        $this->assertEquals(200, $response->getStatusCode());
553
    }
554
555
    /**
556
     * @test
557
     */
558
    public function shouldRejectCallWithoutRequest()
559
    {
560
        $this->expectException(InvalidArgumentException::class);
561
562
        $controller = new GenericEntityCreateController(
563
            $this->controllerHelper,
564
            $this->argumentBuilder,
565
            $this->container,
566
            [
567
                'entity-class' => SampleEntity::class,
568
            ]
569
        );
570
571
        $this->controllerHelper->method('getCurrentRequest')->willReturn(null);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\SymfonyGen...trollerHelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
572
573
        $controller();
574
    }
575
576
}
577