Completed
Push — master ( 8c5203...c6433c )
by Gerrit
23:20
created

GenericEntityRemoveControllerTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 78
c 0
b 0
f 0
wmc 5
lcom 1
cbo 3
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A shouldRejectMissingEntityClass() 0 6 1
A shouldRejectControllerCalledAgain() 0 8 1
A shouldThrowExceptionIfEntityNotFound() 0 6 1
A shouldRemoveEntity() 0 19 1
1
<?php
2
/**
3
 * Copyright (C) 2017  Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 * @license GPL-3.0
8
 * @author Gerrit Addiks <[email protected]>
9
 */
10
11
namespace Addiks\SymfonyGenerics\Tests\Unit\Controllers;
12
13
use PHPUnit\Framework\TestCase;
14
use Addiks\SymfonyGenerics\Controllers\GenericEntityRemoveController;
15
use Addiks\SymfonyGenerics\Controllers\ControllerHelperInterface;
16
use stdClass;
17
use Symfony\Component\HttpFoundation\Response;
18
use InvalidArgumentException;
19
20
final class GenericEntityRemoveControllerTest extends TestCase
21
{
22
23
    /**
24
     * @var GenericEntityRemoveController
25
     */
26
    private $controller;
27
28
    /**
29
     * @var ControllerHelperInterface
30
     */
31
    private $controllerHelper;
32
33
    public function setUp()
34
    {
35
        $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...
36
37
        $this->controller = new GenericEntityRemoveController($this->controllerHelper, [
0 ignored issues
show
Documentation introduced by
$this->controllerHelper is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\SymfonyGen...trollerHelperInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
38
            'entity-class' => stdClass::class
39
        ]);
40
    }
41
42
    /**
43
     * @test
44
     */
45
    public function shouldRejectMissingEntityClass()
46
    {
47
        $this->expectException(InvalidArgumentException::class);
48
49
        new GenericEntityRemoveController($this->controllerHelper, []);
50
    }
51
52
    /**
53
     * @test
54
     */
55
    public function shouldRejectControllerCalledAgain()
56
    {
57
        $this->expectException(InvalidArgumentException::class);
58
59
        $this->controller->__construct($this->controllerHelper, [
60
            'entity-class' => stdClass::class
61
        ]);
62
    }
63
64
    /**
65
     * @test
66
     */
67
    public function shouldThrowExceptionIfEntityNotFound()
68
    {
69
        $this->expectException(InvalidArgumentException::class);
70
71
        $this->controller->removeEntity("some-id");
72
    }
73
74
    /**
75
     * @test
76
     */
77
    public function shouldRemoveEntity()
78
    {
79
        $entity = new stdClass();
80
81
        $this->controllerHelper->expects($this->once())->method('findEntity')->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...
82
            $this->equalTo(stdClass::class),
83
            $this->equalTo('some-id')
84
        )->willReturn($entity);
85
86
        $this->controllerHelper->expects($this->once())->method('removeEntity')->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...
87
            $this->identicalTo($entity)
88
        );
89
90
        /** @var Response $response */
91
        $response = $this->controller->removeEntity("some-id");
92
93
        $this->assertEquals(200, $response->getStatusCode());
94
        $this->assertEquals('Entity removed!', $response->getContent());
95
    }
96
97
}
98