PoolTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Tests\Admin;
  11. use Sonata\AdminBundle\Admin\Pool;
  12. class PoolTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @var Sonata\AdminBundle\Admin\Pool
  16. */
  17. private $pool = null;
  18. public function setUp()
  19. {
  20. $this->pool = new Pool($this->getContainer(), 'Sonata Admin', '/path/to/pic.png');
  21. }
  22. public function testGetGroups()
  23. {
  24. $this->pool->setAdminGroups(array(
  25. 'adminGroup1' => array('sonata.user.admin.group1' => array())
  26. ));
  27. $expectedOutput = array(
  28. 'adminGroup1' => array(
  29. 'sonata.user.admin.group1' => 'adminUserClass'
  30. )
  31. );
  32. $this->assertEquals($expectedOutput, $this->pool->getGroups());
  33. }
  34. public function testGetDashboardGroups()
  35. {
  36. $admin_group1 = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  37. $admin_group1->expects($this->once())->method('showIn')->will($this->returnValue(true));
  38. $admin_group2 = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  39. $admin_group2->expects($this->once())->method('showIn')->will($this->returnValue(false));
  40. $admin_group3 = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  41. $admin_group3->expects($this->once())->method('showIn')->will($this->returnValue(false));
  42. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  43. $container->expects($this->any())->method('get')->will($this->onConsecutiveCalls(
  44. $admin_group1, $admin_group2, $admin_group3
  45. ));
  46. $pool = new Pool($container, 'Sonata Admin', '/path/to/pic.png');
  47. $pool->setAdminGroups(array(
  48. 'adminGroup1' => array(
  49. 'items' => array('itemKey' => 'sonata.user.admin.group1')
  50. ),
  51. 'adminGroup2' => array(
  52. 'items' => array('itemKey' => 'sonata.user.admin.group2')
  53. ),
  54. 'adminGroup3' => array(
  55. 'items' => array('itemKey' => 'sonata.user.admin.group3')
  56. ),
  57. ));
  58. $groups = $pool->getDashboardGroups();
  59. $this->assertCount(1, $groups);
  60. }
  61. public function testGetAdminForClassWhenAdminClassIsNotSet()
  62. {
  63. $this->pool->setAdminClasses(array('someclass' => 'sonata.user.admin.group1'));
  64. $this->assertFalse($this->pool->hasAdminByClass('notexists'));
  65. $this->assertNull($this->pool->getAdminByClass('notexists'));
  66. }
  67. public function testGetAdminForClassWhenAdminClassIsSet()
  68. {
  69. $this->pool->setAdminClasses(array('someclass' => 'sonata.user.admin.group1'));
  70. $this->assertTrue($this->pool->hasAdminByClass('someclass'));
  71. $this->assertEquals('adminUserClass', $this->pool->getAdminByClass('someclass'));
  72. }
  73. public function testGetAdminByAdminCode()
  74. {
  75. $this->assertEquals('adminUserClass', $this->pool->getAdminByAdminCode('sonata.news.admin.post'));
  76. }
  77. public function testGetAdminByAdminCodeForChildClass()
  78. {
  79. $adminMock = $this->getMockBuilder('Sonata\AdminBundle\Admin\AdminInterface')
  80. ->disableOriginalConstructor()
  81. ->getMock();
  82. $adminMock->expects($this->any())
  83. ->method('hasChild')
  84. ->will($this->returnValue(true));
  85. $adminMock->expects($this->once())
  86. ->method('getChild')
  87. ->with($this->equalTo('sonata.news.admin.comment'))
  88. ->will($this->returnValue('commentAdminClass'));
  89. $containerMock = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  90. $containerMock->expects($this->any())
  91. ->method('get')
  92. ->will($this->returnValue($adminMock));
  93. $this->pool = new Pool($containerMock, 'Sonata', '/path/to/logo.png');
  94. $this->assertEquals('commentAdminClass', $this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.comment'));
  95. }
  96. public function testGetAdminClasses()
  97. {
  98. $this->pool->setAdminClasses(array('someclass' => 'sonata.user.admin.group1'));
  99. $this->assertEquals(array('someclass' => 'sonata.user.admin.group1'), $this->pool->getAdminClasses());
  100. }
  101. public function testGetAdminGroups()
  102. {
  103. $this->pool->setAdminGroups(array('adminGroup1' => 'sonata.user.admin.group1'));
  104. $this->assertEquals(array('adminGroup1' => 'sonata.user.admin.group1'), $this->pool->getAdminGroups());
  105. }
  106. public function testGetAdminServiceIds()
  107. {
  108. $this->pool->setAdminServiceIds(array('sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3'));
  109. $this->assertEquals(array('sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3'), $this->pool->getAdminServiceIds());
  110. }
  111. public function testGetContainer()
  112. {
  113. $this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerInterface', $this->pool->getContainer());
  114. }
  115. public function testTemplates()
  116. {
  117. $this->assertInternalType('array', $this->pool->getTemplates());
  118. $this->pool->setTemplates(array('ajax' => 'Foo.html.twig'));
  119. $this->assertNull($this->pool->getTemplate('bar'));
  120. $this->assertEquals('Foo.html.twig', $this->pool->getTemplate('ajax'));
  121. }
  122. /**
  123. * @return Symfony\Component\DependencyInjection\ContainerInterface - the mock of container interface
  124. */
  125. private function getContainer()
  126. {
  127. $containerMock = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  128. $containerMock->expects($this->any())
  129. ->method('get')
  130. ->will($this->returnValue('adminUserClass'));
  131. return $containerMock;
  132. }
  133. }