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

shouldBeCallableByInvokingController()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
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\GenericEntityRemoveController;
15
use Addiks\SymfonyGenerics\Controllers\ControllerHelperInterface;
16
use Symfony\Component\HttpFoundation\Response;
17
use InvalidArgumentException;
18
use Symfony\Component\Finder\Exception\AccessDeniedException;
19
use Addiks\SymfonyGenerics\Tests\Unit\Controllers\SampleEntity;
20
use Symfony\Component\HttpFoundation\Request;
21
22
final class GenericEntityRemoveControllerTest extends TestCase
23
{
24
25
    /**
26
     * @var GenericEntityRemoveController
27
     */
28
    private $controller;
29
30
    /**
31
     * @var ControllerHelperInterface
32
     */
33
    private $controllerHelper;
34
35
    public function setUp()
36
    {
37
        $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...
38
39
        $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...
40
            'entity-class' => SampleEntity::class
41
        ]);
42
    }
43
44
    /**
45
     * @test
46
     */
47
    public function shouldRejectMissingEntityClass()
48
    {
49
        $this->expectException(InvalidArgumentException::class);
50
51
        new GenericEntityRemoveController($this->controllerHelper, []);
52
    }
53
54
    /**
55
     * @test
56
     */
57
    public function shouldRejectControllerCalledAgain()
58
    {
59
        $this->expectException(InvalidArgumentException::class);
60
61
        $this->controller->__construct($this->controllerHelper, [
62
            'entity-class' => SampleEntity::class
63
        ]);
64
    }
65
66
    /**
67
     * @test
68
     */
69
    public function shouldThrowExceptionIfEntityNotFound()
70
    {
71
        $this->expectException(InvalidArgumentException::class);
72
73
        $this->controller->removeEntity("some-id");
74
    }
75
76
    /**
77
     * @test
78
     */
79
    public function shouldRemoveEntity()
80
    {
81
        $entity = new SampleEntity();
82
83
        $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...
84
            $this->equalTo(SampleEntity::class),
85
            $this->equalTo('some-id')
86
        )->willReturn($entity);
87
88
        $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...
89
            $this->identicalTo($entity)
90
        );
91
92
        /** @var Response $response */
93
        $response = $this->controller->removeEntity("some-id");
94
95
        $this->assertEquals(200, $response->getStatusCode());
96
        $this->assertEquals('Entity removed!', $response->getContent());
97
    }
98
99
    /**
100
     * @test
101
     */
102 View Code Duplication
    public function shouldCheckIfAccessIsGranted()
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...
103
    {
104
        $this->expectException(AccessDeniedException::class);
105
106
        $entity = new SampleEntity();
107
108
        $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...
109
            $this->equalTo(SampleEntity::class),
110
            $this->equalTo("some-id")
111
        )->willReturn($entity);
112
113
        $this->controllerHelper->expects($this->once())->method('denyAccessUnlessGranted')->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...
114
            $this->equalTo('some-attribute'),
115
            $this->identicalTo($entity)
116
        )->will($this->returnCallback(
117
            function () {
118
                throw new AccessDeniedException('Lorem ipsum!');
119
            }
120
        ));
121
122
        $controller = new GenericEntityRemoveController($this->controllerHelper, [
123
            'entity-class' => SampleEntity::class,
124
            'authorization-attribute' => 'some-attribute',
125
        ]);
126
127
        $controller->removeEntity("some-id");
128
    }
129
130
    /**
131
     * @test
132
     */
133
    public function shouldBeCallableByInvokingController()
134
    {
135
        $entity = new SampleEntity();
136
137
        $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...
138
            $this->equalTo(SampleEntity::class),
139
            $this->equalTo('some-id')
140
        )->willReturn($entity);
141
142
        $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...
143
            $this->identicalTo($entity)
144
        );
145
146
        /** @var Request $request */
147
        $request = $this->createMock(Request::class);
148
        $request->method("get")->willReturn('some-id');
0 ignored issues
show
Bug introduced by
The method method() does not exist on Symfony\Component\HttpFoundation\Request. Did you maybe mean enableHttpMethodParameterOverride()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
149
150
        $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...
151
152
        /** @var Response $actualResponse */
153
        $actualResponse = ($this->controller)();
154
155
        $this->assertEquals(200, $actualResponse->getStatusCode());
156
        $this->assertEquals('Entity removed!', $actualResponse->getContent());
157
    }
158
159
    /**
160
     * @test
161
     */
162
    public function shouldRejectCallWithoutRequest()
163
    {
164
        $this->expectException(InvalidArgumentException::class);
165
166
        $controller = new GenericEntityRemoveController($this->controllerHelper, [
167
            'entity-class' => SampleEntity::class,
168
        ]);
169
170
        $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...
171
172
        $controller();
173
    }
174
175
}
176