Complex classes like AdminTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AdminTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
69 | class AdminTest extends TestCase |
||
70 | { |
||
71 | protected $cacheTempFolder; |
||
72 | |||
73 | public function setUp() |
||
79 | |||
80 | /** |
||
81 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::__construct |
||
82 | */ |
||
83 | public function testConstructor() |
||
93 | |||
94 | public function testGetClass() |
||
117 | |||
118 | public function testGetClassException() |
||
119 | { |
||
120 | $this->expectException(\RuntimeException::class); |
||
121 | $this->expectExceptionMessage('Feature not implemented: an embedded admin cannot have subclass'); |
||
122 | |||
123 | $class = 'Application\Sonata\NewsBundle\Entity\Post'; |
||
124 | $baseControllerName = 'SonataNewsBundle:PostAdmin'; |
||
125 | |||
126 | $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName); |
||
127 | $admin->setParentFieldDescription(new FieldDescription()); |
||
128 | $admin->setSubClasses(['foo' => 'bar']); |
||
129 | $admin->setRequest(new Request(['subclass' => 'foo'])); |
||
130 | $admin->getClass(); |
||
131 | } |
||
132 | |||
133 | public function testCheckAccessThrowsExceptionOnMadeUpAction() |
||
134 | { |
||
135 | $admin = new PostAdmin( |
||
136 | 'sonata.post.admin.post', |
||
137 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
138 | 'SonataNewsBundle:PostAdmin' |
||
139 | ); |
||
140 | $this->expectException( |
||
141 | \InvalidArgumentException::class |
||
142 | ); |
||
143 | $this->expectExceptionMessage( |
||
144 | 'Action "made-up" could not be found' |
||
145 | ); |
||
146 | $admin->checkAccess('made-up'); |
||
147 | } |
||
148 | |||
149 | public function testCheckAccessThrowsAccessDeniedException() |
||
150 | { |
||
151 | $admin = new PostAdmin( |
||
152 | 'sonata.post.admin.post', |
||
153 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
154 | 'SonataNewsBundle:PostAdmin' |
||
155 | ); |
||
156 | $securityHandler = $this->prophesize(SecurityHandlerInterface::class); |
||
157 | $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true); |
||
158 | $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(false); |
||
159 | $customExtension = $this->prophesize(AbstractAdminExtension::class); |
||
160 | $customExtension->getAccessMapping($admin)->willReturn( |
||
161 | ['custom_action' => ['CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE']] |
||
162 | ); |
||
163 | $admin->addExtension($customExtension->reveal()); |
||
164 | $admin->setSecurityHandler($securityHandler->reveal()); |
||
165 | $this->expectException( |
||
166 | AccessDeniedException::class |
||
167 | ); |
||
168 | $this->expectExceptionMessage( |
||
169 | 'Access Denied to the action custom_action and role EXTRA_CUSTOM_ROLE' |
||
170 | ); |
||
171 | $admin->checkAccess('custom_action'); |
||
172 | } |
||
173 | |||
174 | public function testHasAccessOnMadeUpAction() |
||
175 | { |
||
176 | $admin = new PostAdmin( |
||
177 | 'sonata.post.admin.post', |
||
178 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
179 | 'SonataNewsBundle:PostAdmin' |
||
180 | ); |
||
181 | |||
182 | $this->assertFalse($admin->hasAccess('made-up')); |
||
183 | } |
||
184 | |||
185 | public function testHasAccess() |
||
186 | { |
||
187 | $admin = new PostAdmin( |
||
188 | 'sonata.post.admin.post', |
||
189 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
190 | 'SonataNewsBundle:PostAdmin' |
||
191 | ); |
||
192 | $securityHandler = $this->prophesize(SecurityHandlerInterface::class); |
||
193 | $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true); |
||
194 | $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(false); |
||
195 | $customExtension = $this->prophesize(AbstractAdminExtension::class); |
||
196 | $customExtension->getAccessMapping($admin)->willReturn( |
||
197 | ['custom_action' => ['CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE']] |
||
198 | ); |
||
199 | $admin->addExtension($customExtension->reveal()); |
||
200 | $admin->setSecurityHandler($securityHandler->reveal()); |
||
201 | |||
202 | $this->assertFalse($admin->hasAccess('custom_action')); |
||
203 | } |
||
204 | |||
205 | public function testHasAccessAllowsAccess() |
||
206 | { |
||
207 | $admin = new PostAdmin( |
||
208 | 'sonata.post.admin.post', |
||
209 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
210 | 'SonataNewsBundle:PostAdmin' |
||
211 | ); |
||
212 | $securityHandler = $this->prophesize(SecurityHandlerInterface::class); |
||
213 | $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true); |
||
214 | $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(true); |
||
215 | $customExtension = $this->prophesize(AbstractAdminExtension::class); |
||
216 | $customExtension->getAccessMapping($admin)->willReturn( |
||
217 | ['custom_action' => ['CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE']] |
||
218 | ); |
||
219 | $admin->addExtension($customExtension->reveal()); |
||
220 | $admin->setSecurityHandler($securityHandler->reveal()); |
||
221 | |||
222 | $this->assertTrue($admin->hasAccess('custom_action')); |
||
223 | } |
||
224 | |||
225 | public function testHasAccessAllowsAccessEditAction() |
||
226 | { |
||
227 | $admin = new PostAdmin( |
||
228 | 'sonata.post.admin.post', |
||
229 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
230 | 'SonataNewsBundle:PostAdmin' |
||
231 | ); |
||
232 | $securityHandler = $this->prophesize(SecurityHandlerInterface::class); |
||
233 | $securityHandler->isGranted($admin, 'EDIT_ROLE', $admin)->willReturn(true); |
||
234 | $customExtension = $this->prophesize(AbstractAdminExtension::class); |
||
235 | $customExtension->getAccessMapping($admin)->willReturn( |
||
236 | ['edit_action' => ['EDIT_ROLE']] |
||
237 | ); |
||
238 | $admin->addExtension($customExtension->reveal()); |
||
239 | $admin->setSecurityHandler($securityHandler->reveal()); |
||
240 | |||
241 | $this->assertTrue($admin->hasAccess('edit_action')); |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasChild |
||
246 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::addChild |
||
247 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getChild |
||
248 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::isChild |
||
249 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasChildren |
||
250 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getChildren |
||
251 | */ |
||
252 | public function testChildren() |
||
253 | { |
||
254 | $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
255 | $this->assertFalse($postAdmin->hasChildren()); |
||
256 | $this->assertFalse($postAdmin->hasChild('comment')); |
||
257 | |||
258 | $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin'); |
||
259 | $postAdmin->addChild($commentAdmin); |
||
260 | $this->assertTrue($postAdmin->hasChildren()); |
||
261 | $this->assertTrue($postAdmin->hasChild('sonata.post.admin.comment')); |
||
262 | |||
263 | $this->assertSame('sonata.post.admin.comment', $postAdmin->getChild('sonata.post.admin.comment')->getCode()); |
||
264 | $this->assertSame('sonata.post.admin.post|sonata.post.admin.comment', $postAdmin->getChild('sonata.post.admin.comment')->getBaseCodeRoute()); |
||
265 | $this->assertSame($postAdmin, $postAdmin->getChild('sonata.post.admin.comment')->getParent()); |
||
266 | |||
267 | $this->assertFalse($postAdmin->isChild()); |
||
268 | $this->assertTrue($commentAdmin->isChild()); |
||
269 | |||
270 | $this->assertSame(['sonata.post.admin.comment' => $commentAdmin], $postAdmin->getChildren()); |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configure |
||
275 | */ |
||
276 | public function testConfigure() |
||
277 | { |
||
278 | $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
279 | $this->assertNotNull($admin->getUniqid()); |
||
280 | |||
281 | $admin->initialize(); |
||
282 | $this->assertNotNull($admin->getUniqid()); |
||
283 | $this->assertSame('Post', $admin->getClassnameLabel()); |
||
284 | |||
285 | $admin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin'); |
||
286 | $admin->setClassnameLabel('postcomment'); |
||
287 | |||
288 | $admin->initialize(); |
||
289 | $this->assertSame('postcomment', $admin->getClassnameLabel()); |
||
290 | } |
||
291 | |||
292 | public function testConfigureWithValidParentAssociationMapping() |
||
293 | { |
||
294 | $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
295 | $admin->setParentAssociationMapping('Category'); |
||
296 | |||
297 | $admin->initialize(); |
||
298 | $this->assertSame('Category', $admin->getParentAssociationMapping()); |
||
299 | } |
||
300 | |||
301 | public function provideGetBaseRoutePattern() |
||
302 | { |
||
303 | return [ |
||
304 | [ |
||
305 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
306 | '/sonata/news/post', |
||
307 | ], |
||
308 | [ |
||
309 | 'Application\Sonata\NewsBundle\Document\Post', |
||
310 | '/sonata/news/post', |
||
311 | ], |
||
312 | [ |
||
313 | 'MyApplication\MyBundle\Entity\Post', |
||
314 | '/myapplication/my/post', |
||
315 | ], |
||
316 | [ |
||
317 | 'MyApplication\MyBundle\Entity\Post\Category', |
||
318 | '/myapplication/my/post-category', |
||
319 | ], |
||
320 | [ |
||
321 | 'MyApplication\MyBundle\Entity\Product\Category', |
||
322 | '/myapplication/my/product-category', |
||
323 | ], |
||
324 | [ |
||
325 | 'MyApplication\MyBundle\Entity\Other\Product\Category', |
||
326 | '/myapplication/my/other-product-category', |
||
327 | ], |
||
328 | [ |
||
329 | 'Symfony\Cmf\Bundle\FooBundle\Document\Menu', |
||
330 | '/cmf/foo/menu', |
||
331 | ], |
||
332 | [ |
||
333 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Phpcr\Menu', |
||
334 | '/cmf/foo/menu', |
||
335 | ], |
||
336 | [ |
||
337 | 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu', |
||
338 | '/symfony/barbar/menu', |
||
339 | ], |
||
340 | [ |
||
341 | 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu\Item', |
||
342 | '/symfony/barbar/menu-item', |
||
343 | ], |
||
344 | [ |
||
345 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Orm\Menu', |
||
346 | '/cmf/foo/menu', |
||
347 | ], |
||
348 | [ |
||
349 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\MongoDB\Menu', |
||
350 | '/cmf/foo/menu', |
||
351 | ], |
||
352 | [ |
||
353 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\CouchDB\Menu', |
||
354 | '/cmf/foo/menu', |
||
355 | ], |
||
356 | [ |
||
357 | 'AppBundle\Entity\User', |
||
358 | '/app/user', |
||
359 | ], |
||
360 | [ |
||
361 | 'App\Entity\User', |
||
362 | '/app/user', |
||
363 | ], |
||
364 | ]; |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * @dataProvider provideGetBaseRoutePattern |
||
369 | */ |
||
370 | public function testGetBaseRoutePattern($objFqn, $expected) |
||
371 | { |
||
372 | $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin'); |
||
373 | $this->assertSame($expected, $admin->getBaseRoutePattern()); |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * @dataProvider provideGetBaseRoutePattern |
||
378 | */ |
||
379 | public function testGetBaseRoutePatternWithChildAdmin($objFqn, $expected) |
||
380 | { |
||
381 | $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin'); |
||
382 | $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin'); |
||
383 | $commentAdmin->setParent($postAdmin); |
||
384 | |||
385 | $this->assertSame($expected.'/{id}/comment', $commentAdmin->getBaseRoutePattern()); |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * @dataProvider provideGetBaseRoutePattern |
||
390 | */ |
||
391 | public function testGetBaseRoutePatternWithTwoNestedChildAdmin($objFqn, $expected) |
||
392 | { |
||
393 | $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin'); |
||
394 | $commentAdmin = new CommentAdmin( |
||
395 | 'sonata.post.admin.comment', |
||
396 | 'Application\Sonata\NewsBundle\Entity\Comment', |
||
397 | 'SonataNewsBundle:CommentAdmin' |
||
398 | ); |
||
399 | $commentVoteAdmin = new CommentVoteAdmin( |
||
400 | 'sonata.post.admin.comment_vote', |
||
401 | 'Application\Sonata\NewsBundle\Entity\CommentVote', |
||
402 | 'SonataNewsBundle:CommentVoteAdmin' |
||
403 | ); |
||
404 | $commentAdmin->setParent($postAdmin); |
||
405 | $commentVoteAdmin->setParent($commentAdmin); |
||
406 | |||
407 | $this->assertSame($expected.'/{id}/comment/{childId}/commentvote', $commentVoteAdmin->getBaseRoutePattern()); |
||
408 | } |
||
409 | |||
410 | public function testGetBaseRoutePatternWithSpecifedPattern() |
||
411 | { |
||
412 | $postAdmin = new PostWithCustomRouteAdmin('sonata.post.admin.post_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostWithCustomRouteAdmin'); |
||
413 | |||
414 | $this->assertSame('/post-custom', $postAdmin->getBaseRoutePattern()); |
||
415 | } |
||
416 | |||
417 | public function testGetBaseRoutePatternWithChildAdminAndWithSpecifedPattern() |
||
418 | { |
||
419 | $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
420 | $commentAdmin = new CommentWithCustomRouteAdmin('sonata.post.admin.comment_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentWithCustomRouteAdmin'); |
||
421 | $commentAdmin->setParent($postAdmin); |
||
422 | |||
423 | $this->assertSame('/sonata/news/post/{id}/comment-custom', $commentAdmin->getBaseRoutePattern()); |
||
424 | } |
||
425 | |||
426 | public function testGetBaseRoutePatternWithUnreconizedClassname() |
||
427 | { |
||
428 | $this->expectException(\RuntimeException::class); |
||
429 | |||
430 | $admin = new PostAdmin('sonata.post.admin.post', 'News\Thing\Post', 'SonataNewsBundle:PostAdmin'); |
||
431 | $admin->getBaseRoutePattern(); |
||
432 | } |
||
433 | |||
434 | public function provideGetBaseRouteName() |
||
435 | { |
||
436 | return [ |
||
437 | [ |
||
438 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
439 | 'admin_sonata_news_post', |
||
440 | ], |
||
441 | [ |
||
442 | 'Application\Sonata\NewsBundle\Document\Post', |
||
443 | 'admin_sonata_news_post', |
||
444 | ], |
||
445 | [ |
||
446 | 'MyApplication\MyBundle\Entity\Post', |
||
447 | 'admin_myapplication_my_post', |
||
448 | ], |
||
449 | [ |
||
450 | 'MyApplication\MyBundle\Entity\Post\Category', |
||
451 | 'admin_myapplication_my_post_category', |
||
452 | ], |
||
453 | [ |
||
454 | 'MyApplication\MyBundle\Entity\Product\Category', |
||
455 | 'admin_myapplication_my_product_category', |
||
456 | ], |
||
457 | [ |
||
458 | 'MyApplication\MyBundle\Entity\Other\Product\Category', |
||
459 | 'admin_myapplication_my_other_product_category', |
||
460 | ], |
||
461 | [ |
||
462 | 'Symfony\Cmf\Bundle\FooBundle\Document\Menu', |
||
463 | 'admin_cmf_foo_menu', |
||
464 | ], |
||
465 | [ |
||
466 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Phpcr\Menu', |
||
467 | 'admin_cmf_foo_menu', |
||
468 | ], |
||
469 | [ |
||
470 | 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu', |
||
471 | 'admin_symfony_barbar_menu', |
||
472 | ], |
||
473 | [ |
||
474 | 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu\Item', |
||
475 | 'admin_symfony_barbar_menu_item', |
||
476 | ], |
||
477 | [ |
||
478 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Orm\Menu', |
||
479 | 'admin_cmf_foo_menu', |
||
480 | ], |
||
481 | [ |
||
482 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\MongoDB\Menu', |
||
483 | 'admin_cmf_foo_menu', |
||
484 | ], |
||
485 | [ |
||
486 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\CouchDB\Menu', |
||
487 | 'admin_cmf_foo_menu', |
||
488 | ], |
||
489 | [ |
||
490 | 'AppBundle\Entity\User', |
||
491 | 'admin_app_user', |
||
492 | ], |
||
493 | [ |
||
494 | 'App\Entity\User', |
||
495 | 'admin_app_user', |
||
496 | ], |
||
497 | ]; |
||
498 | } |
||
499 | |||
500 | /** |
||
501 | * @dataProvider provideGetBaseRouteName |
||
502 | */ |
||
503 | public function testGetBaseRouteName($objFqn, $expected) |
||
504 | { |
||
505 | $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin'); |
||
506 | |||
507 | $this->assertSame($expected, $admin->getBaseRouteName()); |
||
508 | } |
||
509 | |||
510 | /** |
||
511 | * @dataProvider provideGetBaseRouteName |
||
512 | */ |
||
513 | public function testGetBaseRouteNameWithChildAdmin($objFqn, $expected) |
||
514 | { |
||
515 | $routeGenerator = new DefaultRouteGenerator( |
||
516 | $this->createMock(RouterInterface::class), |
||
517 | new RoutesCache($this->cacheTempFolder, true) |
||
518 | ); |
||
519 | |||
520 | $container = new Container(); |
||
521 | $pool = new Pool($container, 'Sonata Admin', '/path/to/pic.png'); |
||
522 | |||
523 | $pathInfo = new PathInfoBuilder($this->createMock(AuditManagerInterface::class)); |
||
524 | $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin'); |
||
525 | $container->set('sonata.post.admin.post', $postAdmin); |
||
526 | $postAdmin->setConfigurationPool($pool); |
||
527 | $postAdmin->setRouteBuilder($pathInfo); |
||
528 | $postAdmin->setRouteGenerator($routeGenerator); |
||
529 | $postAdmin->initialize(); |
||
530 | |||
531 | $commentAdmin = new CommentAdmin( |
||
532 | 'sonata.post.admin.comment', |
||
533 | 'Application\Sonata\NewsBundle\Entity\Comment', |
||
534 | 'SonataNewsBundle:CommentAdmin' |
||
535 | ); |
||
536 | $container->set('sonata.post.admin.comment', $commentAdmin); |
||
537 | $commentAdmin->setConfigurationPool($pool); |
||
538 | $commentAdmin->setRouteBuilder($pathInfo); |
||
539 | $commentAdmin->setRouteGenerator($routeGenerator); |
||
540 | $commentAdmin->initialize(); |
||
541 | |||
542 | $postAdmin->addChild($commentAdmin); |
||
543 | |||
544 | $commentVoteAdmin = new CommentVoteAdmin( |
||
545 | 'sonata.post.admin.comment_vote', |
||
546 | 'Application\Sonata\NewsBundle\Entity\CommentVote', |
||
547 | 'SonataNewsBundle:CommentVoteAdmin' |
||
548 | ); |
||
549 | $container->set('sonata.post.admin.comment_vote', $commentVoteAdmin); |
||
550 | $commentVoteAdmin->setConfigurationPool($pool); |
||
551 | $commentVoteAdmin->setRouteBuilder($pathInfo); |
||
552 | $commentVoteAdmin->setRouteGenerator($routeGenerator); |
||
553 | $commentVoteAdmin->initialize(); |
||
554 | |||
555 | $commentAdmin->addChild($commentVoteAdmin); |
||
556 | $pool->setAdminServiceIds([ |
||
557 | 'sonata.post.admin.post', |
||
558 | 'sonata.post.admin.comment', |
||
559 | 'sonata.post.admin.comment_vote', |
||
560 | ]); |
||
561 | |||
562 | $this->assertSame($expected.'_comment', $commentAdmin->getBaseRouteName()); |
||
563 | |||
564 | $this->assertTrue($postAdmin->hasRoute('show')); |
||
565 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post.show')); |
||
566 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment.show')); |
||
567 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment|sonata.post.admin.comment_vote.show')); |
||
568 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.comment.list')); |
||
569 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.comment|sonata.post.admin.comment_vote.list')); |
||
570 | $this->assertFalse($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment.edit')); |
||
571 | $this->assertFalse($commentAdmin->hasRoute('edit')); |
||
572 | |||
573 | /* |
||
574 | * Test the route name from request |
||
575 | */ |
||
576 | $postListRequest = new Request( |
||
577 | [], |
||
578 | [], |
||
579 | [ |
||
580 | '_route' => $postAdmin->getBaseRouteName().'_list', |
||
581 | ] |
||
582 | ); |
||
583 | |||
584 | $postAdmin->setRequest($postListRequest); |
||
585 | $commentAdmin->setRequest($postListRequest); |
||
586 | |||
587 | $this->assertTrue($postAdmin->isCurrentRoute('list')); |
||
588 | $this->assertFalse($postAdmin->isCurrentRoute('create')); |
||
589 | $this->assertFalse($commentAdmin->isCurrentRoute('list')); |
||
590 | $this->assertFalse($commentVoteAdmin->isCurrentRoute('list')); |
||
591 | $this->assertTrue($commentAdmin->isCurrentRoute('list', 'sonata.post.admin.post')); |
||
592 | $this->assertFalse($commentAdmin->isCurrentRoute('edit', 'sonata.post.admin.post')); |
||
593 | $this->assertTrue($commentVoteAdmin->isCurrentRoute('list', 'sonata.post.admin.post')); |
||
594 | $this->assertFalse($commentVoteAdmin->isCurrentRoute('edit', 'sonata.post.admin.post')); |
||
595 | } |
||
596 | |||
597 | public function testGetBaseRouteNameWithUnreconizedClassname() |
||
598 | { |
||
599 | $this->expectException(\RuntimeException::class); |
||
600 | |||
601 | $admin = new PostAdmin('sonata.post.admin.post', 'News\Thing\Post', 'SonataNewsBundle:PostAdmin'); |
||
602 | $admin->getBaseRouteName(); |
||
603 | } |
||
604 | |||
605 | public function testGetBaseRouteNameWithSpecifiedName() |
||
606 | { |
||
607 | $postAdmin = new PostWithCustomRouteAdmin('sonata.post.admin.post_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
608 | |||
609 | $this->assertSame('post_custom', $postAdmin->getBaseRouteName()); |
||
610 | } |
||
611 | |||
612 | public function testGetBaseRouteNameWithChildAdminAndWithSpecifiedName() |
||
613 | { |
||
614 | $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
615 | $commentAdmin = new CommentWithCustomRouteAdmin('sonata.post.admin.comment_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentWithCustomRouteAdmin'); |
||
616 | $commentAdmin->setParent($postAdmin); |
||
617 | |||
618 | $this->assertSame('admin_sonata_news_post_comment_custom', $commentAdmin->getBaseRouteName()); |
||
619 | } |
||
620 | |||
621 | public function testGetBaseRouteNameWithTwoNestedChildAdminAndWithSpecifiedName() |
||
622 | { |
||
623 | $postAdmin = new PostAdmin( |
||
624 | 'sonata.post.admin.post', |
||
625 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
626 | 'SonataNewsBundle:PostAdmin' |
||
627 | ); |
||
628 | $commentAdmin = new CommentWithCustomRouteAdmin( |
||
629 | 'sonata.post.admin.comment_with_custom_route', |
||
630 | 'Application\Sonata\NewsBundle\Entity\Comment', |
||
631 | 'SonataNewsBundle:CommentWithCustomRouteAdmin' |
||
632 | ); |
||
633 | $commentVoteAdmin = new CommentVoteAdmin( |
||
634 | 'sonata.post.admin.comment_vote', |
||
635 | 'Application\Sonata\NewsBundle\Entity\CommentVote', |
||
636 | 'SonataNewsBundle:CommentVoteAdmin' |
||
637 | ); |
||
638 | $commentAdmin->setParent($postAdmin); |
||
639 | $commentVoteAdmin->setParent($commentAdmin); |
||
640 | |||
641 | $this->assertSame('admin_sonata_news_post_comment_custom_commentvote', $commentVoteAdmin->getBaseRouteName()); |
||
642 | } |
||
643 | |||
644 | /** |
||
645 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::setUniqid |
||
646 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getUniqid |
||
647 | */ |
||
648 | public function testUniqid() |
||
649 | { |
||
650 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
651 | |||
652 | $uniqid = uniqid(); |
||
653 | $admin->setUniqid($uniqid); |
||
654 | |||
655 | $this->assertSame($uniqid, $admin->getUniqid()); |
||
656 | } |
||
657 | |||
658 | public function testToString() |
||
659 | { |
||
660 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
661 | |||
662 | $s = new \stdClass(); |
||
663 | |||
664 | $this->assertNotEmpty($admin->toString($s)); |
||
665 | |||
666 | $s = new FooToString(); |
||
667 | $this->assertSame('salut', $admin->toString($s)); |
||
668 | |||
669 | // To string method is implemented, but returns null |
||
670 | $s = new FooToStringNull(); |
||
671 | $this->assertNotEmpty($admin->toString($s)); |
||
672 | |||
673 | $this->assertSame('', $admin->toString(false)); |
||
674 | } |
||
675 | |||
676 | public function testIsAclEnabled() |
||
677 | { |
||
678 | $postAdmin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
679 | |||
680 | $this->assertFalse($postAdmin->isAclEnabled()); |
||
681 | |||
682 | $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin'); |
||
683 | $commentAdmin->setSecurityHandler($this->createMock(AclSecurityHandlerInterface::class)); |
||
684 | $this->assertTrue($commentAdmin->isAclEnabled()); |
||
685 | } |
||
686 | |||
687 | /** |
||
688 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getSubClasses |
||
689 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getSubClass |
||
690 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::setSubClasses |
||
691 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasSubClass |
||
692 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass |
||
693 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubClass |
||
694 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubclassCode |
||
695 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getClass |
||
696 | */ |
||
697 | public function testSubClass() |
||
698 | { |
||
699 | $admin = new PostAdmin( |
||
700 | 'sonata.post.admin.post', |
||
701 | Post::class, |
||
702 | 'SonataNewsBundle:PostAdmin' |
||
703 | ); |
||
704 | $this->assertFalse($admin->hasSubClass('test')); |
||
705 | $this->assertFalse($admin->hasActiveSubClass()); |
||
706 | $this->assertCount(0, $admin->getSubClasses()); |
||
707 | $this->assertNull($admin->getActiveSubClass()); |
||
708 | $this->assertNull($admin->getActiveSubclassCode()); |
||
709 | $this->assertSame(Post::class, $admin->getClass()); |
||
710 | |||
711 | // Just for the record, if there is no inheritance set, the getSubject is not used |
||
712 | // the getSubject can also lead to some issue |
||
713 | $admin->setSubject(new BlogPost()); |
||
714 | $this->assertSame(BlogPost::class, $admin->getClass()); |
||
715 | |||
716 | $admin->setSubClasses(['extended1' => 'NewsBundle\Entity\PostExtended1', 'extended2' => 'NewsBundle\Entity\PostExtended2']); |
||
717 | $this->assertFalse($admin->hasSubClass('test')); |
||
718 | $this->assertTrue($admin->hasSubClass('extended1')); |
||
719 | $this->assertFalse($admin->hasActiveSubClass()); |
||
720 | $this->assertCount(2, $admin->getSubClasses()); |
||
721 | $this->assertNull($admin->getActiveSubClass()); |
||
722 | $this->assertNull($admin->getActiveSubclassCode()); |
||
723 | $this->assertSame(BlogPost::class, $admin->getClass()); |
||
724 | |||
725 | $request = new Request(['subclass' => 'extended1']); |
||
726 | $admin->setRequest($request); |
||
727 | $this->assertFalse($admin->hasSubClass('test')); |
||
728 | $this->assertTrue($admin->hasSubClass('extended1')); |
||
729 | $this->assertTrue($admin->hasActiveSubClass()); |
||
730 | $this->assertCount(2, $admin->getSubClasses()); |
||
731 | $this->assertSame(BlogPost::class, $admin->getActiveSubClass()); |
||
732 | $this->assertSame('extended1', $admin->getActiveSubclassCode()); |
||
733 | $this->assertSame(BlogPost::class, $admin->getClass()); |
||
734 | |||
735 | $request->query->set('subclass', 'inject'); |
||
736 | $this->assertNull($admin->getActiveSubclassCode()); |
||
737 | } |
||
738 | |||
739 | public function testNonExistantSubclass() |
||
740 | { |
||
741 | $this->expectException(\RuntimeException::class); |
||
742 | |||
743 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
744 | $admin->setModelManager($this->getMockForAbstractClass(ModelManagerInterface::class)); |
||
745 | |||
746 | $admin->setRequest(new Request(['subclass' => 'inject'])); |
||
747 | |||
748 | $admin->setSubClasses(['extended1' => 'NewsBundle\Entity\PostExtended1', 'extended2' => 'NewsBundle\Entity\PostExtended2']); |
||
749 | |||
750 | $this->assertTrue($admin->hasActiveSubClass()); |
||
751 | |||
752 | $admin->getActiveSubClass(); |
||
753 | } |
||
754 | |||
755 | /** |
||
756 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass |
||
757 | */ |
||
758 | public function testOnlyOneSubclassNeededToBeActive() |
||
766 | |||
767 | public function testGetPerPageOptions() |
||
775 | |||
776 | public function testGetLabelTranslatorStrategy() |
||
786 | |||
787 | public function testGetRouteBuilder() |
||
797 | |||
798 | public function testGetMenuFactory() |
||
808 | |||
809 | public function testGetExtensions() |
||
822 | |||
823 | public function testGetFilterTheme() |
||
832 | |||
833 | public function testGetFormTheme() |
||
843 | |||
844 | public function testGetValidator() |
||
855 | |||
856 | public function testGetSecurityHandler() |
||
866 | |||
867 | public function testGetSecurityInformation() |
||
881 | |||
882 | public function testGetManagerType() |
||
891 | |||
892 | public function testGetModelManager() |
||
903 | |||
904 | /** |
||
905 | * NEXT_MAJOR: remove this method. |
||
906 | * |
||
907 | * @group legacy |
||
908 | */ |
||
909 | public function testGetBaseCodeRoute() |
||
918 | |||
919 | // NEXT_MAJOR: uncomment this method. |
||
920 | // public function testGetBaseCodeRoute() |
||
921 | // { |
||
922 | // $postAdmin = new PostAdmin( |
||
923 | // 'sonata.post.admin.post', |
||
924 | // 'NewsBundle\Entity\Post', |
||
925 | // 'SonataNewsBundle:PostAdmin' |
||
926 | // ); |
||
927 | // $commentAdmin = new CommentAdmin( |
||
928 | // 'sonata.post.admin.comment', |
||
929 | // 'Application\Sonata\NewsBundle\Entity\Comment', |
||
930 | // 'SonataNewsBundle:CommentAdmin' |
||
931 | // ); |
||
932 | // |
||
933 | // $this->assertSame($postAdmin->getCode(), $postAdmin->getBaseCodeRoute()); |
||
934 | // |
||
935 | // $postAdmin->addChild($commentAdmin); |
||
936 | // |
||
937 | // $this->assertSame( |
||
938 | // 'sonata.post.admin.post|sonata.post.admin.comment', |
||
939 | // $commentAdmin->getBaseCodeRoute() |
||
940 | // ); |
||
941 | // } |
||
942 | |||
943 | public function testGetRouteGenerator() |
||
954 | |||
955 | public function testGetConfigurationPool() |
||
968 | |||
969 | public function testGetShowBuilder() |
||
980 | |||
981 | public function testGetListBuilder() |
||
992 | |||
993 | public function testGetDatagridBuilder() |
||
1004 | |||
1005 | public function testGetFormContractor() |
||
1016 | |||
1017 | public function testGetRequest() |
||
1029 | |||
1030 | public function testGetRequestWithException() |
||
1031 | { |
||
1032 | $this->expectException(\RuntimeException::class); |
||
1033 | $this->expectExceptionMessage('The Request object has not been set'); |
||
1034 | |||
1035 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1036 | $admin->getRequest(); |
||
1037 | } |
||
1038 | |||
1039 | public function testGetTranslationDomain() |
||
1040 | { |
||
1041 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1042 | |||
1043 | $this->assertSame('messages', $admin->getTranslationDomain()); |
||
1044 | |||
1045 | $admin->setTranslationDomain('foo'); |
||
1046 | $this->assertSame('foo', $admin->getTranslationDomain()); |
||
1047 | } |
||
1048 | |||
1049 | /** |
||
1050 | * @group legacy |
||
1051 | */ |
||
1052 | public function testGetTranslator() |
||
1053 | { |
||
1054 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1055 | |||
1056 | $this->assertNull($admin->getTranslator()); |
||
1057 | |||
1058 | $translator = $this->createMock(TranslatorInterface::class); |
||
1059 | |||
1060 | $admin->setTranslator($translator); |
||
1061 | $this->assertSame($translator, $admin->getTranslator()); |
||
1062 | } |
||
1063 | |||
1064 | public function testGetShowGroups() |
||
1065 | { |
||
1066 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1067 | |||
1068 | $this->assertFalse($admin->getShowGroups()); |
||
1069 | |||
1070 | $groups = ['foo', 'bar', 'baz']; |
||
1071 | |||
1072 | $admin->setShowGroups($groups); |
||
1073 | $this->assertSame($groups, $admin->getShowGroups()); |
||
1074 | } |
||
1075 | |||
1076 | public function testGetFormGroups() |
||
1077 | { |
||
1078 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1079 | |||
1080 | $this->assertFalse($admin->getFormGroups()); |
||
1081 | |||
1082 | $groups = ['foo', 'bar', 'baz']; |
||
1083 | |||
1084 | $admin->setFormGroups($groups); |
||
1085 | $this->assertSame($groups, $admin->getFormGroups()); |
||
1086 | } |
||
1087 | |||
1088 | public function testGetMaxPageLinks() |
||
1089 | { |
||
1090 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1091 | |||
1092 | $this->assertSame(25, $admin->getMaxPageLinks()); |
||
1093 | |||
1094 | $admin->setMaxPageLinks(14); |
||
1095 | $this->assertSame(14, $admin->getMaxPageLinks()); |
||
1096 | } |
||
1097 | |||
1098 | public function testGetMaxPerPage() |
||
1099 | { |
||
1100 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1101 | |||
1102 | $this->assertSame(32, $admin->getMaxPerPage()); |
||
1103 | |||
1104 | $admin->setMaxPerPage(94); |
||
1105 | $this->assertSame(94, $admin->getMaxPerPage()); |
||
1106 | } |
||
1107 | |||
1108 | public function testGetLabel() |
||
1109 | { |
||
1110 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1111 | |||
1112 | $this->assertNull($admin->getLabel()); |
||
1113 | |||
1114 | $admin->setLabel('FooLabel'); |
||
1115 | $this->assertSame('FooLabel', $admin->getLabel()); |
||
1116 | } |
||
1117 | |||
1118 | public function testGetBaseController() |
||
1119 | { |
||
1120 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1121 | |||
1122 | $this->assertSame('SonataNewsBundle:PostAdmin', $admin->getBaseControllerName()); |
||
1123 | |||
1124 | $admin->setBaseControllerName('SonataNewsBundle:FooAdmin'); |
||
1125 | $this->assertSame('SonataNewsBundle:FooAdmin', $admin->getBaseControllerName()); |
||
1126 | } |
||
1127 | |||
1128 | public function testGetTemplates() |
||
1129 | { |
||
1130 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1131 | |||
1132 | $this->assertSame([], $admin->getTemplates()); |
||
1133 | |||
1134 | $templates = [ |
||
1135 | 'list' => 'FooAdminBundle:CRUD:list.html.twig', |
||
1136 | 'show' => 'FooAdminBundle:CRUD:show.html.twig', |
||
1137 | 'edit' => 'FooAdminBundle:CRUD:edit.html.twig', |
||
1138 | ]; |
||
1139 | |||
1140 | $admin->setTemplates($templates); |
||
1141 | $this->assertSame($templates, $admin->getTemplates()); |
||
1142 | } |
||
1143 | |||
1144 | public function testGetTemplate1() |
||
1145 | { |
||
1146 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1147 | |||
1148 | $this->assertNull($admin->getTemplate('edit')); |
||
1149 | |||
1150 | $admin->setTemplate('edit', 'FooAdminBundle:CRUD:edit.html.twig'); |
||
1151 | $admin->setTemplate('show', 'FooAdminBundle:CRUD:show.html.twig'); |
||
1152 | |||
1153 | $this->assertSame('FooAdminBundle:CRUD:edit.html.twig', $admin->getTemplate('edit')); |
||
1154 | $this->assertSame('FooAdminBundle:CRUD:show.html.twig', $admin->getTemplate('show')); |
||
1155 | } |
||
1156 | |||
1157 | public function testGetTemplate2() |
||
1158 | { |
||
1159 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1160 | |||
1161 | $this->assertNull($admin->getTemplate('edit')); |
||
1162 | |||
1163 | $templates = [ |
||
1164 | 'list' => 'FooAdminBundle:CRUD:list.html.twig', |
||
1165 | 'show' => 'FooAdminBundle:CRUD:show.html.twig', |
||
1166 | 'edit' => 'FooAdminBundle:CRUD:edit.html.twig', |
||
1167 | ]; |
||
1168 | |||
1169 | $admin->setTemplates($templates); |
||
1170 | |||
1171 | $this->assertSame('FooAdminBundle:CRUD:edit.html.twig', $admin->getTemplate('edit')); |
||
1172 | $this->assertSame('FooAdminBundle:CRUD:show.html.twig', $admin->getTemplate('show')); |
||
1173 | } |
||
1174 | |||
1175 | public function testGetIdParameter() |
||
1176 | { |
||
1177 | $postAdmin = new PostAdmin( |
||
1178 | 'sonata.post.admin.post', |
||
1179 | 'NewsBundle\Entity\Post', |
||
1180 | 'SonataNewsBundle:PostAdmin' |
||
1181 | ); |
||
1182 | |||
1183 | $this->assertSame('id', $postAdmin->getIdParameter()); |
||
1184 | $this->assertFalse($postAdmin->isChild()); |
||
1185 | |||
1186 | $commentAdmin = new CommentAdmin( |
||
1187 | 'sonata.post.admin.comment', |
||
1188 | 'Application\Sonata\NewsBundle\Entity\Comment', |
||
1189 | 'SonataNewsBundle:CommentAdmin' |
||
1190 | ); |
||
1191 | $commentAdmin->setParent($postAdmin); |
||
1192 | |||
1193 | $this->assertTrue($commentAdmin->isChild()); |
||
1194 | $this->assertSame('childId', $commentAdmin->getIdParameter()); |
||
1195 | |||
1196 | $commentVoteAdmin = new CommentVoteAdmin( |
||
1197 | 'sonata.post.admin.comment_vote', |
||
1198 | 'Application\Sonata\NewsBundle\Entity\CommentVote', |
||
1199 | 'SonataNewsBundle:CommentVoteAdmin' |
||
1200 | ); |
||
1201 | $commentVoteAdmin->setParent($commentAdmin); |
||
1202 | |||
1203 | $this->assertTrue($commentVoteAdmin->isChild()); |
||
1204 | $this->assertSame('childChildId', $commentVoteAdmin->getIdParameter()); |
||
1205 | } |
||
1206 | |||
1207 | public function testGetExportFormats() |
||
1208 | { |
||
1209 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1210 | |||
1211 | $this->assertSame(['json', 'xml', 'csv', 'xls'], $admin->getExportFormats()); |
||
1212 | } |
||
1213 | |||
1214 | public function testGetUrlsafeIdentifier() |
||
1215 | { |
||
1216 | $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1217 | |||
1218 | $entity = new \stdClass(); |
||
1219 | |||
1220 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
1221 | $modelManager->expects($this->once()) |
||
1222 | ->method('getUrlsafeIdentifier') |
||
1223 | ->with($this->equalTo($entity)) |
||
1224 | ->will($this->returnValue('foo')); |
||
1225 | $admin->setModelManager($modelManager); |
||
1226 | |||
1227 | $this->assertSame('foo', $admin->getUrlsafeIdentifier($entity)); |
||
1228 | } |
||
1229 | |||
1230 | public function testDeterminedPerPageValue() |
||
1231 | { |
||
1232 | $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1233 | |||
1234 | $this->assertFalse($admin->determinedPerPageValue('foo')); |
||
1235 | $this->assertFalse($admin->determinedPerPageValue(123)); |
||
1236 | $this->assertTrue($admin->determinedPerPageValue(16)); |
||
1237 | $this->assertTrue($admin->determinedPerPageValue(32)); |
||
1238 | $this->assertTrue($admin->determinedPerPageValue(64)); |
||
1239 | $this->assertTrue($admin->determinedPerPageValue(128)); |
||
1240 | $this->assertTrue($admin->determinedPerPageValue(192)); |
||
1241 | |||
1242 | $admin->setPerPageOptions([101, 102, 103]); |
||
1243 | $this->assertFalse($admin->determinedPerPageValue(15)); |
||
1244 | $this->assertFalse($admin->determinedPerPageValue(25)); |
||
1245 | $this->assertFalse($admin->determinedPerPageValue(200)); |
||
1246 | $this->assertTrue($admin->determinedPerPageValue(101)); |
||
1247 | $this->assertTrue($admin->determinedPerPageValue(102)); |
||
1248 | $this->assertTrue($admin->determinedPerPageValue(103)); |
||
1249 | } |
||
1250 | |||
1251 | public function testIsGranted() |
||
1252 | { |
||
1253 | $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1254 | |||
1255 | $entity = new \stdClass(); |
||
1256 | |||
1257 | $securityHandler = $this->createMock(AclSecurityHandlerInterface::class); |
||
1258 | $securityHandler->expects($this->any()) |
||
1259 | ->method('isGranted') |
||
1260 | ->will($this->returnCallback(function (AdminInterface $adminIn, $attributes, $object = null) use ($admin, $entity) { |
||
1261 | if ($admin == $adminIn && 'FOO' == $attributes) { |
||
1262 | if (($object == $admin) || ($object == $entity)) { |
||
1263 | return true; |
||
1264 | } |
||
1265 | } |
||
1266 | |||
1267 | return false; |
||
1268 | })); |
||
1269 | |||
1270 | $admin->setSecurityHandler($securityHandler); |
||
1271 | |||
1272 | $this->assertTrue($admin->isGranted('FOO')); |
||
1273 | $this->assertTrue($admin->isGranted('FOO', $entity)); |
||
1274 | $this->assertFalse($admin->isGranted('BAR')); |
||
1275 | $this->assertFalse($admin->isGranted('BAR', $entity)); |
||
1276 | } |
||
1277 | |||
1278 | public function testSupportsPreviewMode() |
||
1279 | { |
||
1280 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1281 | |||
1282 | $this->assertFalse($admin->supportsPreviewMode()); |
||
1283 | } |
||
1284 | |||
1285 | public function testGetPermissionsShow() |
||
1286 | { |
||
1287 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1288 | |||
1289 | $this->assertSame(['LIST'], $admin->getPermissionsShow(AbstractAdmin::CONTEXT_DASHBOARD)); |
||
1290 | $this->assertSame(['LIST'], $admin->getPermissionsShow(AbstractAdmin::CONTEXT_MENU)); |
||
1291 | $this->assertSame(['LIST'], $admin->getPermissionsShow('foo')); |
||
1292 | } |
||
1293 | |||
1294 | public function testShowIn() |
||
1295 | { |
||
1296 | $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1297 | |||
1298 | $securityHandler = $this->createMock(AclSecurityHandlerInterface::class); |
||
1299 | $securityHandler->expects($this->any()) |
||
1300 | ->method('isGranted') |
||
1301 | ->will($this->returnCallback(function (AdminInterface $adminIn, $attributes, $object = null) use ($admin) { |
||
1302 | if ($admin == $adminIn && $attributes == ['LIST']) { |
||
1303 | return true; |
||
1304 | } |
||
1305 | |||
1306 | return false; |
||
1307 | })); |
||
1308 | |||
1309 | $admin->setSecurityHandler($securityHandler); |
||
1310 | |||
1311 | $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_DASHBOARD)); |
||
1312 | $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_MENU)); |
||
1313 | $this->assertTrue($admin->showIn('foo')); |
||
1314 | } |
||
1315 | |||
1316 | public function testGetObjectIdentifier() |
||
1317 | { |
||
1318 | $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1319 | |||
1320 | $this->assertSame('sonata.post.admin.post', $admin->getObjectIdentifier()); |
||
1321 | } |
||
1322 | |||
1323 | /** |
||
1324 | * @group legacy |
||
1325 | */ |
||
1326 | public function testTrans() |
||
1327 | { |
||
1328 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1329 | $admin->setTranslationDomain('fooMessageDomain'); |
||
1330 | |||
1331 | $translator = $this->createMock(TranslatorInterface::class); |
||
1332 | $admin->setTranslator($translator); |
||
1333 | |||
1334 | $translator->expects($this->once()) |
||
1335 | ->method('trans') |
||
1336 | ->with($this->equalTo('foo'), $this->equalTo([]), $this->equalTo('fooMessageDomain')) |
||
1337 | ->will($this->returnValue('fooTranslated')); |
||
1338 | |||
1339 | $this->assertSame('fooTranslated', $admin->trans('foo')); |
||
1340 | } |
||
1341 | |||
1342 | /** |
||
1343 | * @group legacy |
||
1344 | */ |
||
1345 | public function testTransWithMessageDomain() |
||
1346 | { |
||
1347 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1348 | |||
1349 | $translator = $this->createMock(TranslatorInterface::class); |
||
1350 | $admin->setTranslator($translator); |
||
1351 | |||
1352 | $translator->expects($this->once()) |
||
1353 | ->method('trans') |
||
1354 | ->with($this->equalTo('foo'), $this->equalTo(['name' => 'Andrej']), $this->equalTo('fooMessageDomain')) |
||
1355 | ->will($this->returnValue('fooTranslated')); |
||
1356 | |||
1357 | $this->assertSame('fooTranslated', $admin->trans('foo', ['name' => 'Andrej'], 'fooMessageDomain')); |
||
1358 | } |
||
1359 | |||
1360 | /** |
||
1361 | * @group legacy |
||
1362 | */ |
||
1363 | public function testTransChoice() |
||
1364 | { |
||
1365 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1366 | $admin->setTranslationDomain('fooMessageDomain'); |
||
1367 | |||
1368 | $translator = $this->createMock(TranslatorInterface::class); |
||
1369 | $admin->setTranslator($translator); |
||
1370 | |||
1371 | $translator->expects($this->once()) |
||
1372 | ->method('transChoice') |
||
1373 | ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo([]), $this->equalTo('fooMessageDomain')) |
||
1374 | ->will($this->returnValue('fooTranslated')); |
||
1375 | |||
1376 | $this->assertSame('fooTranslated', $admin->transChoice('foo', 2)); |
||
1377 | } |
||
1378 | |||
1379 | /** |
||
1380 | * @group legacy |
||
1381 | */ |
||
1382 | public function testTransChoiceWithMessageDomain() |
||
1383 | { |
||
1384 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1385 | |||
1386 | $translator = $this->createMock(TranslatorInterface::class); |
||
1387 | $admin->setTranslator($translator); |
||
1388 | |||
1389 | $translator->expects($this->once()) |
||
1390 | ->method('transChoice') |
||
1391 | ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo(['name' => 'Andrej']), $this->equalTo('fooMessageDomain')) |
||
1392 | ->will($this->returnValue('fooTranslated')); |
||
1393 | |||
1394 | $this->assertSame('fooTranslated', $admin->transChoice('foo', 2, ['name' => 'Andrej'], 'fooMessageDomain')); |
||
1395 | } |
||
1396 | |||
1397 | public function testSetPersistFilters() |
||
1398 | { |
||
1399 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1400 | |||
1401 | $this->assertAttributeSame(false, 'persistFilters', $admin); |
||
1402 | $admin->setPersistFilters(true); |
||
1403 | $this->assertAttributeSame(true, 'persistFilters', $admin); |
||
1404 | } |
||
1405 | |||
1406 | public function testGetRootCode() |
||
1407 | { |
||
1408 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1409 | |||
1410 | $this->assertSame('sonata.post.admin.post', $admin->getRootCode()); |
||
1411 | |||
1412 | $parentAdmin = new PostAdmin('sonata.post.admin.post.parent', 'NewsBundle\Entity\PostParent', 'SonataNewsBundle:PostParentAdmin'); |
||
1413 | $parentFieldDescription = $this->createMock(FieldDescriptionInterface::class); |
||
1414 | $parentFieldDescription->expects($this->once()) |
||
1415 | ->method('getAdmin') |
||
1416 | ->will($this->returnValue($parentAdmin)); |
||
1417 | |||
1418 | $this->assertNull($admin->getParentFieldDescription()); |
||
1419 | $admin->setParentFieldDescription($parentFieldDescription); |
||
1420 | $this->assertSame($parentFieldDescription, $admin->getParentFieldDescription()); |
||
1421 | $this->assertSame('sonata.post.admin.post.parent', $admin->getRootCode()); |
||
1422 | } |
||
1423 | |||
1424 | public function testGetRoot() |
||
1425 | { |
||
1426 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1427 | |||
1428 | $this->assertSame($admin, $admin->getRoot()); |
||
1429 | |||
1430 | $parentAdmin = new PostAdmin('sonata.post.admin.post.parent', 'NewsBundle\Entity\PostParent', 'SonataNewsBundle:PostParentAdmin'); |
||
1431 | $parentFieldDescription = $this->createMock(FieldDescriptionInterface::class); |
||
1432 | $parentFieldDescription->expects($this->once()) |
||
1433 | ->method('getAdmin') |
||
1434 | ->will($this->returnValue($parentAdmin)); |
||
1435 | |||
1436 | $this->assertNull($admin->getParentFieldDescription()); |
||
1437 | $admin->setParentFieldDescription($parentFieldDescription); |
||
1438 | $this->assertSame($parentFieldDescription, $admin->getParentFieldDescription()); |
||
1439 | $this->assertSame($parentAdmin, $admin->getRoot()); |
||
1440 | } |
||
1441 | |||
1442 | public function testGetExportFields() |
||
1443 | { |
||
1444 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1445 | |||
1446 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
1447 | $modelManager->expects($this->once()) |
||
1448 | ->method('getExportFields') |
||
1449 | ->with($this->equalTo('NewsBundle\Entity\Post')) |
||
1450 | ->will($this->returnValue(['foo', 'bar'])); |
||
1451 | |||
1452 | $admin->setModelManager($modelManager); |
||
1453 | $this->assertSame(['foo', 'bar'], $admin->getExportFields()); |
||
1454 | } |
||
1455 | |||
1456 | public function testGetPersistentParametersWithNoExtension() |
||
1457 | { |
||
1458 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1459 | |||
1460 | $this->assertEmpty($admin->getPersistentParameters()); |
||
1461 | } |
||
1462 | |||
1463 | public function testGetPersistentParametersWithInvalidExtension() |
||
1464 | { |
||
1465 | $this->expectException(\RuntimeException::class); |
||
1466 | |||
1467 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1468 | |||
1469 | $extension = $this->createMock(AdminExtensionInterface::class); |
||
1470 | $extension->expects($this->once())->method('getPersistentParameters')->will($this->returnValue(null)); |
||
1471 | |||
1472 | $admin->addExtension($extension); |
||
1473 | |||
1474 | $admin->getPersistentParameters(); |
||
1475 | } |
||
1476 | |||
1477 | public function testGetPersistentParametersWithValidExtension() |
||
1492 | |||
1493 | public function testGetFormWithNonCollectionParentValue() |
||
1503 | |||
1504 | public function testGetFormWithCollectionParentValue() |
||
1538 | |||
1539 | public function testRemoveFieldFromFormGroup() |
||
1565 | |||
1566 | public function testGetFilterParameters() |
||
1600 | |||
1601 | public function testGetFilterFieldDescription() |
||
1672 | |||
1673 | public function testGetSubjectNoRequest() |
||
1685 | |||
1686 | public function testGetSideMenu() |
||
1710 | |||
1711 | /** |
||
1712 | * @return array |
||
1713 | */ |
||
1714 | public function provideGetSubject() |
||
1724 | |||
1725 | /** |
||
1726 | * @dataProvider provideGetSubject |
||
1727 | */ |
||
1728 | public function testGetSubjectFailed($id) |
||
1743 | |||
1744 | /** |
||
1745 | * @dataProvider provideGetSubject |
||
1746 | */ |
||
1747 | public function testGetSubject($id) |
||
1765 | |||
1766 | public function testGetSubjectWithParentDescription() |
||
1795 | |||
1796 | /** |
||
1797 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons |
||
1798 | */ |
||
1799 | public function testGetActionButtonsList() |
||
1829 | |||
1830 | /** |
||
1831 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons |
||
1832 | */ |
||
1833 | public function testGetActionButtonsListCreateDisabled() |
||
1847 | |||
1848 | /** |
||
1849 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureBatchActions |
||
1850 | */ |
||
1851 | public function testGetBatchActions() |
||
1909 | |||
1910 | /** |
||
1911 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton |
||
1912 | */ |
||
1913 | public function testShowMosaicButton() |
||
1922 | |||
1923 | /** |
||
1924 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton |
||
1925 | */ |
||
1926 | public function testShowMosaicButtonHideMosaic() |
||
1936 | |||
1937 | /** |
||
1938 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getDashboardActions |
||
1939 | * @dataProvider provideGetBaseRouteName |
||
1940 | */ |
||
1941 | public function testDefaultDashboardActionsArePresent($objFqn, $expected) |
||
1971 | |||
1972 | public function testDefaultFilters() |
||
2031 | |||
2032 | /** |
||
2033 | * @group legacy |
||
2034 | */ |
||
2035 | public function testDefaultBreadcrumbsBuilder() |
||
2059 | |||
2060 | /** |
||
2061 | * @group legacy |
||
2062 | */ |
||
2063 | public function testBreadcrumbsBuilderSetter() |
||
2073 | |||
2074 | /** |
||
2075 | * @group legacy |
||
2076 | */ |
||
2077 | public function testGetBreadcrumbs() |
||
2087 | |||
2088 | /** |
||
2089 | * @group legacy |
||
2090 | */ |
||
2091 | public function testBuildBreadcrumbs() |
||
2108 | |||
2109 | /** |
||
2110 | * NEXT_MAJOR: remove this method. |
||
2111 | * |
||
2112 | * @group legacy |
||
2113 | */ |
||
2114 | public function testCreateQueryLegacyCallWorks() |
||
2128 | |||
2129 | public function testGetDataSourceIterator() |
||
2171 | |||
2172 | public function testCircularChildAdmin() |
||
2192 | |||
2193 | public function testCircularChildAdminTripleLevel() |
||
2219 | |||
2220 | public function testCircularChildAdminWithItself() |
||
2234 | |||
2235 | public function testGetRootAncestor() |
||
2269 | |||
2270 | public function testGetChildDepth() |
||
2304 | |||
2305 | public function testGetCurrentLeafChildAdmin() |
||
2342 | |||
2343 | private function createTagAdmin(Post $post) |
||
2382 | } |
||
2383 |
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: