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\Driver\Statement; |
||
7 | use Doctrine\DBAL\Platforms\AbstractPlatform; |
||
8 | use Doctrine\ORM\Configuration; |
||
9 | use Doctrine\ORM\EntityManager; |
||
10 | use Doctrine\ORM\Mapping\ClassMetadata; |
||
11 | use Doctrine\ORM\Mapping\QuoteStrategy; |
||
12 | use Doctrine\ORM\NativeQuery; |
||
13 | use Doctrine\ORM\Query; |
||
14 | use Doctrine\ORM\QueryBuilder; |
||
15 | use FOS\UserBundle\Model\UserInterface; |
||
16 | use Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper; |
||
17 | use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\MaskBuilder; |
||
18 | use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionDefinition; |
||
19 | use PHPUnit\Framework\TestCase; |
||
20 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
||
21 | use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
||
22 | use Symfony\Component\Security\Core\Role\Role; |
||
23 | use Symfony\Component\Security\Core\Role\RoleHierarchy; |
||
24 | use Symfony\Component\Security\Core\Role\RoleHierarchyInterface; |
||
25 | |||
26 | class AclHelperTest extends TestCase |
||
27 | { |
||
28 | /** |
||
29 | * @var EntityManager |
||
30 | */ |
||
31 | protected $em; |
||
32 | |||
33 | /** |
||
34 | * @var TokenStorageInterface |
||
35 | */ |
||
36 | protected $tokenStorage; |
||
37 | |||
38 | /** |
||
39 | * @var RoleHierarchyInterface |
||
40 | */ |
||
41 | protected $rh; |
||
42 | |||
43 | /** |
||
44 | * @var TokenInterface |
||
45 | */ |
||
46 | protected $token; |
||
47 | |||
48 | /** |
||
49 | * @var UserInterface |
||
50 | */ |
||
51 | protected $user; |
||
52 | |||
53 | /** |
||
54 | * @var AclHelper |
||
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 | /* @var $conn Connection */ |
||
69 | $conn = $this->getMockBuilder('Doctrine\DBAL\Connection') |
||
70 | ->disableOriginalConstructor() |
||
71 | ->getMock(); |
||
72 | |||
73 | $conn->expects($this->any()) |
||
74 | ->method('getDatabase') |
||
75 | ->will($this->returnValue('myDatabase')); |
||
76 | |||
77 | /* @var $platform AbstractPlatform */ |
||
78 | $platform = $this->getMockForAbstractClass('Doctrine\DBAL\Platforms\AbstractPlatform'); |
||
79 | |||
80 | $conn->expects($this->any()) |
||
81 | ->method('getDatabasePlatform') |
||
82 | ->will($this->returnValue($platform)); |
||
83 | |||
84 | /* @var $stmt Statement */ |
||
85 | $stmt = $this->createMock(Statement::class); |
||
86 | |||
87 | $conn->expects($this->any()) |
||
88 | ->method('executeQuery') |
||
89 | ->will($this->returnValue($stmt)); |
||
90 | |||
91 | $this->em->expects($this->any()) |
||
92 | ->method('getConnection') |
||
93 | ->will($this->returnValue($conn)); |
||
94 | |||
95 | /* @var $conf Configuration */ |
||
96 | $conf = $this->getMockBuilder('Doctrine\ORM\Configuration') |
||
97 | ->disableOriginalConstructor() |
||
98 | ->getMock(); |
||
99 | |||
100 | /* @var $strat QuoteStrategy */ |
||
101 | $strat = $this->getMockBuilder('Doctrine\ORM\Mapping\QuoteStrategy') |
||
102 | ->disableOriginalConstructor() |
||
103 | ->getMock(); |
||
104 | |||
105 | $strat->expects($this->any()) |
||
106 | ->method('getTableName') |
||
107 | ->will($this->returnValue('rootTable')); |
||
108 | |||
109 | $conf->expects($this->any()) |
||
110 | ->method('getQuoteStrategy') |
||
111 | ->will($this->returnValue($strat)); |
||
112 | |||
113 | $conf->expects($this->any()) |
||
114 | ->method('getDefaultQueryHints') |
||
115 | ->willReturn(array()); |
||
116 | |||
117 | $conf->expects($this->any()) |
||
118 | ->method('isSecondLevelCacheEnabled') |
||
119 | ->willReturn(false); |
||
120 | |||
121 | $this->em->expects($this->any()) |
||
122 | ->method('getConfiguration') |
||
123 | ->will($this->returnValue($conf)); |
||
124 | |||
125 | /* @var $meta ClassMetadata */ |
||
126 | $meta = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata') |
||
127 | ->disableOriginalConstructor() |
||
128 | ->getMock(); |
||
129 | |||
130 | $this->em->expects($this->any()) |
||
131 | ->method('getClassMetadata') |
||
132 | ->will($this->returnValue($meta)); |
||
133 | |||
134 | $this->tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface') |
||
0 ignored issues
–
show
|
|||
135 | ->getMock(); |
||
136 | |||
137 | $this->token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface') |
||
138 | ->getMock(); |
||
139 | |||
140 | $this->tokenStorage->expects($this->any()) |
||
141 | ->method('getToken') |
||
142 | ->will($this->returnValue($this->token)); |
||
143 | |||
144 | $this->rh = $this->getMockBuilder(RoleHierarchy::class) |
||
145 | ->disableOriginalConstructor() |
||
146 | ->getMock(); |
||
147 | |||
148 | $this->object = new AclHelper($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...
|
|||
149 | } |
||
150 | |||
151 | public function testApply() |
||
152 | { |
||
153 | /* @var $queryBuilder QueryBuilder */ |
||
154 | $queryBuilder = $this->getMockBuilder('Doctrine\ORM\QueryBuilder') |
||
155 | ->disableOriginalConstructor() |
||
156 | ->getMock(); |
||
157 | |||
158 | $query = new Query($this->em); |
||
159 | $query->setParameter('paramName', 'paramValue', 'paramType'); |
||
160 | $queryBuilder->expects($this->any()) |
||
161 | ->method('getQuery') |
||
162 | ->will($this->returnValue($query)); |
||
163 | |||
164 | $queryBuilder->expects($this->once()) |
||
165 | ->method('getRootEntities') |
||
166 | ->will($this->returnValue(array('Kunstmaan\NodeBundle\Entity\Node'))); |
||
167 | |||
168 | $queryBuilder->expects($this->once()) |
||
169 | ->method('getRootAliases') |
||
170 | ->will($this->returnValue(array('n'))); |
||
171 | |||
172 | $user = $this->getMockBuilder('FOS\UserBundle\Model\UserInterface') |
||
173 | ->getMock(); |
||
174 | |||
175 | $user->expects($this->any()) |
||
176 | ->method('getUsername') |
||
177 | ->will($this->returnValue('MyUser')); |
||
178 | |||
179 | $this->token->expects($this->any()) |
||
180 | ->method('getUser') |
||
181 | ->will($this->returnValue($user)); |
||
182 | |||
183 | $roles = array(new Role('ROLE_KING')); |
||
184 | $allRoles = array($roles[0], new Role('ROLE_SUBJECT')); |
||
185 | |||
186 | $this->token->expects($this->once()) |
||
187 | ->method('getRoles') |
||
188 | ->will($this->returnValue($roles)); |
||
189 | |||
190 | $this->rh->expects($this->once()) |
||
191 | ->method('getReachableRoles') |
||
192 | ->with($roles) |
||
193 | ->will($this->returnValue($allRoles)); |
||
194 | |||
195 | $permissionDef = new PermissionDefinition(array('view'), 'Kunstmaan\NodeBundle\Entity\Node'); |
||
196 | |||
197 | /* @var $query Query */ |
||
198 | $query = $this->object->apply($queryBuilder, $permissionDef); |
||
199 | |||
200 | $this->assertEquals(MaskBuilder::MASK_VIEW, $query->getHint('acl.mask')); |
||
201 | $this->assertEquals($permissionDef->getEntity(), $query->getHint('acl.root.entity')); |
||
202 | $this->assertEquals('rootTable', $query->getHint('acl.entityRootTableName')); |
||
203 | $this->assertEquals('n', $query->getHint('acl.entityRootTableDqlAlias')); |
||
204 | |||
205 | $aclQuery = $query->getHint('acl.extra.query'); |
||
206 | $this->assertContains('"ROLE_SUBJECT"', $aclQuery); |
||
207 | $this->assertContains('"ROLE_KING"', $aclQuery); |
||
208 | $this->assertContains('"IS_AUTHENTICATED_ANONYMOUSLY"', $aclQuery); |
||
209 | $this->assertContains('MyUser', $aclQuery); |
||
210 | } |
||
211 | |||
212 | public function testApplyAnonymous() |
||
213 | { |
||
214 | /* @var $queryBuilder QueryBuilder */ |
||
215 | $queryBuilder = $this->getMockBuilder('Doctrine\ORM\QueryBuilder') |
||
216 | ->disableOriginalConstructor() |
||
217 | ->getMock(); |
||
218 | |||
219 | $query = new Query($this->em); |
||
220 | $query->setParameter('paramName', 'paramValue', 'paramType'); |
||
221 | $queryBuilder->expects($this->any()) |
||
222 | ->method('getQuery') |
||
223 | ->will($this->returnValue($query)); |
||
224 | |||
225 | $queryBuilder->expects($this->once()) |
||
226 | ->method('getRootEntities') |
||
227 | ->will($this->returnValue(array('Kunstmaan\NodeBundle\Entity\Node'))); |
||
228 | |||
229 | $queryBuilder->expects($this->once()) |
||
230 | ->method('getRootAliases') |
||
231 | ->will($this->returnValue(array('n'))); |
||
232 | |||
233 | $roles = array(); |
||
234 | |||
235 | $this->token->expects($this->once()) |
||
236 | ->method('getRoles') |
||
237 | ->will($this->returnValue($roles)); |
||
238 | |||
239 | $this->rh->expects($this->once()) |
||
240 | ->method('getReachableRoles') |
||
241 | ->with($roles) |
||
242 | ->will($this->returnValue($roles)); |
||
243 | |||
244 | $this->token->expects($this->any()) |
||
245 | ->method('getUser') |
||
246 | ->will($this->returnValue('anon.')); |
||
247 | |||
248 | $permissionDef = new PermissionDefinition(array('view'), 'Kunstmaan\NodeBundle\Entity\Node'); |
||
249 | |||
250 | /* @var $query Query */ |
||
251 | $query = $this->object->apply($queryBuilder, $permissionDef); |
||
252 | |||
253 | $this->assertEquals(MaskBuilder::MASK_VIEW, $query->getHint('acl.mask')); |
||
254 | $this->assertEquals($permissionDef->getEntity(), $query->getHint('acl.root.entity')); |
||
255 | $this->assertEquals('rootTable', $query->getHint('acl.entityRootTableName')); |
||
256 | $this->assertEquals('n', $query->getHint('acl.entityRootTableDqlAlias')); |
||
257 | |||
258 | $aclQuery = $query->getHint('acl.extra.query'); |
||
259 | $this->assertContains('"IS_AUTHENTICATED_ANONYMOUSLY"', $aclQuery); |
||
260 | } |
||
261 | |||
262 | public function testGetAllowedEntityIds() |
||
263 | { |
||
264 | $roles = array(new Role('ROLE_KING')); |
||
265 | $allRoles = array($roles[0], new Role('ROLE_SUBJECT')); |
||
266 | |||
267 | $this->token->expects($this->once()) |
||
268 | ->method('getRoles') |
||
269 | ->will($this->returnValue($roles)); |
||
270 | |||
271 | $this->rh->expects($this->once()) |
||
272 | ->method('getReachableRoles') |
||
273 | ->with($roles) |
||
274 | ->will($this->returnValue($allRoles)); |
||
275 | |||
276 | $user = $this->getMockBuilder('FOS\UserBundle\Model\UserInterface') |
||
277 | ->getMock(); |
||
278 | |||
279 | $user->expects($this->any()) |
||
280 | ->method('getUsername') |
||
281 | ->will($this->returnValue('MyUser')); |
||
282 | |||
283 | $this->token->expects($this->any()) |
||
284 | ->method('getUser') |
||
285 | ->will($this->returnValue($user)); |
||
286 | |||
287 | $hydrator = $this->getMockBuilder('Doctrine\ORM\Internal\Hydration\ScalarHydrator') |
||
288 | ->disableOriginalConstructor() |
||
289 | ->getMock(); |
||
290 | |||
291 | $rows = array( |
||
292 | array('id' => 1), |
||
293 | array('id' => 9), |
||
294 | ); |
||
295 | |||
296 | $hydrator->expects($this->once()) |
||
297 | ->method('hydrateAll') |
||
298 | ->will($this->returnValue($rows)); |
||
299 | |||
300 | $this->em->expects($this->any()) |
||
301 | ->method('newHydrator') // was ->method('getHydrator') |
||
302 | ->will($this->returnValue($hydrator)); |
||
303 | |||
304 | /* @var $query NativeQuery */ |
||
305 | $query = new NativeQuery($this->em); |
||
306 | |||
307 | $this->em->expects($this->once()) |
||
308 | ->method('createNativeQuery') |
||
309 | ->will($this->returnValue($query)); |
||
310 | |||
311 | $permissionDef = new PermissionDefinition(array('view'), 'Kunstmaan\NodeBundle\Entity\Node', 'n'); |
||
312 | |||
313 | /* @var $result array */ |
||
314 | $result = $this->object->getAllowedEntityIds($permissionDef); |
||
315 | |||
316 | $this->assertEquals(array(1, 9), $result); |
||
317 | } |
||
318 | |||
319 | public function testGetAllowedEntityIdsNoEntity() |
||
320 | { |
||
321 | $this->expectException('InvalidArgumentException'); |
||
322 | |||
323 | $this->object->getAllowedEntityIds(new PermissionDefinition(array('view'))); |
||
324 | } |
||
325 | |||
326 | public function testGetTokenStorage() |
||
327 | { |
||
328 | $this->assertSame($this->tokenStorage, $this->object->getTokenStorage()); |
||
329 | } |
||
330 | } |
||
331 |
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..