Completed
Push — 5.3 ( 958546...1cc96e )
by Jeroen
14:02 queued 07:05
created

unit/Helper/Security/Acl/AclNativeHelperTest.php (2 issues)

mismatching argument types.

Documentation Minor

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')
64
            ->disableOriginalConstructor()
65
            ->getMock();
66
67
        $this->conn = $this->getMockBuilder('Doctrine\DBAL\Connection')
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')
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'));
126
        $allRoles = array($roles[0], new Role('ROLE_SUBJECT'));
127
128
        $this->token->expects($this->once())
129
            ->method('getRoles')
130
            ->will($this->returnValue($roles));
131
132
        $this->rh->expects($this->once())
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())
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())
176
            ->method('getRoles')
177
            ->will($this->returnValue($roles));
178
179
        $this->rh->expects($this->once())
180
            ->method('getReachableRoles')
181
            ->with($roles)
182
            ->will($this->returnValue($roles));
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