GroupMenuProviderTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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\Menu\Provider;
  11. use Knp\Menu\FactoryInterface;
  12. use Knp\Menu\MenuFactory;
  13. use Knp\Menu\Provider\MenuProviderInterface;
  14. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  15. use Sonata\AdminBundle\Admin\AdminInterface;
  16. use Sonata\AdminBundle\Admin\Pool;
  17. use Sonata\AdminBundle\Menu\Provider\GroupMenuProvider;
  18. class GroupMenuProviderTest extends \PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * @var MockObject|Pool
  22. */
  23. private $pool;
  24. /**
  25. * @var MockObject|MenuProviderInterface
  26. */
  27. private $provider;
  28. /**
  29. * @var MockObject|FactoryInterface
  30. */
  31. private $factory;
  32. protected function setUp()
  33. {
  34. $this->pool = $this->getMockBuilder('Sonata\AdminBundle\Admin\Pool')->disableOriginalConstructor()->getMock();
  35. $this->factory = new MenuFactory();
  36. $this->provider = new GroupMenuProvider($this->factory, $this->pool);
  37. }
  38. public function testGroupMenuProviderName()
  39. {
  40. $this->assertTrue($this->provider->has('sonata_group_menu'));
  41. }
  42. /**
  43. * @param array $adminGroups
  44. *
  45. * @dataProvider getAdminGroups
  46. */
  47. public function testGetMenuProviderWithAdmin(array $adminGroups)
  48. {
  49. $this->pool->expects($this->once())
  50. ->method('getInstance')
  51. ->with($this->equalTo('sonata_admin_foo_service'))
  52. ->will($this->returnValue($this->getAdminMock()));
  53. $menu = $this->provider->get(
  54. 'providerFoo',
  55. array(
  56. 'name' => 'foo',
  57. 'group' => $adminGroups,
  58. )
  59. );
  60. $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
  61. $this->assertSame('foo', $menu->getName());
  62. $children = $menu->getChildren();
  63. $this->assertCount(2, $children);
  64. $this->assertArrayHasKey('foo_admin_label', $children);
  65. $this->assertArrayHasKey('route_label', $children);
  66. $this->assertInstanceOf('Knp\Menu\MenuItem', $menu['foo_admin_label']);
  67. $this->assertSame('foo_admin_label', $menu['foo_admin_label']->getLabel());
  68. }
  69. /**
  70. * @param array $adminGroups
  71. *
  72. * @dataProvider getAdminGroups
  73. */
  74. public function testGetKnpMenuWithListRoute(array $adminGroups)
  75. {
  76. $this->pool->expects($this->once())
  77. ->method('getInstance')
  78. ->with($this->equalTo('sonata_admin_foo_service'))
  79. ->will($this->returnValue($this->getAdminMock(false)));
  80. $menu = $this->provider->get(
  81. 'providerFoo',
  82. array(
  83. 'name' => 'foo',
  84. 'group' => $adminGroups,
  85. )
  86. );
  87. $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
  88. $this->assertArrayNotHasKey('foo_admin_label', $menu->getChildren());
  89. $this->assertArrayHasKey('route_label', $menu->getChildren());
  90. $this->assertCount(1, $menu->getChildren());
  91. }
  92. /**
  93. * @param array $adminGroups
  94. *
  95. * @dataProvider getAdminGroups
  96. */
  97. public function testGetKnpMenuWithGrantedList(array $adminGroups)
  98. {
  99. $this->pool->expects($this->once())
  100. ->method('getInstance')
  101. ->with($this->equalTo('sonata_admin_foo_service'))
  102. ->will($this->returnValue($this->getAdminMock(true, false)));
  103. $menu = $this->provider->get(
  104. 'providerFoo',
  105. array(
  106. 'name' => 'foo',
  107. 'group' => $adminGroups,
  108. )
  109. );
  110. $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
  111. $this->assertArrayNotHasKey('foo_admin_label', $menu->getChildren());
  112. $this->assertArrayHasKey('route_label', $menu->getChildren());
  113. $this->assertCount(1, $menu->getChildren());
  114. }
  115. /**
  116. * @param array $adminGroupsOnTopOption
  117. *
  118. * @dataProvider getAdminGroupsWithOnTopOption
  119. */
  120. public function testGetMenuProviderOnTopOptions(array $adminGroupsOnTopOption)
  121. {
  122. $this->pool->expects($this->once())
  123. ->method('getInstance')
  124. ->with($this->equalTo('sonata_admin_foo_service'))
  125. ->will($this->returnValue($this->getAdminMock(true, false)));
  126. $menu = $this->provider->get(
  127. 'providerFoo',
  128. array(
  129. 'name' => 'foo',
  130. 'group' => $adminGroupsOnTopOption,
  131. )
  132. );
  133. $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
  134. $this->assertCount(0, $menu->getChildren());
  135. }
  136. /**
  137. * @return array
  138. */
  139. public function getAdminGroups()
  140. {
  141. return array(
  142. array(
  143. 'bar' => array(
  144. 'label' => 'foo',
  145. 'icon' => '<i class="fa fa-edit"></i>',
  146. 'label_catalogue' => 'SonataAdminBundle',
  147. 'items' => array(
  148. array(
  149. 'admin' => 'sonata_admin_foo_service',
  150. 'label' => 'fooLabel',
  151. 'route_absolute' => true,
  152. ),
  153. array(
  154. 'admin' => '',
  155. 'label' => 'route_label',
  156. 'route' => 'FooRoute',
  157. 'route_params' => array('foo' => 'bar'),
  158. 'route_absolute' => true,
  159. ),
  160. ),
  161. 'item_adds' => array(),
  162. 'roles' => array(),
  163. ),
  164. ),
  165. );
  166. }
  167. /**
  168. * @return array
  169. */
  170. public function getAdminGroupsWithOnTopOption()
  171. {
  172. return array(
  173. array(
  174. 'foo' => array(
  175. 'label' => 'foo_on_top',
  176. 'icon' => '<i class="fa fa-edit"></i>',
  177. 'label_catalogue' => 'SonataAdminBundle',
  178. 'on_top' => true,
  179. 'items' => array(
  180. array(
  181. 'admin' => 'sonata_admin_foo_service',
  182. 'label' => 'fooLabel',
  183. 'route_absolute' => true,
  184. 'route_params' => array(),
  185. ),
  186. ),
  187. 'item_adds' => array(),
  188. 'roles' => array(),
  189. ),
  190. ),
  191. );
  192. }
  193. /**
  194. * @param bool $hasRoute
  195. * @param bool $isGranted
  196. *
  197. * @return MockObject|AdminInterface
  198. */
  199. private function getAdminMock($hasRoute = true, $isGranted = true)
  200. {
  201. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  202. $admin->expects($this->once())
  203. ->method('hasRoute')
  204. ->with($this->equalTo('list'))
  205. ->will($this->returnValue($hasRoute));
  206. $admin->expects($this->any())
  207. ->method('isGranted')
  208. ->with($this->equalTo('LIST'))
  209. ->will($this->returnValue($isGranted));
  210. $admin->expects($this->any())
  211. ->method('getLabel')
  212. ->will($this->returnValue('foo_admin_label'));
  213. $admin->expects($this->any())
  214. ->method('generateMenuUrl')
  215. ->will($this->returnValue(array()));
  216. return $admin;
  217. }
  218. }