Completed
Push — l10n_master ( 6cb695...818918 )
by Kunstmaan
50:17 queued 35:37
created

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