Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

unit/Helper/Security/Acl/AclNativeHelperTest.php (12 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminBundle\Tests\Helper\Security\Acl;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Platforms\AbstractPlatform;
7
use Doctrine\DBAL\Query\QueryBuilder;
8
use Doctrine\ORM\EntityManager;
9
use Doctrine\ORM\Mapping\ClassMetadata;
10
use FOS\UserBundle\Model\UserInterface;
11
use Kunstmaan\AdminBundle\Helper\Security\Acl\AclNativeHelper;
12
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionDefinition;
13
use PHPUnit\Framework\TestCase;
14
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
15
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
16
use Symfony\Component\Security\Core\Role\Role;
17
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
18
19
class AclNativeHelperTest extends TestCase
20
{
21
    /**
22
     * @var EntityManager
23
     */
24
    protected $em;
25
26
    /**
27
     * @var TokenStorageInterface
28
     */
29
    protected $tokenStorage;
30
31
    /**
32
     * @var RoleHierarchyInterface
33
     */
34
    protected $rh;
35
36
    /**
37
     * @var TokenInterface
38
     */
39
    protected $token;
40
41
    /**
42
     * @var UserInterface
43
     */
44
    protected $user;
45
46
    /**
47
     * @var Connection
48
     */
49
    protected $conn;
50
51
    /**
52
     * @var AclNativeHelper
53
     */
54
    protected $object;
55
56
    /**
57
     * Sets up the fixture, for example, opens a network connection.
58
     * This method is called before a test is executed.
59
     */
60
    protected function setUp()
61
    {
62
        $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder('D...onstructor()->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Doctrine\ORM\EntityManager> of property $em.

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...
63
            ->disableOriginalConstructor()
64
            ->getMock();
65
66
        $this->conn = $this->getMockBuilder('Doctrine\DBAL\Connection')
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder('D...onstructor()->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Doctrine\DBAL\Connection> of property $conn.

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...
67
            ->disableOriginalConstructor()
68
            ->getMock();
69
70
        $this->conn->expects($this->any())
71
            ->method('getDatabase')
72
            ->will($this->returnValue('myDatabase'));
73
74
        /* @var $platform AbstractPlatform */
75
        $platform = $this->getMockForAbstractClass('Doctrine\DBAL\Platforms\AbstractPlatform');
76
77
        $this->conn->expects($this->any())
78
            ->method('getDatabasePlatform')
79
            ->will($this->returnValue($platform));
80
81
        $this->em->expects($this->any())
82
            ->method('getConnection')
83
            ->will($this->returnValue($this->conn));
84
85
        /* @var $meta ClassMetadata */
86
        $meta = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')
87
            ->disableOriginalConstructor()
88
            ->getMock();
89
90
        $this->em->expects($this->any())
91
            ->method('getClassMetadata')
92
            ->will($this->returnValue($meta));
93
94
        $this->tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')
95
            ->getMock();
96
97
        $this->token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder('S...nInterface')->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Symfony\Component...n\Token\TokenInterface> of property $token.

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...
98
            ->getMock();
99
100
        $this->tokenStorage->expects($this->any())
101
            ->method('getToken')
102
            ->will($this->returnValue($this->token));
103
104
        $this->rh = $this->getMockBuilder('Symfony\Component\Security\Core\Role\RoleHierarchyInterface')
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder('S...yInterface')->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Symfony\Component...RoleHierarchyInterface> of property $rh.

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...
105
            ->getMock();
106
107
        $this->object = new AclNativeHelper($this->em, $this->tokenStorage, $this->rh);
108
    }
109
110
    public function testConstructor()
111
    {
112
        new AclNativeHelper($this->em, $this->tokenStorage, $this->rh);
113
    }
114
115
    public function testApply()
116
    {
117
        $queryBuilder = new QueryBuilder($this->conn);
118
        $queryBuilder->add(
119
            'from',
120
            array(
0 ignored issues
show
array(array('table' => '...able', 'alias' => 'n')) is of type array<integer,array<stri...alias\":\"string\"}>"}>, but the function expects a string.

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...
121
                array(
122
                    'table' => 'myTable',
123
                    'alias' => 'n',
124
                ),
125
            )
126
        );
127
128
        $roles = array(new Role('ROLE_KING'));
129
        $allRoles = array($roles[0], new Role('ROLE_SUBJECT'));
130
131
        $this->token->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Symfony\Component...n\Token\TokenInterface>.

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...
132
            ->method('getRoles')
133
            ->will($this->returnValue($roles));
134
135
        $this->rh->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Symfony\Component...RoleHierarchyInterface>.

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...
136
            ->method('getReachableRoles')
137
            ->with($roles)
138
            ->will($this->returnValue($allRoles));
139
140
        $user = $this->getMockBuilder('FOS\UserBundle\Model\UserInterface')
141
            ->getMock();
142
143
        $user->expects($this->any())
144
            ->method('getUsername')
145
            ->will($this->returnValue('MyUser'));
146
147
        $this->token->expects($this->any())
0 ignored issues
show
The method expects() does not seem to exist on object<Symfony\Component...n\Token\TokenInterface>.

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...
148
            ->method('getUser')
149
            ->will($this->returnValue($user));
150
151
        $permissionDef = new PermissionDefinition(array('view'), 'Kunstmaan\NodeBundle\Entity\Node', 'n');
152
153
        /* @var $qb QueryBuilder */
154
        $qb = $this->object->apply($queryBuilder, $permissionDef);
155
        $query = $qb->getSQL();
156
157
        $this->assertContains('"ROLE_SUBJECT"', $query);
158
        $this->assertContains('"ROLE_KING"', $query);
159
        $this->assertContains('"IS_AUTHENTICATED_ANONYMOUSLY"', $query);
160
        $this->assertContains('MyUser', $query);
161
    }
162
163
    public function testApplyAnonymous()
164
    {
165
        $queryBuilder = new QueryBuilder($this->conn);
166
        $queryBuilder->add(
167
            'from',
168
            array(
0 ignored issues
show
array(array('table' => '...able', 'alias' => 'n')) is of type array<integer,array<stri...alias\":\"string\"}>"}>, but the function expects a string.

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...
169
                array(
170
                    'table' => 'myTable',
171
                    'alias' => 'n',
172
                ),
173
            )
174
        );
175
176
        $roles = array();
177
178
        $this->token->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Symfony\Component...n\Token\TokenInterface>.

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...
179
            ->method('getRoles')
180
            ->will($this->returnValue($roles));
181
182
        $this->rh->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Symfony\Component...RoleHierarchyInterface>.

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...
183
            ->method('getReachableRoles')
184
            ->with($roles)
185
            ->will($this->returnValue($roles));
186
187
        $this->token->expects($this->any())
0 ignored issues
show
The method expects() does not seem to exist on object<Symfony\Component...n\Token\TokenInterface>.

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...
188
            ->method('getUser')
189
            ->will($this->returnValue('anon.'));
190
191
        $permissionDef = new PermissionDefinition(array('view'), 'Kunstmaan\NodeBundle\Entity\Node', 'n');
192
193
        /* @var $qb QueryBuilder */
194
        $qb = $this->object->apply($queryBuilder, $permissionDef);
195
        $query = $qb->getSQL();
196
197
        $this->assertContains('"IS_AUTHENTICATED_ANONYMOUSLY"', $query);
198
    }
199
200
    public function testGetTokenStorage()
201
    {
202
        $this->assertSame($this->tokenStorage, $this->object->getTokenStorage());
203
    }
204
}
205