Completed
Push — master ( 770316...74fc07 )
by Jeroen
09:08 queued 02:44
created

unit/Helper/Security/Acl/AclNativeHelperTest.php (13 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\RoleHierarchy;
18
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
19
20
class AclNativeHelperTest extends TestCase
21
{
22
    /**
23
     * @var EntityManager
24
     */
25
    protected $em;
26
27
    /**
28
     * @var TokenStorageInterface
29
     */
30
    protected $tokenStorage;
31
32
    /**
33
     * @var RoleHierarchyInterface
34
     */
35
    protected $rh;
36
37
    /**
38
     * @var TokenInterface
39
     */
40
    protected $token;
41
42
    /**
43
     * @var UserInterface
44
     */
45
    protected $user;
46
47
    /**
48
     * @var Connection
49
     */
50
    protected $conn;
51
52
    /**
53
     * @var AclNativeHelper
54
     */
55
    protected $object;
56
57
    /**
58
     * Sets up the fixture, for example, opens a network connection.
59
     * This method is called before a test is executed.
60
     */
61
    protected function setUp()
62
    {
63
        $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...
64
            ->disableOriginalConstructor()
65
            ->getMock();
66
67
        $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...
68
            ->disableOriginalConstructor()
69
            ->getMock();
70
71
        $this->conn->expects($this->any())
72
            ->method('getDatabase')
73
            ->will($this->returnValue('myDatabase'));
74
75
        /* @var $platform AbstractPlatform */
76
        $platform = $this->getMockForAbstractClass('Doctrine\DBAL\Platforms\AbstractPlatform');
77
78
        $this->conn->expects($this->any())
79
            ->method('getDatabasePlatform')
80
            ->will($this->returnValue($platform));
81
82
        $this->em->expects($this->any())
83
            ->method('getConnection')
84
            ->will($this->returnValue($this->conn));
85
86
        /* @var $meta ClassMetadata */
87
        $meta = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')
88
            ->disableOriginalConstructor()
89
            ->getMock();
90
91
        $this->em->expects($this->any())
92
            ->method('getClassMetadata')
93
            ->will($this->returnValue($meta));
94
95
        $this->tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')
96
            ->getMock();
97
98
        $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...
99
            ->getMock();
100
101
        $this->tokenStorage->expects($this->any())
102
            ->method('getToken')
103
            ->will($this->returnValue($this->token));
104
105
        $this->rh = $this->getMockBuilder(RoleHierarchy::class)
106
            ->disableOriginalConstructor()
107
            ->getMock();
108
109
        $this->object = new AclNativeHelper($this->em, $this->tokenStorage, $this->rh);
110
    }
111
112
    public function testApply()
113
    {
114
        $queryBuilder = new QueryBuilder($this->conn);
115
        $queryBuilder->add(
116
            'from',
117
            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...
118
                array(
119
                    'table' => 'myTable',
120
                    'alias' => 'n',
121
                ),
122
            )
123
        );
124
125
        $roles = array(new Role('ROLE_KING'));
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Security\Core\Role\Role has been deprecated with message: since Symfony 4.3, to be removed in 5.0. Use strings as roles instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
126
        $allRoles = array($roles[0], new Role('ROLE_SUBJECT'));
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Security\Core\Role\Role has been deprecated with message: since Symfony 4.3, to be removed in 5.0. Use strings as roles instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
127
128
        $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...
129
            ->method('getRoles')
130
            ->will($this->returnValue($roles));
131
132
        $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...
133
            ->method('getReachableRoles')
134
            ->with($roles)
135
            ->will($this->returnValue($allRoles));
136
137
        $user = $this->getMockBuilder('FOS\UserBundle\Model\UserInterface')
138
            ->getMock();
139
140
        $user->expects($this->any())
141
            ->method('getUsername')
142
            ->will($this->returnValue('MyUser'));
143
144
        $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...
145
            ->method('getUser')
146
            ->will($this->returnValue($user));
147
148
        $permissionDef = new PermissionDefinition(array('view'), 'Kunstmaan\NodeBundle\Entity\Node', 'n');
149
150
        /* @var $qb QueryBuilder */
151
        $qb = $this->object->apply($queryBuilder, $permissionDef);
152
        $query = $qb->getSQL();
153
154
        $this->assertContains('"ROLE_SUBJECT"', $query);
155
        $this->assertContains('"ROLE_KING"', $query);
156
        $this->assertContains('"IS_AUTHENTICATED_ANONYMOUSLY"', $query);
157
        $this->assertContains('MyUser', $query);
158
    }
159
160
    public function testApplyAnonymous()
161
    {
162
        $queryBuilder = new QueryBuilder($this->conn);
163
        $queryBuilder->add(
164
            'from',
165
            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...
166
                array(
167
                    'table' => 'myTable',
168
                    'alias' => 'n',
169
                ),
170
            )
171
        );
172
173
        $roles = array();
174
175
        $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...
176
            ->method('getRoles')
177
            ->will($this->returnValue($roles));
178
179
        $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...
180
            ->method('getReachableRoles')
181
            ->with($roles)
182
            ->will($this->returnValue($roles));
183
184
        $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...
185
            ->method('getUser')
186
            ->will($this->returnValue('anon.'));
187
188
        $permissionDef = new PermissionDefinition(array('view'), 'Kunstmaan\NodeBundle\Entity\Node', 'n');
189
190
        /* @var $qb QueryBuilder */
191
        $qb = $this->object->apply($queryBuilder, $permissionDef);
192
        $query = $qb->getSQL();
193
194
        $this->assertContains('"IS_AUTHENTICATED_ANONYMOUSLY"', $query);
195
    }
196
197
    public function testGetTokenStorage()
198
    {
199
        $this->assertSame($this->tokenStorage, $this->object->getTokenStorage());
200
    }
201
}
202