Completed
Push — master ( c6433c...a8c688 )
by Gerrit
38:24
created

shouldCheckIfAccessIsGranted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27

Duplication

Lines 27
Ratio 100 %

Importance

Changes 0
Metric Value
dl 27
loc 27
c 0
b 0
f 0
rs 9.488
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;
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
use Symfony\Component\Finder\Exception\AccessDeniedException;
20
21
final class GenericEntityRemoveControllerTest extends TestCase
22
{
23
24
    /**
25
     * @var GenericEntityRemoveController
26
     */
27
    private $controller;
28
29
    /**
30
     * @var ControllerHelperInterface
31
     */
32
    private $controllerHelper;
33
34
    public function setUp()
35
    {
36
        $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...
37
38
        $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...
39
            'entity-class' => stdClass::class
40
        ]);
41
    }
42
43
    /**
44
     * @test
45
     */
46
    public function shouldRejectMissingEntityClass()
47
    {
48
        $this->expectException(InvalidArgumentException::class);
49
50
        new GenericEntityRemoveController($this->controllerHelper, []);
51
    }
52
53
    /**
54
     * @test
55
     */
56
    public function shouldRejectControllerCalledAgain()
57
    {
58
        $this->expectException(InvalidArgumentException::class);
59
60
        $this->controller->__construct($this->controllerHelper, [
61
            'entity-class' => stdClass::class
62
        ]);
63
    }
64
65
    /**
66
     * @test
67
     */
68
    public function shouldThrowExceptionIfEntityNotFound()
69
    {
70
        $this->expectException(InvalidArgumentException::class);
71
72
        $this->controller->removeEntity("some-id");
73
    }
74
75
    /**
76
     * @test
77
     */
78
    public function shouldRemoveEntity()
79
    {
80
        $entity = new stdClass();
81
82
        $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...
83
            $this->equalTo(stdClass::class),
84
            $this->equalTo('some-id')
85
        )->willReturn($entity);
86
87
        $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...
88
            $this->identicalTo($entity)
89
        );
90
91
        /** @var Response $response */
92
        $response = $this->controller->removeEntity("some-id");
93
94
        $this->assertEquals(200, $response->getStatusCode());
95
        $this->assertEquals('Entity removed!', $response->getContent());
96
    }
97
98
    /**
99
     * @test
100
     */
101 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...
102
    {
103
        $this->expectException(AccessDeniedException::class);
104
105
        $entity = new SampleEntity();
106
107
        $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...
108
            $this->equalTo(SampleEntity::class),
109
            $this->equalTo("some-id")
110
        )->willReturn($entity);
111
112
        $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...
113
            $this->equalTo('some-attribute'),
114
            $this->identicalTo($entity)
115
        )->will($this->returnCallback(
116
            function () {
117
                throw new AccessDeniedException('Lorem ipsum!');
118
            }
119
        ));
120
121
        $controller = new GenericEntityRemoveController($this->controllerHelper, [
122
            'entity-class' => SampleEntity::class,
123
            'authorization-attribute' => 'some-attribute',
124
        ]);
125
126
        $controller->removeEntity("some-id");
127
    }
128
129
}
130