Completed
Push — 5.0 ( 5d9832...217db4 )
by Kristof
96:00 queued 83:47
created

Tests/Helper/Security/Acl/AclNativeHelperTest.php (8 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 Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
14
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
15
use Symfony\Component\Security\Core\Role\Role;
16
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
17
18
class AclNativeHelperTest extends \PHPUnit_Framework_TestCase
19
{
20
    /**
21
     * @var EntityManager
22
     */
23
    protected $em;
24
25
    /**
26
     * @var TokenStorageInterface
27
     */
28
    protected $tokenStorage;
29
30
    /**
31
     * @var RoleHierarchyInterface
32
     */
33
    protected $rh;
34
35
    /**
36
     * @var TokenInterface
37
     */
38
    protected $token;
39
40
    /**
41
     * @var UserInterface
42
     */
43
    protected $user;
44
45
    /**
46
     * @var Connection
47
     */
48
    protected $conn;
49
50
    /**
51
     * @var AclNativeHelper
52
     */
53
    protected $object;
54
55
    /**
56
     * Sets up the fixture, for example, opens a network connection.
57
     * This method is called before a test is executed.
58
     */
59
    protected function setUp()
60
    {
61
        $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
62
            ->disableOriginalConstructor()
63
            ->getMock();
64
65
        $this->conn = $this->getMockBuilder('Doctrine\DBAL\Connection')
66
            ->disableOriginalConstructor()
67
            ->getMock();
68
69
        $this->conn->expects($this->any())
70
            ->method('getDatabase')
71
            ->will($this->returnValue('myDatabase'));
72
73
        /* @var $platform AbstractPlatform */
74
        $platform = $this->getMockForAbstractClass('Doctrine\DBAL\Platforms\AbstractPlatform');
75
76
        $this->conn->expects($this->any())
77
            ->method('getDatabasePlatform')
78
            ->will($this->returnValue($platform));
79
80
        $this->em->expects($this->any())
81
            ->method('getConnection')
82
            ->will($this->returnValue($this->conn));
83
84
        /* @var $meta ClassMetadata */
85
        $meta = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')
86
            ->disableOriginalConstructor()
87
            ->getMock();
88
89
        $this->em->expects($this->any())
90
            ->method('getClassMetadata')
91
            ->will($this->returnValue($meta));
92
93
        $this->tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')
94
            ->getMock();
95
96
        $this->token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')
97
            ->getMock();
98
99
        $this->tokenStorage->expects($this->any())
100
            ->method('getToken')
101
            ->will($this->returnValue($this->token));
102
103
        $this->rh = $this->getMockBuilder('Symfony\Component\Security\Core\Role\RoleHierarchyInterface')
104
            ->getMock();
105
106
        $this->object = new AclNativeHelper($this->em, $this->tokenStorage, $this->rh);
107
    }
108
109
    /**
110
     * Tears down the fixture, for example, closes a network connection.
111
     * This method is called after a test is executed.
112
     */
113
    protected function tearDown()
114
    {
115
    }
116
117
    /**
118
     * @covers \Kunstmaan\AdminBundle\Helper\Security\Acl\AclNativeHelper::__construct
119
     */
120
    public function testConstructor()
121
    {
122
        new AclNativeHelper($this->em, $this->tokenStorage, $this->rh);
123
    }
124
125
    /**
126
     * @covers \Kunstmaan\AdminBundle\Helper\Security\Acl\AclNativeHelper::apply
127
     */
128
    public function testApply()
129
    {
130
        $queryBuilder = new QueryBuilder($this->conn);
131
        $queryBuilder->add(
132
            'from',
133
            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...
134
                array(
135
                    'table' => 'myTable',
136
                    'alias' => 'n',
137
                ),
138
            )
139
        );
140
141
        $roles = array(new Role('ROLE_KING'));
142
        $allRoles = array($roles[0], new Role('ROLE_SUBJECT'));
143
144
        $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...
145
            ->method('getRoles')
146
            ->will($this->returnValue($roles));
147
148
        $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...
149
            ->method('getReachableRoles')
150
            ->with($roles)
151
            ->will($this->returnValue($allRoles));
152
153
        $user = $this->getMockBuilder('FOS\UserBundle\Model\UserInterface')
154
            ->getMock();
155
156
        $user->expects($this->any())
157
            ->method('getUsername')
158
            ->will($this->returnValue('MyUser'));
159
160
        $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...
161
            ->method('getUser')
162
            ->will($this->returnValue($user));
163
164
        $permissionDef = new PermissionDefinition(array('view'), 'Kunstmaan\NodeBundle\Entity\Node', 'n');
165
166
        /* @var $qb QueryBuilder */
167
        $qb = $this->object->apply($queryBuilder, $permissionDef);
168
        $query = $qb->getSQL();
169
170
        $this->assertContains('"ROLE_SUBJECT"', $query);
171
        $this->assertContains('"ROLE_KING"', $query);
172
        $this->assertContains('"IS_AUTHENTICATED_ANONYMOUSLY"', $query);
173
        $this->assertContains('MyUser', $query);
174
    }
175
176
    /**
177
     * @covers \Kunstmaan\AdminBundle\Helper\Security\Acl\AclNativeHelper::apply
178
     */
179
    public function testApplyAnonymous()
180
    {
181
        $queryBuilder = new QueryBuilder($this->conn);
182
        $queryBuilder->add(
183
            'from',
184
            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...
185
                array(
186
                    'table' => 'myTable',
187
                    'alias' => 'n',
188
                ),
189
            )
190
        );
191
192
        $roles = array();
193
194
        $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...
195
            ->method('getRoles')
196
            ->will($this->returnValue($roles));
197
198
        $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...
199
            ->method('getReachableRoles')
200
            ->with($roles)
201
            ->will($this->returnValue($roles));
202
203
        $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...
204
            ->method('getUser')
205
            ->will($this->returnValue('anon.'));
206
207
        $permissionDef = new PermissionDefinition(array('view'), 'Kunstmaan\NodeBundle\Entity\Node', 'n');
208
209
        /* @var $qb QueryBuilder */
210
        $qb = $this->object->apply($queryBuilder, $permissionDef);
211
        $query = $qb->getSQL();
212
213
        $this->assertContains('"IS_AUTHENTICATED_ANONYMOUSLY"', $query);
214
    }
215
216
    /**
217
     * @covers \Kunstmaan\AdminBundle\Helper\Security\Acl\AclNativeHelper::getTokenStorage
218
     */
219
    public function testGetTokenStorage()
220
    {
221
        $this->assertSame($this->tokenStorage, $this->object->getTokenStorage());
222
    }
223
}
224