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 |
||
68 | class AdminTest extends TestCase |
||
69 | { |
||
70 | protected $cacheTempFolder; |
||
71 | |||
72 | public function setUp(): void |
||
73 | { |
||
74 | $this->cacheTempFolder = sys_get_temp_dir().'/sonata_test_route'; |
||
75 | |||
76 | exec('rm -rf '.$this->cacheTempFolder); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::__construct |
||
81 | */ |
||
82 | public function testConstructor(): void |
||
83 | { |
||
84 | $class = 'Application\Sonata\NewsBundle\Entity\Post'; |
||
85 | $baseControllerName = 'SonataNewsBundle:PostAdmin'; |
||
86 | |||
87 | $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName); |
||
88 | $this->assertInstanceOf(AbstractAdmin::class, $admin); |
||
89 | $this->assertSame($class, $admin->getClass()); |
||
90 | $this->assertSame($baseControllerName, $admin->getBaseControllerName()); |
||
91 | } |
||
92 | |||
93 | public function testGetClass(): void |
||
94 | { |
||
95 | $class = Post::class; |
||
96 | $baseControllerName = 'SonataNewsBundle:PostAdmin'; |
||
97 | |||
98 | $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName); |
||
99 | |||
100 | $admin->setModelManager($this->getMockForAbstractClass(ModelManagerInterface::class)); |
||
101 | |||
102 | $admin->setSubject(new BlogPost()); |
||
103 | $this->assertSame(BlogPost::class, $admin->getClass()); |
||
104 | |||
105 | $admin->setSubClasses(['foo']); |
||
106 | $this->assertSame(BlogPost::class, $admin->getClass()); |
||
107 | |||
108 | $admin->setSubject(null); |
||
|
|||
109 | $admin->setSubClasses([]); |
||
110 | $this->assertSame($class, $admin->getClass()); |
||
111 | |||
112 | $admin->setSubClasses(['foo' => 'bar']); |
||
113 | $admin->setRequest(new Request(['subclass' => 'foo'])); |
||
114 | $this->assertSame('bar', $admin->getClass()); |
||
115 | } |
||
116 | |||
117 | public function testGetClassException(): void |
||
118 | { |
||
119 | $this->expectException(\RuntimeException::class); |
||
120 | $this->expectExceptionMessage('Feature not implemented: an embedded admin cannot have subclass'); |
||
121 | |||
122 | $class = 'Application\Sonata\NewsBundle\Entity\Post'; |
||
123 | $baseControllerName = 'SonataNewsBundle:PostAdmin'; |
||
124 | |||
125 | $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName); |
||
126 | $admin->setParentFieldDescription(new FieldDescription()); |
||
127 | $admin->setSubClasses(['foo' => 'bar']); |
||
128 | $admin->setRequest(new Request(['subclass' => 'foo'])); |
||
129 | $admin->getClass(); |
||
130 | } |
||
131 | |||
132 | public function testCheckAccessThrowsExceptionOnMadeUpAction(): void |
||
133 | { |
||
134 | $admin = new PostAdmin( |
||
135 | 'sonata.post.admin.post', |
||
136 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
137 | 'SonataNewsBundle:PostAdmin' |
||
138 | ); |
||
139 | $this->expectException( |
||
140 | \InvalidArgumentException::class |
||
141 | ); |
||
142 | $this->expectExceptionMessage( |
||
143 | 'Action "made-up" could not be found' |
||
144 | ); |
||
145 | $admin->checkAccess('made-up'); |
||
146 | } |
||
147 | |||
148 | public function testCheckAccessThrowsAccessDeniedException(): void |
||
149 | { |
||
150 | $admin = new PostAdmin( |
||
151 | 'sonata.post.admin.post', |
||
152 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
153 | 'SonataNewsBundle:PostAdmin' |
||
154 | ); |
||
155 | $securityHandler = $this->prophesize(SecurityHandlerInterface::class); |
||
156 | $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true); |
||
157 | $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(false); |
||
158 | $customExtension = $this->prophesize(AbstractAdminExtension::class); |
||
159 | $customExtension->getAccessMapping($admin)->willReturn( |
||
160 | ['custom_action' => ['CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE']] |
||
161 | ); |
||
162 | $admin->addExtension($customExtension->reveal()); |
||
163 | $admin->setSecurityHandler($securityHandler->reveal()); |
||
164 | $this->expectException( |
||
165 | AccessDeniedException::class |
||
166 | ); |
||
167 | $this->expectExceptionMessage( |
||
168 | 'Access Denied to the action custom_action and role EXTRA_CUSTOM_ROLE' |
||
169 | ); |
||
170 | $admin->checkAccess('custom_action'); |
||
171 | } |
||
172 | |||
173 | public function testHasAccessOnMadeUpAction(): void |
||
174 | { |
||
175 | $admin = new PostAdmin( |
||
176 | 'sonata.post.admin.post', |
||
177 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
178 | 'SonataNewsBundle:PostAdmin' |
||
179 | ); |
||
180 | |||
181 | $this->assertFalse($admin->hasAccess('made-up')); |
||
182 | } |
||
183 | |||
184 | public function testHasAccess(): void |
||
185 | { |
||
186 | $admin = new PostAdmin( |
||
187 | 'sonata.post.admin.post', |
||
188 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
189 | 'SonataNewsBundle:PostAdmin' |
||
190 | ); |
||
191 | $securityHandler = $this->prophesize(SecurityHandlerInterface::class); |
||
192 | $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true); |
||
193 | $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(false); |
||
194 | $customExtension = $this->prophesize(AbstractAdminExtension::class); |
||
195 | $customExtension->getAccessMapping($admin)->willReturn( |
||
196 | ['custom_action' => ['CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE']] |
||
197 | ); |
||
198 | $admin->addExtension($customExtension->reveal()); |
||
199 | $admin->setSecurityHandler($securityHandler->reveal()); |
||
200 | |||
201 | $this->assertFalse($admin->hasAccess('custom_action')); |
||
202 | } |
||
203 | |||
204 | public function testHasAccessAllowsAccess(): void |
||
205 | { |
||
206 | $admin = new PostAdmin( |
||
207 | 'sonata.post.admin.post', |
||
208 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
209 | 'SonataNewsBundle:PostAdmin' |
||
210 | ); |
||
211 | $securityHandler = $this->prophesize(SecurityHandlerInterface::class); |
||
212 | $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true); |
||
213 | $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(true); |
||
214 | $customExtension = $this->prophesize(AbstractAdminExtension::class); |
||
215 | $customExtension->getAccessMapping($admin)->willReturn( |
||
216 | ['custom_action' => ['CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE']] |
||
217 | ); |
||
218 | $admin->addExtension($customExtension->reveal()); |
||
219 | $admin->setSecurityHandler($securityHandler->reveal()); |
||
220 | |||
221 | $this->assertTrue($admin->hasAccess('custom_action')); |
||
222 | } |
||
223 | |||
224 | public function testHasAccessAllowsAccessEditAction(): void |
||
225 | { |
||
226 | $admin = new PostAdmin( |
||
227 | 'sonata.post.admin.post', |
||
228 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
229 | 'SonataNewsBundle:PostAdmin' |
||
230 | ); |
||
231 | $securityHandler = $this->prophesize(SecurityHandlerInterface::class); |
||
232 | $securityHandler->isGranted($admin, 'EDIT_ROLE', $admin)->willReturn(true); |
||
233 | $customExtension = $this->prophesize(AbstractAdminExtension::class); |
||
234 | $customExtension->getAccessMapping($admin)->willReturn( |
||
235 | ['edit_action' => ['EDIT_ROLE']] |
||
236 | ); |
||
237 | $admin->addExtension($customExtension->reveal()); |
||
238 | $admin->setSecurityHandler($securityHandler->reveal()); |
||
239 | |||
240 | $this->assertTrue($admin->hasAccess('edit_action')); |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasChild |
||
245 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::addChild |
||
246 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getChild |
||
247 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::isChild |
||
248 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasChildren |
||
249 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getChildren |
||
250 | */ |
||
251 | public function testChildren(): void |
||
252 | { |
||
253 | $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
254 | $this->assertFalse($postAdmin->hasChildren()); |
||
255 | $this->assertFalse($postAdmin->hasChild('comment')); |
||
256 | |||
257 | $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin'); |
||
258 | $postAdmin->addChild($commentAdmin); |
||
259 | $this->assertTrue($postAdmin->hasChildren()); |
||
260 | $this->assertTrue($postAdmin->hasChild('sonata.post.admin.comment')); |
||
261 | |||
262 | $this->assertSame('sonata.post.admin.comment', $postAdmin->getChild('sonata.post.admin.comment')->getCode()); |
||
263 | $this->assertSame('sonata.post.admin.post|sonata.post.admin.comment', $postAdmin->getChild('sonata.post.admin.comment')->getBaseCodeRoute()); |
||
264 | $this->assertSame($postAdmin, $postAdmin->getChild('sonata.post.admin.comment')->getParent()); |
||
265 | |||
266 | $this->assertFalse($postAdmin->isChild()); |
||
267 | $this->assertTrue($commentAdmin->isChild()); |
||
268 | |||
269 | $this->assertSame(['sonata.post.admin.comment' => $commentAdmin], $postAdmin->getChildren()); |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configure |
||
274 | */ |
||
275 | public function testConfigure(): void |
||
276 | { |
||
277 | $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
278 | $this->assertNotNull($admin->getUniqid()); |
||
279 | |||
280 | $admin->initialize(); |
||
281 | $this->assertNotNull($admin->getUniqid()); |
||
282 | $this->assertSame('Post', $admin->getClassnameLabel()); |
||
283 | |||
284 | $admin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin'); |
||
285 | $admin->setClassnameLabel('postcomment'); |
||
286 | |||
287 | $admin->initialize(); |
||
288 | $this->assertSame('postcomment', $admin->getClassnameLabel()); |
||
289 | } |
||
290 | |||
291 | public function testConfigureWithValidParentAssociationMapping(): void |
||
292 | { |
||
293 | $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
294 | $admin->setParentAssociationMapping('Category'); |
||
295 | |||
296 | $admin->initialize(); |
||
297 | $this->assertSame('Category', $admin->getParentAssociationMapping()); |
||
298 | } |
||
299 | |||
300 | public function provideGetBaseRoutePattern() |
||
301 | { |
||
302 | return [ |
||
303 | [ |
||
304 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
305 | '/sonata/news/post', |
||
306 | ], |
||
307 | [ |
||
308 | 'Application\Sonata\NewsBundle\Document\Post', |
||
309 | '/sonata/news/post', |
||
310 | ], |
||
311 | [ |
||
312 | 'MyApplication\MyBundle\Entity\Post', |
||
313 | '/myapplication/my/post', |
||
314 | ], |
||
315 | [ |
||
316 | 'MyApplication\MyBundle\Entity\Post\Category', |
||
317 | '/myapplication/my/post-category', |
||
318 | ], |
||
319 | [ |
||
320 | 'MyApplication\MyBundle\Entity\Product\Category', |
||
321 | '/myapplication/my/product-category', |
||
322 | ], |
||
323 | [ |
||
324 | 'MyApplication\MyBundle\Entity\Other\Product\Category', |
||
325 | '/myapplication/my/other-product-category', |
||
326 | ], |
||
327 | [ |
||
328 | 'Symfony\Cmf\Bundle\FooBundle\Document\Menu', |
||
329 | '/cmf/foo/menu', |
||
330 | ], |
||
331 | [ |
||
332 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Phpcr\Menu', |
||
333 | '/cmf/foo/menu', |
||
334 | ], |
||
335 | [ |
||
336 | 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu', |
||
337 | '/symfony/barbar/menu', |
||
338 | ], |
||
339 | [ |
||
340 | 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu\Item', |
||
341 | '/symfony/barbar/menu-item', |
||
342 | ], |
||
343 | [ |
||
344 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Orm\Menu', |
||
345 | '/cmf/foo/menu', |
||
346 | ], |
||
347 | [ |
||
348 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\MongoDB\Menu', |
||
349 | '/cmf/foo/menu', |
||
350 | ], |
||
351 | [ |
||
352 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\CouchDB\Menu', |
||
353 | '/cmf/foo/menu', |
||
354 | ], |
||
355 | [ |
||
356 | 'AppBundle\Entity\User', |
||
357 | '/app/user', |
||
358 | ], |
||
359 | [ |
||
360 | 'App\Entity\User', |
||
361 | '/app/user', |
||
362 | ], |
||
363 | ]; |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * @dataProvider provideGetBaseRoutePattern |
||
368 | */ |
||
369 | public function testGetBaseRoutePattern($objFqn, $expected): void |
||
370 | { |
||
371 | $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin'); |
||
372 | $this->assertSame($expected, $admin->getBaseRoutePattern()); |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * @dataProvider provideGetBaseRoutePattern |
||
377 | */ |
||
378 | public function testGetBaseRoutePatternWithChildAdmin($objFqn, $expected): void |
||
379 | { |
||
380 | $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin'); |
||
381 | $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin'); |
||
382 | $commentAdmin->setParent($postAdmin); |
||
383 | |||
384 | $this->assertSame($expected.'/{id}/comment', $commentAdmin->getBaseRoutePattern()); |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * @dataProvider provideGetBaseRoutePattern |
||
389 | */ |
||
390 | public function testGetBaseRoutePatternWithTwoNestedChildAdmin($objFqn, $expected): void |
||
391 | { |
||
392 | $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin'); |
||
393 | $commentAdmin = new CommentAdmin( |
||
394 | 'sonata.post.admin.comment', |
||
395 | 'Application\Sonata\NewsBundle\Entity\Comment', |
||
396 | 'SonataNewsBundle:CommentAdmin' |
||
397 | ); |
||
398 | $commentVoteAdmin = new CommentVoteAdmin( |
||
399 | 'sonata.post.admin.comment_vote', |
||
400 | 'Application\Sonata\NewsBundle\Entity\CommentVote', |
||
401 | 'SonataNewsBundle:CommentVoteAdmin' |
||
402 | ); |
||
403 | $commentAdmin->setParent($postAdmin); |
||
404 | $commentVoteAdmin->setParent($commentAdmin); |
||
405 | |||
406 | $this->assertSame($expected.'/{id}/comment/{childId}/commentvote', $commentVoteAdmin->getBaseRoutePattern()); |
||
407 | } |
||
408 | |||
409 | public function testGetBaseRoutePatternWithSpecifedPattern(): void |
||
410 | { |
||
411 | $postAdmin = new PostWithCustomRouteAdmin('sonata.post.admin.post_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostWithCustomRouteAdmin'); |
||
412 | |||
413 | $this->assertSame('/post-custom', $postAdmin->getBaseRoutePattern()); |
||
414 | } |
||
415 | |||
416 | public function testGetBaseRoutePatternWithChildAdminAndWithSpecifedPattern(): void |
||
417 | { |
||
418 | $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
419 | $commentAdmin = new CommentWithCustomRouteAdmin('sonata.post.admin.comment_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentWithCustomRouteAdmin'); |
||
420 | $commentAdmin->setParent($postAdmin); |
||
421 | |||
422 | $this->assertSame('/sonata/news/post/{id}/comment-custom', $commentAdmin->getBaseRoutePattern()); |
||
423 | } |
||
424 | |||
425 | public function testGetBaseRoutePatternWithUnreconizedClassname(): void |
||
426 | { |
||
427 | $this->expectException(\RuntimeException::class); |
||
428 | |||
429 | $admin = new PostAdmin('sonata.post.admin.post', 'News\Thing\Post', 'SonataNewsBundle:PostAdmin'); |
||
430 | $admin->getBaseRoutePattern(); |
||
431 | } |
||
432 | |||
433 | public function provideGetBaseRouteName() |
||
434 | { |
||
435 | return [ |
||
436 | [ |
||
437 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
438 | 'admin_sonata_news_post', |
||
439 | ], |
||
440 | [ |
||
441 | 'Application\Sonata\NewsBundle\Document\Post', |
||
442 | 'admin_sonata_news_post', |
||
443 | ], |
||
444 | [ |
||
445 | 'MyApplication\MyBundle\Entity\Post', |
||
446 | 'admin_myapplication_my_post', |
||
447 | ], |
||
448 | [ |
||
449 | 'MyApplication\MyBundle\Entity\Post\Category', |
||
450 | 'admin_myapplication_my_post_category', |
||
451 | ], |
||
452 | [ |
||
453 | 'MyApplication\MyBundle\Entity\Product\Category', |
||
454 | 'admin_myapplication_my_product_category', |
||
455 | ], |
||
456 | [ |
||
457 | 'MyApplication\MyBundle\Entity\Other\Product\Category', |
||
458 | 'admin_myapplication_my_other_product_category', |
||
459 | ], |
||
460 | [ |
||
461 | 'Symfony\Cmf\Bundle\FooBundle\Document\Menu', |
||
462 | 'admin_cmf_foo_menu', |
||
463 | ], |
||
464 | [ |
||
465 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Phpcr\Menu', |
||
466 | 'admin_cmf_foo_menu', |
||
467 | ], |
||
468 | [ |
||
469 | 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu', |
||
470 | 'admin_symfony_barbar_menu', |
||
471 | ], |
||
472 | [ |
||
473 | 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu\Item', |
||
474 | 'admin_symfony_barbar_menu_item', |
||
475 | ], |
||
476 | [ |
||
477 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Orm\Menu', |
||
478 | 'admin_cmf_foo_menu', |
||
479 | ], |
||
480 | [ |
||
481 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\MongoDB\Menu', |
||
482 | 'admin_cmf_foo_menu', |
||
483 | ], |
||
484 | [ |
||
485 | 'Symfony\Cmf\Bundle\FooBundle\Doctrine\CouchDB\Menu', |
||
486 | 'admin_cmf_foo_menu', |
||
487 | ], |
||
488 | [ |
||
489 | 'AppBundle\Entity\User', |
||
490 | 'admin_app_user', |
||
491 | ], |
||
492 | [ |
||
493 | 'App\Entity\User', |
||
494 | 'admin_app_user', |
||
495 | ], |
||
496 | ]; |
||
497 | } |
||
498 | |||
499 | /** |
||
500 | * @dataProvider provideGetBaseRouteName |
||
501 | */ |
||
502 | public function testGetBaseRouteName($objFqn, $expected): void |
||
503 | { |
||
504 | $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin'); |
||
505 | |||
506 | $this->assertSame($expected, $admin->getBaseRouteName()); |
||
507 | } |
||
508 | |||
509 | /** |
||
510 | * @dataProvider provideGetBaseRouteName |
||
511 | */ |
||
512 | public function testGetBaseRouteNameWithChildAdmin($objFqn, $expected): void |
||
513 | { |
||
514 | $routeGenerator = new DefaultRouteGenerator( |
||
515 | $this->createMock(RouterInterface::class), |
||
516 | new RoutesCache($this->cacheTempFolder, true) |
||
517 | ); |
||
518 | |||
519 | $container = new Container(); |
||
520 | $pool = new Pool($container, 'Sonata Admin', '/path/to/pic.png'); |
||
521 | |||
522 | $pathInfo = new PathInfoBuilder($this->createMock(AuditManagerInterface::class)); |
||
523 | $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin'); |
||
524 | $container->set('sonata.post.admin.post', $postAdmin); |
||
525 | $postAdmin->setConfigurationPool($pool); |
||
526 | $postAdmin->setRouteBuilder($pathInfo); |
||
527 | $postAdmin->setRouteGenerator($routeGenerator); |
||
528 | $postAdmin->initialize(); |
||
529 | |||
530 | $commentAdmin = new CommentAdmin( |
||
531 | 'sonata.post.admin.comment', |
||
532 | 'Application\Sonata\NewsBundle\Entity\Comment', |
||
533 | 'SonataNewsBundle:CommentAdmin' |
||
534 | ); |
||
535 | $container->set('sonata.post.admin.comment', $commentAdmin); |
||
536 | $commentAdmin->setConfigurationPool($pool); |
||
537 | $commentAdmin->setRouteBuilder($pathInfo); |
||
538 | $commentAdmin->setRouteGenerator($routeGenerator); |
||
539 | $commentAdmin->initialize(); |
||
540 | |||
541 | $postAdmin->addChild($commentAdmin); |
||
542 | |||
543 | $commentVoteAdmin = new CommentVoteAdmin( |
||
544 | 'sonata.post.admin.comment_vote', |
||
545 | 'Application\Sonata\NewsBundle\Entity\CommentVote', |
||
546 | 'SonataNewsBundle:CommentVoteAdmin' |
||
547 | ); |
||
548 | $container->set('sonata.post.admin.comment_vote', $commentVoteAdmin); |
||
549 | $commentVoteAdmin->setConfigurationPool($pool); |
||
550 | $commentVoteAdmin->setRouteBuilder($pathInfo); |
||
551 | $commentVoteAdmin->setRouteGenerator($routeGenerator); |
||
552 | $commentVoteAdmin->initialize(); |
||
553 | |||
554 | $commentAdmin->addChild($commentVoteAdmin); |
||
555 | $pool->setAdminServiceIds([ |
||
556 | 'sonata.post.admin.post', |
||
557 | 'sonata.post.admin.comment', |
||
558 | 'sonata.post.admin.comment_vote', |
||
559 | ]); |
||
560 | |||
561 | $this->assertSame($expected.'_comment', $commentAdmin->getBaseRouteName()); |
||
562 | |||
563 | $this->assertTrue($postAdmin->hasRoute('show')); |
||
564 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post.show')); |
||
565 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment.show')); |
||
566 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment|sonata.post.admin.comment_vote.show')); |
||
567 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.comment.list')); |
||
568 | $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.comment|sonata.post.admin.comment_vote.list')); |
||
569 | $this->assertFalse($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment.edit')); |
||
570 | $this->assertFalse($commentAdmin->hasRoute('edit')); |
||
571 | |||
572 | /* |
||
573 | * Test the route name from request |
||
574 | */ |
||
575 | $postListRequest = new Request( |
||
576 | [], |
||
577 | [], |
||
578 | [ |
||
579 | '_route' => $postAdmin->getBaseRouteName().'_list', |
||
580 | ] |
||
581 | ); |
||
582 | |||
583 | $postAdmin->setRequest($postListRequest); |
||
584 | $commentAdmin->setRequest($postListRequest); |
||
585 | |||
586 | $this->assertTrue($postAdmin->isCurrentRoute('list')); |
||
587 | $this->assertFalse($postAdmin->isCurrentRoute('create')); |
||
588 | $this->assertFalse($commentAdmin->isCurrentRoute('list')); |
||
589 | $this->assertFalse($commentVoteAdmin->isCurrentRoute('list')); |
||
590 | $this->assertTrue($commentAdmin->isCurrentRoute('list', 'sonata.post.admin.post')); |
||
591 | $this->assertFalse($commentAdmin->isCurrentRoute('edit', 'sonata.post.admin.post')); |
||
592 | $this->assertTrue($commentVoteAdmin->isCurrentRoute('list', 'sonata.post.admin.post')); |
||
593 | $this->assertFalse($commentVoteAdmin->isCurrentRoute('edit', 'sonata.post.admin.post')); |
||
594 | } |
||
595 | |||
596 | public function testGetBaseRouteNameWithUnreconizedClassname(): void |
||
597 | { |
||
598 | $this->expectException(\RuntimeException::class); |
||
599 | |||
600 | $admin = new PostAdmin('sonata.post.admin.post', 'News\Thing\Post', 'SonataNewsBundle:PostAdmin'); |
||
601 | $admin->getBaseRouteName(); |
||
602 | } |
||
603 | |||
604 | public function testGetBaseRouteNameWithSpecifiedName(): void |
||
605 | { |
||
606 | $postAdmin = new PostWithCustomRouteAdmin('sonata.post.admin.post_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
607 | |||
608 | $this->assertSame('post_custom', $postAdmin->getBaseRouteName()); |
||
609 | } |
||
610 | |||
611 | public function testGetBaseRouteNameWithChildAdminAndWithSpecifiedName(): void |
||
612 | { |
||
613 | $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
614 | $commentAdmin = new CommentWithCustomRouteAdmin('sonata.post.admin.comment_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentWithCustomRouteAdmin'); |
||
615 | $commentAdmin->setParent($postAdmin); |
||
616 | |||
617 | $this->assertSame('admin_sonata_news_post_comment_custom', $commentAdmin->getBaseRouteName()); |
||
618 | } |
||
619 | |||
620 | public function testGetBaseRouteNameWithTwoNestedChildAdminAndWithSpecifiedName(): void |
||
621 | { |
||
622 | $postAdmin = new PostAdmin( |
||
623 | 'sonata.post.admin.post', |
||
624 | 'Application\Sonata\NewsBundle\Entity\Post', |
||
625 | 'SonataNewsBundle:PostAdmin' |
||
626 | ); |
||
627 | $commentAdmin = new CommentWithCustomRouteAdmin( |
||
628 | 'sonata.post.admin.comment_with_custom_route', |
||
629 | 'Application\Sonata\NewsBundle\Entity\Comment', |
||
630 | 'SonataNewsBundle:CommentWithCustomRouteAdmin' |
||
631 | ); |
||
632 | $commentVoteAdmin = new CommentVoteAdmin( |
||
633 | 'sonata.post.admin.comment_vote', |
||
634 | 'Application\Sonata\NewsBundle\Entity\CommentVote', |
||
635 | 'SonataNewsBundle:CommentVoteAdmin' |
||
636 | ); |
||
637 | $commentAdmin->setParent($postAdmin); |
||
638 | $commentVoteAdmin->setParent($commentAdmin); |
||
639 | |||
640 | $this->assertSame('admin_sonata_news_post_comment_custom_commentvote', $commentVoteAdmin->getBaseRouteName()); |
||
641 | } |
||
642 | |||
643 | /** |
||
644 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::setUniqid |
||
645 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getUniqid |
||
646 | */ |
||
647 | public function testUniqid(): void |
||
648 | { |
||
649 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
650 | |||
651 | $uniqid = uniqid(); |
||
652 | $admin->setUniqid($uniqid); |
||
653 | |||
654 | $this->assertSame($uniqid, $admin->getUniqid()); |
||
655 | } |
||
656 | |||
657 | public function testToString(): void |
||
658 | { |
||
659 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
660 | |||
661 | $s = new \stdClass(); |
||
662 | |||
663 | $this->assertNotEmpty($admin->toString($s)); |
||
664 | |||
665 | $s = new FooToString(); |
||
666 | $this->assertSame('salut', $admin->toString($s)); |
||
667 | |||
668 | // To string method is implemented, but returns null |
||
669 | $s = new FooToStringNull(); |
||
670 | $this->assertNotEmpty($admin->toString($s)); |
||
671 | |||
672 | $this->assertSame('', $admin->toString(false)); |
||
673 | } |
||
674 | |||
675 | public function testIsAclEnabled(): void |
||
676 | { |
||
677 | $postAdmin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
678 | |||
679 | $this->assertFalse($postAdmin->isAclEnabled()); |
||
680 | |||
681 | $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin'); |
||
682 | $commentAdmin->setSecurityHandler($this->createMock(AclSecurityHandlerInterface::class)); |
||
683 | $this->assertTrue($commentAdmin->isAclEnabled()); |
||
684 | } |
||
685 | |||
686 | /** |
||
687 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getSubClasses |
||
688 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getSubClass |
||
689 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::setSubClasses |
||
690 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasSubClass |
||
691 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass |
||
692 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubClass |
||
693 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubclassCode |
||
694 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getClass |
||
695 | */ |
||
696 | public function testSubClass(): void |
||
697 | { |
||
698 | $admin = new PostAdmin( |
||
699 | 'sonata.post.admin.post', |
||
700 | Post::class, |
||
701 | 'SonataNewsBundle:PostAdmin' |
||
702 | ); |
||
703 | $this->assertFalse($admin->hasSubClass('test')); |
||
704 | $this->assertFalse($admin->hasActiveSubClass()); |
||
705 | $this->assertCount(0, $admin->getSubClasses()); |
||
706 | $this->assertNull($admin->getActiveSubClass()); |
||
707 | $this->assertNull($admin->getActiveSubclassCode()); |
||
708 | $this->assertSame(Post::class, $admin->getClass()); |
||
709 | |||
710 | // Just for the record, if there is no inheritance set, the getSubject is not used |
||
711 | // the getSubject can also lead to some issue |
||
712 | $admin->setSubject(new BlogPost()); |
||
713 | $this->assertSame(BlogPost::class, $admin->getClass()); |
||
714 | |||
715 | $admin->setSubClasses([ |
||
716 | 'extended1' => 'NewsBundle\Entity\PostExtended1', |
||
717 | 'extended2' => 'NewsBundle\Entity\PostExtended2', |
||
718 | ]); |
||
719 | $this->assertFalse($admin->hasSubClass('test')); |
||
720 | $this->assertTrue($admin->hasSubClass('extended1')); |
||
721 | $this->assertFalse($admin->hasActiveSubClass()); |
||
722 | $this->assertCount(2, $admin->getSubClasses()); |
||
723 | $this->assertNull($admin->getActiveSubClass()); |
||
724 | $this->assertNull($admin->getActiveSubclassCode()); |
||
725 | $this->assertSame( |
||
726 | BlogPost::class, |
||
727 | $admin->getClass(), |
||
728 | 'When there is no subclass in the query the class parameter should be returned' |
||
729 | ); |
||
730 | |||
731 | $request = new Request(['subclass' => 'extended1']); |
||
732 | $admin->setRequest($request); |
||
733 | $this->assertFalse($admin->hasSubClass('test')); |
||
734 | $this->assertTrue($admin->hasSubClass('extended1')); |
||
735 | $this->assertTrue($admin->hasActiveSubClass()); |
||
736 | $this->assertCount(2, $admin->getSubClasses()); |
||
737 | $this->assertSame( |
||
738 | 'NewsBundle\Entity\PostExtended1', |
||
739 | $admin->getActiveSubClass(), |
||
740 | 'It should return the curently active sub class.' |
||
741 | ); |
||
742 | $this->assertSame('extended1', $admin->getActiveSubclassCode()); |
||
743 | $this->assertSame( |
||
744 | 'NewsBundle\Entity\PostExtended1', |
||
745 | $admin->getClass(), |
||
746 | 'getClass() should return the name of the sub class when passed through a request query parameter.' |
||
747 | ); |
||
748 | |||
749 | $request->query->set('subclass', 'inject'); |
||
750 | $this->assertNull($admin->getActiveSubclassCode()); |
||
751 | } |
||
752 | |||
753 | public function testNonExistantSubclass(): void |
||
754 | { |
||
755 | $this->expectException(\RuntimeException::class); |
||
756 | |||
757 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
758 | $admin->setModelManager($this->getMockForAbstractClass(ModelManagerInterface::class)); |
||
759 | |||
760 | $admin->setRequest(new Request(['subclass' => 'inject'])); |
||
761 | |||
762 | $admin->setSubClasses(['extended1' => 'NewsBundle\Entity\PostExtended1', 'extended2' => 'NewsBundle\Entity\PostExtended2']); |
||
763 | |||
764 | $this->assertTrue($admin->hasActiveSubClass()); |
||
765 | |||
766 | $admin->getActiveSubClass(); |
||
767 | } |
||
768 | |||
769 | /** |
||
770 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass |
||
771 | */ |
||
772 | public function testOnlyOneSubclassNeededToBeActive(): void |
||
773 | { |
||
774 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
775 | $admin->setSubClasses(['extended1' => 'NewsBundle\Entity\PostExtended1']); |
||
776 | $request = new Request(['subclass' => 'extended1']); |
||
777 | $admin->setRequest($request); |
||
778 | $this->assertTrue($admin->hasActiveSubClass()); |
||
779 | } |
||
780 | |||
781 | /** |
||
782 | * @group legacy |
||
783 | * @expectedDeprecation Method "Sonata\AdminBundle\Admin\AbstractAdmin::addSubClass" is deprecated since 3.x and will be removed in 4.0. |
||
784 | */ |
||
785 | public function testAddSubClassIsDeprecated(): void |
||
786 | { |
||
787 | $admin = new PostAdmin( |
||
788 | 'sonata.post.admin.post', |
||
789 | Post::class, |
||
790 | 'SonataNewsBundle:PostAdmin' |
||
791 | ); |
||
792 | $admin->addSubClass('whatever'); |
||
793 | } |
||
794 | |||
795 | public function testGetPerPageOptions(): void |
||
796 | { |
||
797 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
798 | |||
799 | $this->assertSame([16, 32, 64, 128, 192], $admin->getPerPageOptions()); |
||
800 | $admin->setPerPageOptions([500, 1000]); |
||
801 | $this->assertSame([500, 1000], $admin->getPerPageOptions()); |
||
802 | } |
||
803 | |||
804 | public function testGetLabelTranslatorStrategy(): void |
||
805 | { |
||
806 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
807 | |||
808 | $this->assertNull($admin->getLabelTranslatorStrategy()); |
||
809 | |||
810 | $labelTranslatorStrategy = $this->createMock(LabelTranslatorStrategyInterface::class); |
||
811 | $admin->setLabelTranslatorStrategy($labelTranslatorStrategy); |
||
812 | $this->assertSame($labelTranslatorStrategy, $admin->getLabelTranslatorStrategy()); |
||
813 | } |
||
814 | |||
815 | public function testGetRouteBuilder(): void |
||
816 | { |
||
817 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
818 | |||
819 | $this->assertNull($admin->getRouteBuilder()); |
||
820 | |||
821 | $routeBuilder = $this->createMock(RouteBuilderInterface::class); |
||
822 | $admin->setRouteBuilder($routeBuilder); |
||
823 | $this->assertSame($routeBuilder, $admin->getRouteBuilder()); |
||
824 | } |
||
825 | |||
826 | public function testGetMenuFactory(): void |
||
827 | { |
||
828 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
829 | |||
830 | $this->assertNull($admin->getMenuFactory()); |
||
831 | |||
832 | $menuFactory = $this->createMock(FactoryInterface::class); |
||
833 | $admin->setMenuFactory($menuFactory); |
||
834 | $this->assertSame($menuFactory, $admin->getMenuFactory()); |
||
835 | } |
||
836 | |||
837 | public function testGetExtensions(): void |
||
838 | { |
||
839 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
840 | |||
841 | $this->assertSame([], $admin->getExtensions()); |
||
842 | |||
843 | $adminExtension1 = $this->createMock(AdminExtensionInterface::class); |
||
844 | $adminExtension2 = $this->createMock(AdminExtensionInterface::class); |
||
845 | |||
846 | $admin->addExtension($adminExtension1); |
||
847 | $admin->addExtension($adminExtension2); |
||
848 | $this->assertSame([$adminExtension1, $adminExtension2], $admin->getExtensions()); |
||
849 | } |
||
850 | |||
851 | public function testGetFilterTheme(): void |
||
852 | { |
||
853 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
854 | |||
855 | $this->assertSame([], $admin->getFilterTheme()); |
||
856 | |||
857 | $admin->setFilterTheme(['FooTheme']); |
||
858 | $this->assertSame(['FooTheme'], $admin->getFilterTheme()); |
||
859 | } |
||
860 | |||
861 | public function testGetFormTheme(): void |
||
862 | { |
||
863 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
864 | |||
865 | $this->assertSame([], $admin->getFormTheme()); |
||
866 | |||
867 | $admin->setFormTheme(['FooTheme']); |
||
868 | |||
869 | $this->assertSame(['FooTheme'], $admin->getFormTheme()); |
||
870 | } |
||
871 | |||
872 | public function testGetValidator(): void |
||
873 | { |
||
874 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
875 | |||
876 | $this->assertNull($admin->getValidator()); |
||
877 | |||
878 | $validator = $this->getMockForAbstractClass(ValidatorInterface::class); |
||
879 | |||
880 | $admin->setValidator($validator); |
||
881 | $this->assertSame($validator, $admin->getValidator()); |
||
882 | } |
||
883 | |||
884 | public function testGetSecurityHandler(): void |
||
885 | { |
||
886 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
887 | |||
888 | $this->assertNull($admin->getSecurityHandler()); |
||
889 | |||
890 | $securityHandler = $this->createMock(SecurityHandlerInterface::class); |
||
891 | $admin->setSecurityHandler($securityHandler); |
||
892 | $this->assertSame($securityHandler, $admin->getSecurityHandler()); |
||
893 | } |
||
894 | |||
895 | public function testGetSecurityInformation(): void |
||
896 | { |
||
897 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
898 | |||
899 | $this->assertSame([], $admin->getSecurityInformation()); |
||
900 | |||
901 | $securityInformation = [ |
||
902 | 'GUEST' => ['VIEW', 'LIST'], |
||
903 | 'STAFF' => ['EDIT', 'LIST', 'CREATE'], |
||
904 | ]; |
||
905 | |||
906 | $admin->setSecurityInformation($securityInformation); |
||
907 | $this->assertSame($securityInformation, $admin->getSecurityInformation()); |
||
908 | } |
||
909 | |||
910 | public function testGetManagerType(): void |
||
911 | { |
||
912 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
913 | |||
914 | $this->assertNull($admin->getManagerType()); |
||
915 | |||
916 | $admin->setManagerType('foo_orm'); |
||
917 | $this->assertSame('foo_orm', $admin->getManagerType()); |
||
918 | } |
||
919 | |||
920 | public function testGetModelManager(): void |
||
921 | { |
||
922 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
923 | |||
924 | $this->assertNull($admin->getModelManager()); |
||
925 | |||
926 | $modelManager = $this->createMock(ModelManagerInterface::class); |
||
927 | |||
928 | $admin->setModelManager($modelManager); |
||
929 | $this->assertSame($modelManager, $admin->getModelManager()); |
||
930 | } |
||
931 | |||
932 | public function testGetBaseCodeRoute(): void |
||
933 | { |
||
934 | $postAdmin = new PostAdmin( |
||
935 | 'sonata.post.admin.post', |
||
936 | 'NewsBundle\Entity\Post', |
||
937 | 'SonataNewsBundle:PostAdmin' |
||
938 | ); |
||
939 | $commentAdmin = new CommentAdmin( |
||
940 | 'sonata.post.admin.comment', |
||
941 | 'Application\Sonata\NewsBundle\Entity\Comment', |
||
942 | 'SonataNewsBundle:CommentAdmin' |
||
943 | ); |
||
944 | |||
945 | $this->assertSame($postAdmin->getCode(), $postAdmin->getBaseCodeRoute()); |
||
946 | |||
947 | $postAdmin->addChild($commentAdmin); |
||
948 | |||
949 | $this->assertSame( |
||
950 | 'sonata.post.admin.post|sonata.post.admin.comment', |
||
951 | $commentAdmin->getBaseCodeRoute() |
||
952 | ); |
||
953 | } |
||
954 | |||
955 | public function testGetRouteGenerator(): void |
||
956 | { |
||
957 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
958 | |||
959 | $this->assertNull($admin->getRouteGenerator()); |
||
960 | |||
961 | $routeGenerator = $this->createMock(RouteGeneratorInterface::class); |
||
962 | |||
963 | $admin->setRouteGenerator($routeGenerator); |
||
964 | $this->assertSame($routeGenerator, $admin->getRouteGenerator()); |
||
965 | } |
||
966 | |||
967 | public function testGetConfigurationPool(): void |
||
968 | { |
||
969 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
970 | |||
971 | $this->assertNull($admin->getConfigurationPool()); |
||
972 | |||
973 | $pool = $this->getMockBuilder(Pool::class) |
||
974 | ->disableOriginalConstructor() |
||
975 | ->getMock(); |
||
976 | |||
977 | $admin->setConfigurationPool($pool); |
||
978 | $this->assertSame($pool, $admin->getConfigurationPool()); |
||
979 | } |
||
980 | |||
981 | public function testGetShowBuilder(): void |
||
982 | { |
||
983 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
984 | |||
985 | $this->assertNull($admin->getShowBuilder()); |
||
986 | |||
987 | $showBuilder = $this->createMock(ShowBuilderInterface::class); |
||
988 | |||
989 | $admin->setShowBuilder($showBuilder); |
||
990 | $this->assertSame($showBuilder, $admin->getShowBuilder()); |
||
991 | } |
||
992 | |||
993 | public function testGetListBuilder(): void |
||
994 | { |
||
995 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
996 | |||
997 | $this->assertNull($admin->getListBuilder()); |
||
998 | |||
999 | $listBuilder = $this->createMock(ListBuilderInterface::class); |
||
1000 | |||
1001 | $admin->setListBuilder($listBuilder); |
||
1002 | $this->assertSame($listBuilder, $admin->getListBuilder()); |
||
1003 | } |
||
1004 | |||
1005 | public function testGetDatagridBuilder(): void |
||
1006 | { |
||
1007 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1008 | |||
1009 | $this->assertNull($admin->getDatagridBuilder()); |
||
1010 | |||
1011 | $datagridBuilder = $this->createMock(DatagridBuilderInterface::class); |
||
1012 | |||
1013 | $admin->setDatagridBuilder($datagridBuilder); |
||
1014 | $this->assertSame($datagridBuilder, $admin->getDatagridBuilder()); |
||
1015 | } |
||
1016 | |||
1017 | public function testGetFormContractor(): void |
||
1018 | { |
||
1019 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1020 | |||
1021 | $this->assertNull($admin->getFormContractor()); |
||
1022 | |||
1023 | $formContractor = $this->createMock(FormContractorInterface::class); |
||
1024 | |||
1025 | $admin->setFormContractor($formContractor); |
||
1026 | $this->assertSame($formContractor, $admin->getFormContractor()); |
||
1027 | } |
||
1028 | |||
1029 | public function testGetRequest(): void |
||
1030 | { |
||
1031 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1032 | |||
1033 | $this->assertFalse($admin->hasRequest()); |
||
1034 | |||
1035 | $request = new Request(); |
||
1036 | |||
1037 | $admin->setRequest($request); |
||
1038 | $this->assertSame($request, $admin->getRequest()); |
||
1039 | $this->assertTrue($admin->hasRequest()); |
||
1040 | } |
||
1041 | |||
1042 | public function testGetRequestWithException(): void |
||
1043 | { |
||
1044 | $this->expectException(\RuntimeException::class); |
||
1045 | $this->expectExceptionMessage('The Request object has not been set'); |
||
1046 | |||
1047 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1048 | $admin->getRequest(); |
||
1049 | } |
||
1050 | |||
1051 | public function testGetTranslationDomain(): void |
||
1060 | |||
1061 | /** |
||
1062 | * @group legacy |
||
1063 | */ |
||
1064 | public function testGetTranslator(): void |
||
1075 | |||
1076 | public function testGetShowGroups(): void |
||
1087 | |||
1088 | public function testGetFormGroups(): void |
||
1099 | |||
1100 | public function testGetMaxPageLinks(): void |
||
1109 | |||
1110 | public function testGetMaxPerPage(): void |
||
1119 | |||
1120 | public function testGetLabel(): void |
||
1129 | |||
1130 | public function testGetBaseController(): void |
||
1139 | |||
1140 | public function testGetTemplates(): void |
||
1155 | |||
1156 | public function testGetTemplate1(): void |
||
1168 | |||
1169 | public function testGetTemplate2(): void |
||
1186 | |||
1187 | public function testGetIdParameter(): void |
||
1218 | |||
1219 | public function testGetExportFormats(): void |
||
1225 | |||
1226 | public function testGetUrlsafeIdentifier(): void |
||
1241 | |||
1242 | public function testDeterminedPerPageValue(): void |
||
1262 | |||
1263 | public function testIsGranted(): void |
||
1289 | |||
1290 | public function testSupportsPreviewMode(): void |
||
1296 | |||
1297 | public function testGetPermissionsShow(): void |
||
1305 | |||
1306 | public function testShowIn(): void |
||
1327 | |||
1328 | public function testGetObjectIdentifier(): void |
||
1334 | |||
1335 | /** |
||
1336 | * @group legacy |
||
1337 | */ |
||
1338 | public function testTrans(): void |
||
1353 | |||
1354 | /** |
||
1355 | * @group legacy |
||
1356 | */ |
||
1357 | public function testTransWithMessageDomain(): void |
||
1371 | |||
1372 | /** |
||
1373 | * @group legacy |
||
1374 | */ |
||
1375 | public function testTransChoice(): void |
||
1390 | |||
1391 | /** |
||
1392 | * @group legacy |
||
1393 | */ |
||
1394 | public function testTransChoiceWithMessageDomain(): void |
||
1408 | |||
1409 | public function testSetPersistFilters(): void |
||
1417 | |||
1418 | public function testGetRootCode(): void |
||
1435 | |||
1436 | public function testGetRoot(): void |
||
1453 | |||
1454 | public function testGetExportFields(): void |
||
1467 | |||
1468 | public function testGetPersistentParametersWithNoExtension(): void |
||
1474 | |||
1475 | public function testGetPersistentParametersWithInvalidExtension(): void |
||
1476 | { |
||
1477 | $this->expectException(\RuntimeException::class); |
||
1478 | |||
1479 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1480 | |||
1481 | $extension = $this->createMock(AdminExtensionInterface::class); |
||
1482 | $extension->expects($this->once())->method('getPersistentParameters')->will($this->returnValue(null)); |
||
1483 | |||
1484 | $admin->addExtension($extension); |
||
1485 | |||
1486 | $admin->getPersistentParameters(); |
||
1487 | } |
||
1488 | |||
1489 | public function testGetPersistentParametersWithValidExtension(): void |
||
1490 | { |
||
1491 | $expected = [ |
||
1492 | 'context' => 'foobar', |
||
1493 | ]; |
||
1494 | |||
1495 | $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin'); |
||
1496 | |||
1497 | $extension = $this->createMock(AdminExtensionInterface::class); |
||
1498 | $extension->expects($this->once())->method('getPersistentParameters')->will($this->returnValue($expected)); |
||
1499 | |||
1500 | $admin->addExtension($extension); |
||
1501 | |||
1502 | $this->assertSame($expected, $admin->getPersistentParameters()); |
||
1503 | } |
||
1504 | |||
1505 | public function testGetFormWithNonCollectionParentValue(): void |
||
1506 | { |
||
1507 | $post = new Post(); |
||
1508 | $tagAdmin = $this->createTagAdmin($post); |
||
1509 | $tag = $tagAdmin->getSubject(); |
||
1510 | |||
1511 | $tag->setPosts(null); |
||
1512 | $tagAdmin->getForm(); |
||
1513 | $this->assertSame($post, $tag->getPosts()); |
||
1514 | } |
||
1515 | |||
1516 | public function testGetFormWithCollectionParentValue(): void |
||
1517 | { |
||
1518 | $post = new Post(); |
||
1519 | $tagAdmin = $this->createTagAdmin($post); |
||
1520 | $tag = $tagAdmin->getSubject(); |
||
1521 | |||
1522 | // Case of a doctrine collection |
||
1523 | $this->assertInstanceOf(Collection::class, $tag->getPosts()); |
||
1524 | $this->assertCount(0, $tag->getPosts()); |
||
1525 | |||
1526 | $tag->addPost(new Post()); |
||
1527 | |||
1528 | $this->assertCount(1, $tag->getPosts()); |
||
1529 | |||
1530 | $tagAdmin->getForm(); |
||
1531 | |||
1532 | $this->assertInstanceOf(Collection::class, $tag->getPosts()); |
||
1550 | |||
1551 | public function testRemoveFieldFromFormGroup(): void |
||
1577 | |||
1578 | public function testGetFilterParameters(): void |
||
1612 | |||
1613 | public function testGetFilterFieldDescription(): void |
||
1684 | |||
1685 | public function testGetSubjectNoRequest(): void |
||
1697 | |||
1698 | public function testGetSideMenu(): void |
||
1722 | |||
1723 | /** |
||
1724 | * @return array |
||
1725 | */ |
||
1726 | public function provideGetSubject() |
||
1736 | |||
1737 | /** |
||
1738 | * @dataProvider provideGetSubject |
||
1739 | */ |
||
1740 | public function testGetSubjectFailed($id): void |
||
1755 | |||
1756 | /** |
||
1757 | * @dataProvider provideGetSubject |
||
1758 | */ |
||
1759 | public function testGetSubject($id): void |
||
1777 | |||
1778 | public function testGetSubjectWithParentDescription(): void |
||
1807 | |||
1808 | /** |
||
1809 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons |
||
1810 | */ |
||
1811 | public function testGetActionButtonsList(): void |
||
1841 | |||
1842 | /** |
||
1843 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons |
||
1844 | */ |
||
1845 | public function testGetActionButtonsListCreateDisabled(): void |
||
1859 | |||
1860 | /** |
||
1861 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureBatchActions |
||
1862 | */ |
||
1863 | public function testGetBatchActions(): void |
||
1921 | |||
1922 | /** |
||
1923 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton |
||
1924 | */ |
||
1925 | public function testShowMosaicButton(): void |
||
1934 | |||
1935 | /** |
||
1936 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton |
||
1937 | */ |
||
1938 | public function testShowMosaicButtonHideMosaic(): void |
||
1948 | |||
1949 | /** |
||
1950 | * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getDashboardActions |
||
1951 | * @dataProvider provideGetBaseRouteName |
||
1952 | */ |
||
1953 | public function testDefaultDashboardActionsArePresent($objFqn, $expected): void |
||
1983 | |||
1984 | public function testDefaultFilters(): void |
||
2043 | |||
2044 | /** |
||
2045 | * NEXT_MAJOR: remove this method. |
||
2046 | * |
||
2047 | * @group legacy |
||
2048 | */ |
||
2049 | public function testCreateQueryLegacyCallWorks(): void |
||
2063 | |||
2064 | public function testGetDataSourceIterator(): void |
||
2106 | |||
2107 | public function testCircularChildAdmin(): void |
||
2127 | |||
2128 | public function testCircularChildAdminTripleLevel(): void |
||
2154 | |||
2155 | public function testCircularChildAdminWithItself(): void |
||
2169 | |||
2170 | public function testGetRootAncestor(): void |
||
2204 | |||
2205 | public function testGetChildDepth(): void |
||
2239 | |||
2240 | public function testGetCurrentLeafChildAdmin(): void |
||
2277 | |||
2278 | private function createTagAdmin(Post $post) |
||
2317 | } |
||
2318 |
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: