Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
created

unit/Helper/Security/Acl/AclNativeHelperTest.php (4 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\HttpKernel\Kernel;
15
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
16
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
17
use Symfony\Component\Security\Core\Role\Role;
18
use Symfony\Component\Security\Core\Role\RoleHierarchy;
19
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
20
21
class AclNativeHelperTest extends TestCase
22
{
23
    /**
24
     * @var EntityManager
25
     */
26
    protected $em;
27
28
    /**
29
     * @var TokenStorageInterface
30
     */
31
    protected $tokenStorage;
32
33
    /**
34
     * @var RoleHierarchyInterface
35
     */
36
    protected $rh;
37
38
    /**
39
     * @var TokenInterface
40
     */
41
    protected $token;
42
43
    /**
44
     * @var UserInterface
45
     */
46
    protected $user;
47
48
    /**
49
     * @var Connection
50
     */
51
    protected $conn;
52
53
    /**
54
     * @var AclNativeHelper
55
     */
56
    protected $object;
57
58
    /**
59
     * Sets up the fixture, for example, opens a network connection.
60
     * This method is called before a test is executed.
61
     */
62
    protected function setUp()
63
    {
64
        $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
65
            ->disableOriginalConstructor()
66
            ->getMock();
67
68
        $this->conn = $this->getMockBuilder('Doctrine\DBAL\Connection')
69
            ->disableOriginalConstructor()
70
            ->getMock();
71
72
        $this->conn->expects($this->any())
73
            ->method('getDatabase')
74
            ->will($this->returnValue('myDatabase'));
75
76
        /* @var $platform AbstractPlatform */
77
        $platform = $this->getMockForAbstractClass('Doctrine\DBAL\Platforms\AbstractPlatform');
78
79
        $this->conn->expects($this->any())
80
            ->method('getDatabasePlatform')
81
            ->will($this->returnValue($platform));
82
83
        $this->em->expects($this->any())
84
            ->method('getConnection')
85
            ->will($this->returnValue($this->conn));
86
87
        /* @var $meta ClassMetadata */
88
        $meta = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')
89
            ->disableOriginalConstructor()
90
            ->getMock();
91
92
        $this->em->expects($this->any())
93
            ->method('getClassMetadata')
94
            ->will($this->returnValue($meta));
95
96
        $this->tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder('S...eInterface')->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Symfony\Component...\TokenStorageInterface> of property $tokenStorage.

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...
97
            ->getMock();
98
99
        $this->token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\AbstractToken')
100
            ->getMock();
101
102
        $this->tokenStorage->expects($this->any())
103
            ->method('getToken')
104
            ->will($this->returnValue($this->token));
105
106
        $this->rh = $this->getMockBuilder(RoleHierarchy::class)
107
            ->disableOriginalConstructor()
108
            ->getMock();
109
110
        $this->object = new AclNativeHelper($this->em, $this->tokenStorage, $this->rh);
0 ignored issues
show
$this->em is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\EntityManager>.

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...
$this->tokenStorage is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...\TokenStorageInterface>.

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...
$this->rh is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...RoleHierarchyInterface>.

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...
111
    }
112
113
    public function testApply()
114
    {
115
        $queryBuilder = new QueryBuilder($this->conn);
116
        $queryBuilder->add(
117
            'from',
118
            array(
119
                array(
120
                    'table' => 'myTable',
121
                    'alias' => 'n',
122
                ),
123
            )
124
        );
125
126
        [$rolesMethodName, $roles, $reachableRolesMethodName, $allRoles,] = $this->getRoleMockData();
127
128
        $this->token->expects($this->once())
129
            ->method($rolesMethodName)
130
            ->will($this->returnValue($roles));
131
132
        $this->rh->expects($this->once())
133
            ->method($reachableRolesMethodName)
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())
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(
166
                array(
167
                    'table' => 'myTable',
168
                    'alias' => 'n',
169
                ),
170
            )
171
        );
172
173
        [$rolesMethodName, $roles, $reachableRolesMethodName, $allRoles,] = $this->getRoleMockData(true);
174
175
        $this->token->expects($this->once())
176
            ->method($rolesMethodName)
177
            ->will($this->returnValue($roles));
178
179
        $this->rh->expects($this->once())
180
            ->method($reachableRolesMethodName)
181
            ->with($roles)
182
            ->will($this->returnValue($allRoles));
183
184
        $this->token->expects($this->any())
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
    private function getRoleMockData($anonymous = false)
203
    {
204
        if (Kernel::VERSION_ID >= 40300) {
205
            $rolesMethodName = 'getRoleNames';
206
            $reachableRolesMethodName = 'getReachableRoleNames';
207
            $roles = ['ROLE_KING'];
208
            $allRoles = [$roles[0], 'ROLE_SUBJECT'];
209
        } else {
210
            $rolesMethodName = 'getRoles';
211
            $reachableRolesMethodName = 'getReachableRoles';
212
            $roles = $anonymous ? [] : [new Role('ROLE_KING')];
213
            $allRoles = $anonymous ? [] : [$roles[0], new Role('ROLE_SUBJECT')];
214
        }
215
216
        return [
217
            $rolesMethodName,
218
            $roles,
219
            $reachableRolesMethodName,
220
            $allRoles,
221
        ];
222
    }
223
}
224