Completed
Push — master ( 9b5821...aba493 )
by Ruud
307:53 queued 297:17
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\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')
63
            ->disableOriginalConstructor()
64
            ->getMock();
65
66
        $this->conn = $this->getMockBuilder('Doctrine\DBAL\Connection')
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')
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...
95
            ->getMock();
96
97
        $this->token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')
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')
105
            ->getMock();
106
107
        $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...
108
    }
109
110
    public function testApply()
111
    {
112
        $queryBuilder = new QueryBuilder($this->conn);
113
        $queryBuilder->add(
114
            'from',
115
            array(
116
                array(
117
                    'table' => 'myTable',
118
                    'alias' => 'n',
119
                ),
120
            )
121
        );
122
123
        $roles = array(new Role('ROLE_KING'));
124
        $allRoles = array($roles[0], new Role('ROLE_SUBJECT'));
125
126
        $this->token->expects($this->once())
127
            ->method('getRoles')
128
            ->will($this->returnValue($roles));
129
130
        $this->rh->expects($this->once())
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())
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(
164
                array(
165
                    'table' => 'myTable',
166
                    'alias' => 'n',
167
                ),
168
            )
169
        );
170
171
        $roles = array();
172
173
        $this->token->expects($this->once())
174
            ->method('getRoles')
175
            ->will($this->returnValue($roles));
176
177
        $this->rh->expects($this->once())
178
            ->method('getReachableRoles')
179
            ->with($roles)
180
            ->will($this->returnValue($roles));
181
182
        $this->token->expects($this->any())
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