PoolTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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' => 'sonata_user_admin_group1_AdminClass',
  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' => $this->getItemArray('sonata.user.admin.group1')),
  60. ),
  61. 'adminGroup2' => array(
  62. 'items' => array('itemKey' => $this->getItemArray('sonata.user.admin.group2')),
  63. ),
  64. 'adminGroup3' => array(
  65. 'items' => array('itemKey' => $this->getItemArray('sonata.user.admin.group3')),
  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(
  94. 'items' => array(
  95. $this->getItemArray('sonata.admin1'),
  96. $this->getItemArray('sonata.admin2'),
  97. ),
  98. ),
  99. 'adminGroup2' => array(
  100. 'items' => array($this->getItemArray('sonata.admin3')),
  101. ),
  102. ));
  103. $this->assertEquals(array(
  104. 'sonata_admin1_AdminClass',
  105. 'sonata_admin2_AdminClass',
  106. ), $this->pool->getAdminsByGroup('adminGroup1'));
  107. $this->assertEquals(array('sonata_admin3_AdminClass'), $this->pool->getAdminsByGroup('adminGroup2'));
  108. }
  109. public function testGetAdminForClassWhenAdminClassIsNotSet()
  110. {
  111. $this->pool->setAdminClasses(array('someclass' => 'sonata.user.admin.group1'));
  112. $this->assertFalse($this->pool->hasAdminByClass('notexists'));
  113. $this->assertNull($this->pool->getAdminByClass('notexists'));
  114. }
  115. /**
  116. * @expectedException \RuntimeException
  117. */
  118. public function testGetAdminForClassWithInvalidFormat()
  119. {
  120. $this->pool->setAdminClasses(array('someclass' => 'sonata.user.admin.group1'));
  121. $this->assertTrue($this->pool->hasAdminByClass('someclass'));
  122. $this->pool->getAdminByClass('someclass');
  123. }
  124. /**
  125. * @expectedException \RuntimeException
  126. */
  127. public function testGetAdminForClassWithTooManyRegisteredAdmin()
  128. {
  129. $this->pool->setAdminClasses(array(
  130. 'someclass' => array('sonata.user.admin.group1', 'sonata.user.admin.group2'),
  131. ));
  132. $this->assertTrue($this->pool->hasAdminByClass('someclass'));
  133. $this->pool->getAdminByClass('someclass');
  134. }
  135. public function testGetAdminForClassWhenAdminClassIsSet()
  136. {
  137. $this->pool->setAdminServiceIds(array('sonata.user.admin.group1'));
  138. $this->pool->setAdminClasses(array(
  139. 'someclass' => array('sonata.user.admin.group1'),
  140. ));
  141. $this->assertTrue($this->pool->hasAdminByClass('someclass'));
  142. $this->assertSame('sonata_user_admin_group1_AdminClass', $this->pool->getAdminByClass('someclass'));
  143. }
  144. /**
  145. * @expectedException \InvalidArgumentException
  146. * @expectedExceptionMessage Admin service "sonata.news.admin.post" not found in admin pool.
  147. */
  148. public function testGetInstanceWithUndefinedServiceId()
  149. {
  150. $this->pool->getInstance('sonata.news.admin.post');
  151. }
  152. public function testGetAdminByAdminCode()
  153. {
  154. $this->pool->setAdminServiceIds(array('sonata.news.admin.post'));
  155. $this->assertSame('sonata_news_admin_post_AdminClass', $this->pool->getAdminByAdminCode('sonata.news.admin.post'));
  156. }
  157. public function testGetAdminByAdminCodeForChildClass()
  158. {
  159. $adminMock = $this->getMockBuilder('Sonata\AdminBundle\Admin\AdminInterface')
  160. ->disableOriginalConstructor()
  161. ->getMock();
  162. $adminMock->expects($this->any())
  163. ->method('hasChild')
  164. ->will($this->returnValue(true));
  165. $adminMock->expects($this->once())
  166. ->method('getChild')
  167. ->with($this->equalTo('sonata.news.admin.comment'))
  168. ->will($this->returnValue('commentAdminClass'));
  169. $containerMock = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  170. $containerMock->expects($this->any())
  171. ->method('get')
  172. ->will($this->returnValue($adminMock));
  173. $this->pool = new Pool($containerMock, 'Sonata', '/path/to/logo.png');
  174. $this->pool->setAdminServiceIds(array('sonata.news.admin.post'));
  175. $this->assertSame('commentAdminClass', $this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.comment'));
  176. }
  177. public function testGetAdminClasses()
  178. {
  179. $this->pool->setAdminClasses(array('someclass' => 'sonata.user.admin.group1'));
  180. $this->assertSame(array('someclass' => 'sonata.user.admin.group1'), $this->pool->getAdminClasses());
  181. }
  182. public function testGetAdminGroups()
  183. {
  184. $this->pool->setAdminGroups(array('adminGroup1' => 'sonata.user.admin.group1'));
  185. $this->assertSame(array('adminGroup1' => 'sonata.user.admin.group1'), $this->pool->getAdminGroups());
  186. }
  187. public function testGetAdminServiceIds()
  188. {
  189. $this->pool->setAdminServiceIds(array('sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3'));
  190. $this->assertSame(array('sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3'), $this->pool->getAdminServiceIds());
  191. }
  192. public function testGetContainer()
  193. {
  194. $this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerInterface', $this->pool->getContainer());
  195. }
  196. public function testTemplates()
  197. {
  198. $this->assertInternalType('array', $this->pool->getTemplates());
  199. $this->pool->setTemplates(array('ajax' => 'Foo.html.twig'));
  200. $this->assertNull($this->pool->getTemplate('bar'));
  201. $this->assertSame('Foo.html.twig', $this->pool->getTemplate('ajax'));
  202. }
  203. public function testGetTitleLogo()
  204. {
  205. $this->assertSame('/path/to/pic.png', $this->pool->getTitleLogo());
  206. }
  207. public function testGetTitle()
  208. {
  209. $this->assertSame('Sonata Admin', $this->pool->getTitle());
  210. }
  211. public function testGetOption()
  212. {
  213. $this->assertSame('bar', $this->pool->getOption('foo'));
  214. $this->assertSame(null, $this->pool->getOption('non_existent_option'));
  215. }
  216. public function testOptionDefault()
  217. {
  218. $this->assertSame(array(), $this->pool->getOption('nonexistantarray', array()));
  219. }
  220. /**
  221. * @return Symfony\Component\DependencyInjection\ContainerInterface - the mock of container interface
  222. */
  223. private function getContainer()
  224. {
  225. $containerMock = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  226. $containerMock->expects($this->any())
  227. ->method('get')
  228. ->will($this->returnCallback(function ($serviceId) {
  229. return str_replace('.', '_', $serviceId).'_AdminClass';
  230. }));
  231. return $containerMock;
  232. }
  233. private function getItemArray($serviceId)
  234. {
  235. return array(
  236. 'admin' => $serviceId,
  237. 'label' => '',
  238. 'route' => '',
  239. 'route_params' => array(),
  240. );
  241. }
  242. }