PoolTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  41. $container->expects($this->any())->method('get')->will($this->onConsecutiveCalls(
  42. $admin_group1, $admin_group2
  43. ));
  44. $pool = new Pool($container, 'Sonata Admin', '/path/to/pic.png');
  45. $pool->setAdminGroups(array(
  46. 'adminGroup1' => array(
  47. 'items' => array('itemKey' => 'sonata.user.admin.group1')
  48. ),
  49. 'adminGroup2' => array(
  50. 'itmes' => array('itemKey' => 'sonata.user.admin.group1')
  51. ),
  52. 'adminGroup3' => array(
  53. 'items' => array('itemKey' => 'sonata.user.admin.group2')
  54. ),
  55. ));
  56. $groups = $pool->getDashboardGroups();
  57. $this->assertCount(1, $groups);
  58. }
  59. public function testGetAdminForClassWhenAdminClassIsNotSet()
  60. {
  61. $this->pool->setAdminClasses(array('someclass' => 'sonata.user.admin.group1'));
  62. $this->assertFalse($this->pool->hasAdminByClass('notexists'));
  63. $this->assertNull($this->pool->getAdminByClass('notexists'));
  64. }
  65. public function testGetAdminForClassWhenAdminClassIsSet()
  66. {
  67. $this->pool->setAdminClasses(array('someclass' => 'sonata.user.admin.group1'));
  68. $this->assertTrue($this->pool->hasAdminByClass('someclass'));
  69. $this->assertEquals('adminUserClass', $this->pool->getAdminByClass('someclass'));
  70. }
  71. public function testGetAdminByAdminCode()
  72. {
  73. $this->assertEquals('adminUserClass', $this->pool->getAdminByAdminCode('sonata.news.admin.post'));
  74. }
  75. public function testGetAdminByAdminCodeForChildClass()
  76. {
  77. $adminMock = $this->getMockBuilder('Sonata\AdminBundle\Admin\AdminInterface')
  78. ->disableOriginalConstructor()
  79. ->getMock();
  80. $adminMock->expects($this->any())
  81. ->method('hasChild')
  82. ->will($this->returnValue(true));
  83. $adminMock->expects($this->once())
  84. ->method('getChild')
  85. ->with($this->equalTo('sonata.news.admin.comment'))
  86. ->will($this->returnValue('commentAdminClass'));
  87. $containerMock = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  88. $containerMock->expects($this->any())
  89. ->method('get')
  90. ->will($this->returnValue($adminMock));
  91. $this->pool = new Pool($containerMock, 'Sonata', '/path/to/logo.png');
  92. $this->assertEquals('commentAdminClass', $this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.comment'));
  93. }
  94. public function testGetAdminClasses()
  95. {
  96. $this->pool->setAdminClasses(array('someclass' => 'sonata.user.admin.group1'));
  97. $this->assertEquals(array('someclass' => 'sonata.user.admin.group1'), $this->pool->getAdminClasses());
  98. }
  99. public function testGetAdminGroups()
  100. {
  101. $this->pool->setAdminGroups(array('adminGroup1' => 'sonata.user.admin.group1'));
  102. $this->assertEquals(array('adminGroup1' => 'sonata.user.admin.group1'), $this->pool->getAdminGroups());
  103. }
  104. public function testGetAdminServiceIds()
  105. {
  106. $this->pool->setAdminServiceIds(array('sonata.user.admin.group1', 'sonata.user.admin.group2'));
  107. $this->assertEquals(array('sonata.user.admin.group1', 'sonata.user.admin.group2'), $this->pool->getAdminServiceIds());
  108. }
  109. public function testGetContainer()
  110. {
  111. $this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerInterface', $this->pool->getContainer());
  112. }
  113. public function testTemplates()
  114. {
  115. $this->assertInternalType('array', $this->pool->getTemplates());
  116. $this->pool->setTemplates(array('ajax' => 'Foo.html.twig'));
  117. $this->assertNull($this->pool->getTemplate('bar'));
  118. $this->assertEquals('Foo.html.twig', $this->pool->getTemplate('ajax'));
  119. }
  120. /**
  121. * @return Symfony\Component\DependencyInjection\ContainerInterface - the mock of container interface
  122. */
  123. private function getContainer()
  124. {
  125. $containerMock = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  126. $containerMock->expects($this->any())
  127. ->method('get')
  128. ->will($this->returnValue('adminUserClass'));
  129. return $containerMock;
  130. }
  131. }