PoolTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project 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', array('foo' => 'bar'));
  21. }
  22. public function testGetGroups()
  23. {
  24. $this->pool->setAdminServiceIds(array('sonata.user.admin.group1'));
  25. $this->pool->setAdminGroups(array(
  26. 'adminGroup1' => array('sonata.user.admin.group1' => array()),
  27. ));
  28. $expectedOutput = array(
  29. 'adminGroup1' => array(
  30. 'sonata.user.admin.group1' => 'adminUserClass',
  31. ),
  32. );
  33. $this->assertSame($expectedOutput, $this->pool->getGroups());
  34. }
  35. public function testHasGroup()
  36. {
  37. $this->pool->setAdminGroups(array(
  38. 'adminGroup1' => array(),
  39. ));
  40. $this->assertTrue($this->pool->hasGroup('adminGroup1'));
  41. $this->assertFalse($this->pool->hasGroup('adminGroup2'));
  42. }
  43. public function testGetDashboardGroups()
  44. {
  45. $admin_group1 = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  46. $admin_group1->expects($this->once())->method('showIn')->will($this->returnValue(true));
  47. $admin_group2 = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  48. $admin_group2->expects($this->once())->method('showIn')->will($this->returnValue(false));
  49. $admin_group3 = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  50. $admin_group3->expects($this->once())->method('showIn')->will($this->returnValue(false));
  51. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  52. $container->expects($this->any())->method('get')->will($this->onConsecutiveCalls(
  53. $admin_group1, $admin_group2, $admin_group3
  54. ));
  55. $pool = new Pool($container, 'Sonata Admin', '/path/to/pic.png');
  56. $pool->setAdminServiceIds(array('sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3'));
  57. $pool->setAdminGroups(array(
  58. 'adminGroup1' => array(
  59. 'items' => array('itemKey' => 'sonata.user.admin.group1'),
  60. ),
  61. 'adminGroup2' => array(
  62. 'items' => array('itemKey' => 'sonata.user.admin.group2'),
  63. ),
  64. 'adminGroup3' => array(
  65. 'items' => array('itemKey' => 'sonata.user.admin.group3'),
  66. ),
  67. ));
  68. $groups = $pool->getDashboardGroups();
  69. $this->assertCount(1, $groups);
  70. }
  71. /**
  72. * @expectedException \InvalidArgumentException
  73. */
  74. public function testGetAdminsByGroupWhenGroupNotSet()
  75. {
  76. $this->pool->setAdminGroups(array(
  77. 'adminGroup1' => array(),
  78. ));
  79. $this->pool->getAdminsByGroup('adminGroup2');
  80. }
  81. public function testGetAdminsByGroupWhenGroupIsEmpty()
  82. {
  83. $this->pool->setAdminGroups(array(
  84. 'adminGroup1' => array(),
  85. ));
  86. $this->assertSame(array(), $this->pool->getAdminsByGroup('adminGroup1'));
  87. }
  88. public function testGetAdminsByGroup()
  89. {
  90. $this->pool->setAdminServiceIds(array('sonata.admin1', 'sonata.admin2', 'sonata.admin3'));
  91. $this->pool->setAdminGroups(array(
  92. 'adminGroup1' => array('items' => array('sonata.admin1', 'sonata.admin2')),
  93. 'adminGroup2' => array('items' => array('sonata.admin3')),
  94. ));
  95. $this->assertCount(2, $this->pool->getAdminsByGroup('adminGroup1'));
  96. $this->assertCount(1, $this->pool->getAdminsByGroup('adminGroup2'));
  97. }
  98. public function testGetAdminForClassWhenAdminClassIsNotSet()
  99. {
  100. $this->pool->setAdminClasses(array('someclass' => 'sonata.user.admin.group1'));
  101. $this->assertFalse($this->pool->hasAdminByClass('notexists'));
  102. $this->assertNull($this->pool->getAdminByClass('notexists'));
  103. }
  104. /**
  105. * @expectedException \RuntimeException
  106. */
  107. public function testGetAdminForClassWithInvalidFormat()
  108. {
  109. $this->pool->setAdminClasses(array('someclass' => 'sonata.user.admin.group1'));
  110. $this->assertTrue($this->pool->hasAdminByClass('someclass'));
  111. $this->pool->getAdminByClass('someclass');
  112. }
  113. /**
  114. * @expectedException \RuntimeException
  115. */
  116. public function testGetAdminForClassWithTooManyRegisteredAdmin()
  117. {
  118. $this->pool->setAdminClasses(array('someclass' => array('sonata.user.admin.group1', 'sonata.user.admin.group2')));
  119. $this->assertTrue($this->pool->hasAdminByClass('someclass'));
  120. $this->assertSame('adminUserClass', $this->pool->getAdminByClass('someclass'));
  121. }
  122. public function testGetAdminForClassWhenAdminClassIsSet()
  123. {
  124. $this->pool->setAdminServiceIds(array('sonata.user.admin.group1'));
  125. $this->pool->setAdminClasses(array('someclass' => array('sonata.user.admin.group1')));
  126. $this->assertTrue($this->pool->hasAdminByClass('someclass'));
  127. $this->assertSame('adminUserClass', $this->pool->getAdminByClass('someclass'));
  128. }
  129. /**
  130. * @expectedException \InvalidArgumentException
  131. * @expectedExceptionMessage Admin service "sonata.news.admin.post" not found in admin pool.
  132. */
  133. public function testGetInstanceWithUndefinedServiceId()
  134. {
  135. $this->pool->getInstance('sonata.news.admin.post');
  136. }
  137. public function testGetAdminByAdminCode()
  138. {
  139. $this->pool->setAdminServiceIds(array('sonata.news.admin.post'));
  140. $this->assertSame('adminUserClass', $this->pool->getAdminByAdminCode('sonata.news.admin.post'));
  141. }
  142. public function testGetAdminByAdminCodeForChildClass()
  143. {
  144. $adminMock = $this->getMockBuilder('Sonata\AdminBundle\Admin\AdminInterface')
  145. ->disableOriginalConstructor()
  146. ->getMock();
  147. $adminMock->expects($this->any())
  148. ->method('hasChild')
  149. ->will($this->returnValue(true));
  150. $adminMock->expects($this->once())
  151. ->method('getChild')
  152. ->with($this->equalTo('sonata.news.admin.comment'))
  153. ->will($this->returnValue('commentAdminClass'));
  154. $containerMock = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  155. $containerMock->expects($this->any())
  156. ->method('get')
  157. ->will($this->returnValue($adminMock));
  158. $this->pool = new Pool($containerMock, 'Sonata', '/path/to/logo.png');
  159. $this->pool->setAdminServiceIds(array('sonata.news.admin.post'));
  160. $this->assertSame('commentAdminClass', $this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.comment'));
  161. }
  162. public function testGetAdminClasses()
  163. {
  164. $this->pool->setAdminClasses(array('someclass' => 'sonata.user.admin.group1'));
  165. $this->assertSame(array('someclass' => 'sonata.user.admin.group1'), $this->pool->getAdminClasses());
  166. }
  167. public function testGetAdminGroups()
  168. {
  169. $this->pool->setAdminGroups(array('adminGroup1' => 'sonata.user.admin.group1'));
  170. $this->assertSame(array('adminGroup1' => 'sonata.user.admin.group1'), $this->pool->getAdminGroups());
  171. }
  172. public function testGetAdminServiceIds()
  173. {
  174. $this->pool->setAdminServiceIds(array('sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3'));
  175. $this->assertSame(array('sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3'), $this->pool->getAdminServiceIds());
  176. }
  177. public function testGetContainer()
  178. {
  179. $this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerInterface', $this->pool->getContainer());
  180. }
  181. public function testTemplates()
  182. {
  183. $this->assertInternalType('array', $this->pool->getTemplates());
  184. $this->pool->setTemplates(array('ajax' => 'Foo.html.twig'));
  185. $this->assertNull($this->pool->getTemplate('bar'));
  186. $this->assertSame('Foo.html.twig', $this->pool->getTemplate('ajax'));
  187. }
  188. public function testGetTitleLogo()
  189. {
  190. $this->assertSame('/path/to/pic.png', $this->pool->getTitleLogo());
  191. }
  192. public function testGetTitle()
  193. {
  194. $this->assertSame('Sonata Admin', $this->pool->getTitle());
  195. }
  196. public function testGetOption()
  197. {
  198. $this->assertSame('bar', $this->pool->getOption('foo'));
  199. $this->assertSame(null, $this->pool->getOption('non_existent_option'));
  200. }
  201. public function testOptionDefault()
  202. {
  203. $this->assertSame(array(), $this->pool->getOption('nonexistantarray', array()));
  204. }
  205. /**
  206. * @return Symfony\Component\DependencyInjection\ContainerInterface - the mock of container interface
  207. */
  208. private function getContainer()
  209. {
  210. $containerMock = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  211. $containerMock->expects($this->any())
  212. ->method('get')
  213. ->will($this->returnValue('adminUserClass'));
  214. return $containerMock;
  215. }
  216. }