Passed
Push — master ( 3f1e18...b03604 )
by Gerrit
05:04
created

shouldRejectCallToNonExistingMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
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
    public function shouldCreateAnEntity()
58
    {
59
        $controller = new GenericEntityCreateController(
60
            $this->controllerHelper,
61
            $this->argumentBuilder,
62
            $this->container,
63
            [
64
                'entity-class' => SampleEntity::class
65
            ]
66
        );
67
68
        $this->controllerHelper->expects($this->once())->method('flushORM');
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 shouldRejectMissingEntityClass()
86
    {
87
        $this->expectException(InvalidArgumentException::class);
88
89
        $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...
90
            $this->controllerHelper,
91
            $this->argumentBuilder,
92
            $this->container,
93
            [
94
            ]
95
        );
96
    }
97
98
    /**
99
     * @test
100
     */
101
    public function shouldRejectControllerCalledAgain()
102
    {
103
        $controller = new GenericEntityCreateController(
104
            $this->controllerHelper,
105
            $this->argumentBuilder,
106
            $this->container,
107
            [
108
                'entity-class' => SampleEntity::class
109
            ]
110
        );
111
112
        $this->expectException(InvalidArgumentException::class);
113
114
        $controller->__construct(
115
            $this->controllerHelper,
116
            $this->argumentBuilder,
117
            $this->container,
118
            [
119
                'entity-class' => SampleEntity::class
120
            ]
121
        );
122
    }
123
124
    /**
125
     * @test
126
     */
127
    public function shouldProvideConstructArguments()
128
    {
129
        $controller = new GenericEntityCreateController(
130
            $this->controllerHelper,
131
            $this->argumentBuilder,
132
            $this->container,
133
            [
134
                'entity-class' => SampleEntity::class,
135
                'arguments' => [
136
                    'foo' => 'bar'
137
                ]
138
            ]
139
        );
140
141
        /** @var SampleEntity|null $persistedEntity */
142
        $persistedEntity = null;
143
144
        $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...
145
        $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...
146
            function (SampleEntity $entity) use (&$persistedEntity) {
147
                $persistedEntity = $entity;
148
            }
149
        ));
150
151
        $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...
152
            'bar'
153
        ]);
154
155
        /** @var Request $request */
156
        $request = $this->createMock(Request::class);
157
158
        /** @var Response $response */
159
        $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...
160
161
        $this->assertEquals('bar', $persistedEntity->constructArgument);
162
    }
163
164
    /**
165
     * @test
166
     */
167
    public function shouldExecuteACall()
168
    {
169
        $controller = new GenericEntityCreateController(
170
            $this->controllerHelper,
171
            $this->argumentBuilder,
172
            $this->container,
173
            [
174
                'entity-class' => SampleEntity::class,
175
                'calls' => [
176
                    'doFoo' => []
177
                ]
178
            ]
179
        );
180
181
        /** @var SampleEntity|null $persistedEntity */
182
        $persistedEntity = null;
183
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
        /** @var Request $request */
191
        $request = $this->createMock(Request::class);
192
193
        $this->assertNull($persistedEntity);
194
195
        $controller->createEntity($request);
196
197
        $this->assertTrue($persistedEntity instanceof SampleEntity);
198
        $this->assertTrue($persistedEntity->fooCalled);
199
    }
200
201
    /**
202
     * @test
203
     */
204
    public function shouldRejectCallToNonExistingMethod()
205
    {
206
        $this->expectException(InvalidArgumentException::class);
207
208
        $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...
209
            $this->controllerHelper,
210
            $this->argumentBuilder,
211
            $this->container,
212
            [
213
                'entity-class' => SampleEntity::class,
214
                'calls' => [
215
                    'doNonExistingThing' => []
216
                ]
217
            ]
218
        );
219
    }
220
221
    /**
222
     * @test
223
     */
224
    public function shouldRejectCallWithNonArrayParameters()
225
    {
226
        $this->expectException(InvalidArgumentException::class);
227
228
        $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...
229
            $this->controllerHelper,
230
            $this->argumentBuilder,
231
            $this->container,
232
            [
233
                'entity-class' => SampleEntity::class,
234
                'calls' => [
235
                    'doFoo' => "notAnArray"
236
                ]
237
            ]
238
        );
239
    }
240
241
    /**
242
     * @test
243
     */
244
    public function shouldRejectCallWithIntegerMethod()
245
    {
246
        $this->expectException(InvalidArgumentException::class);
247
248
        $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...
249
            $this->controllerHelper,
250
            $this->argumentBuilder,
251
            $this->container,
252
            [
253
                'entity-class' => SampleEntity::class,
254
                'calls' => [
255
                    0 => []
256
                ]
257
            ]
258
        );
259
    }
260
261
    /**
262
     * @test
263
     */
264
    public function shouldUseFactoryObject()
265
    {
266
        $controller = new GenericEntityCreateController(
267
            $this->controllerHelper,
268
            $this->argumentBuilder,
269
            $this->container,
270
            [
271
                'entity-class' => SampleEntity::class,
272
                'factory' => '@some_factory_service::serialize'
273
            ]
274
        );
275
276
        $expectedEntity = new SampleEntity();
277
278
        /** @var SampleEntity|null $actualEntity */
279
        $actualEntity = null;
280
281
        /** @var Serializable $factoryMock */
282
        $factoryMock = $this->createMock(Serializable::class);
283
        $factoryMock->method("serialize")->willReturn($expectedEntity);
284
285
        $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...
286
            $this->equalTo('some_factory_service')
287
        )->willReturn($factoryMock);
288
289
        $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...
290
            function (SampleEntity $entity) use (&$actualEntity) {
291
                $actualEntity = $entity;
292
            }
293
        ));
294
295
        /** @var Request $request */
296
        $request = $this->createMock(Request::class);
297
298
        $controller->createEntity($request);
299
300
        $this->assertSame($expectedEntity, $actualEntity);
301
    }
302
303
    /**
304
     * @test
305
     */
306
    public function shouldRejectWrongEntityCreated()
307
    {
308
        $controller = new GenericEntityCreateController(
309
            $this->controllerHelper,
310
            $this->argumentBuilder,
311
            $this->container,
312
            [
313
                'entity-class' => SampleEntity::class,
314
                'factory' => '@some_factory_service::serialize'
315
            ]
316
        );
317
318
        /** @var Serializable $factoryMock */
319
        $factoryMock = $this->createMock(Serializable::class);
320
        $factoryMock->method("serialize")->willReturn(new stdClass());
321
322
        $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...
323
            $this->equalTo('some_factory_service')
324
        )->willReturn($factoryMock);
325
326
        /** @var Request $request */
327
        $request = $this->createMock(Request::class);
328
329
        $this->expectException(InvalidArgumentException::class);
330
331
        $controller->createEntity($request);
332
    }
333
334
    /**
335
     * @test
336
     */
337 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...
338
    {
339
        $controller = new GenericEntityCreateController(
340
            $this->controllerHelper,
341
            $this->argumentBuilder,
342
            $this->container,
343
            [
344
                'entity-class' => SampleEntity::class,
345
                'factory' => '@some_factory_service::serialize'
346
            ]
347
        );
348
349
        $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...
350
            $this->equalTo('some_factory_service')
351
        )->willReturn(null);
352
353
        /** @var Request $request */
354
        $request = $this->createMock(Request::class);
355
356
        $this->expectException(InvalidArgumentException::class);
357
        $this->expectExceptionMessage("Did not find service 'some_factory_service'!");
358
359
        $controller->createEntity($request);
360
    }
361
362
    /**
363
     * @test
364
     */
365
    public function shouldRejectInvalidFactory()
366
    {
367
        $controller = new GenericEntityCreateController(
368
            $this->controllerHelper,
369
            $this->argumentBuilder,
370
            $this->container,
371
            [
372
                'entity-class' => SampleEntity::class,
373
                'factory' => '::serialize'
374
            ]
375
        );
376
377
        /** @var Request $request */
378
        $request = $this->createMock(Request::class);
379
380
        $this->expectException(ErrorException::class);
381
382
        $controller->createEntity($request);
383
    }
384
385
    /**
386
     * @test
387
     */
388 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...
389
    {
390
        $controller = new GenericEntityCreateController(
391
            $this->controllerHelper,
392
            $this->argumentBuilder,
393
            $this->container,
394
            [
395
                'entity-class' => SampleEntity::class,
396
                'factory' => '@some_factory_service::MethodDoesNotExist'
397
            ]
398
        );
399
400
        /** @var Serializable $factoryMock */
401
        $factoryMock = $this->createMock(Serializable::class);
402
403
        $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...
404
            $this->equalTo('some_factory_service')
405
        )->willReturn($factoryMock);
406
407
        /** @var Request $request */
408
        $request = $this->createMock(Request::class);
409
410
        $this->expectException(ReflectionException::class);
411
412
        $controller->createEntity($request);
413
    }
414
415
    /**
416
     * @test
417
     */
418
    public function shouldCorrectlyDetectFactoryMethod()
419
    {
420
        $controller = new GenericEntityCreateController(
421
            $this->controllerHelper,
422
            $this->argumentBuilder,
423
            $this->container,
424
            [
425
                'entity-class' => SampleEntity::class,
426
                'factory' => '@some_factory_service::serialize::thisShouldCauseAnError'
427
            ]
428
        );
429
430
        /** @var Serializable $factoryMock */
431
        $factoryMock = $this->createMock(Serializable::class);
432
        $factoryMock->method("serialize")->willReturn(new SampleEntity());
433
434
        $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...
435
            $this->equalTo('some_factory_service')
436
        )->willReturn($factoryMock);
437
438
        /** @var Request $request */
439
        $request = $this->createMock(Request::class);
440
441
        $this->expectException(ReflectionException::class);
442
443
        $controller->createEntity($request);
444
    }
445
446
    /**
447
     * @test
448
     */
449
    public function shouldCheckIfAccessIsGranted()
450
    {
451
        $this->expectException(AccessDeniedException::class);
452
453
        $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...
454
            function ($attribute, $object) {
455
                if ($object instanceof SampleEntity) {
456
                    throw new AccessDeniedException('Lorem ipsum!');
457
                }
458
            }
459
        ));
460
461
        /** @var Serializable $factoryMock */
462
        $factoryMock = $this->createMock(Serializable::class);
463
        $factoryMock->method("serialize")->willReturn(new SampleEntity());
464
465
        $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...
466
            $this->equalTo('some_factory_service')
467
        )->willReturn($factoryMock);
468
469
        $controller = new GenericEntityCreateController(
470
            $this->controllerHelper,
471
            $this->argumentBuilder,
472
            $this->container,
473
            [
474
                'entity-class' => SampleEntity::class,
475
                'factory' => '@some_factory_service::serialize',
476
                'authorization-attribute' => 'some-attribute',
477
            ]
478
        );
479
480
        /** @var Request $request */
481
        $request = $this->createMock(Request::class);
482
483
        $controller->createEntity($request);
484
    }
485
486
}
487