PoolTest.php 9.6 KB

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