GroupMenuProviderTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. /**
  39. * @param bool $hasRoute
  40. * @param bool $isGranted
  41. *
  42. * @return MockObject|AdminInterface
  43. */
  44. private function getAdminMock($hasRoute = true, $isGranted = true)
  45. {
  46. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  47. $admin->expects($this->once())
  48. ->method('hasRoute')
  49. ->with($this->equalTo('list'))
  50. ->will($this->returnValue($hasRoute));
  51. $admin->expects($this->any())
  52. ->method('isGranted')
  53. ->with($this->equalTo('LIST'))
  54. ->will($this->returnValue($isGranted));
  55. $admin->expects($this->any())
  56. ->method('getLabel')
  57. ->will($this->returnValue('foo_admin_label'));
  58. $admin->expects($this->any())
  59. ->method('generateMenuUrl')
  60. ->will($this->returnValue(array()));
  61. return $admin;
  62. }
  63. public function testGroupMenuProviderName()
  64. {
  65. $this->assertTrue($this->provider->has('sonata_group_menu'));
  66. }
  67. /**
  68. * @param array $adminGroups
  69. *
  70. * @dataProvider getAdminGroups
  71. */
  72. public function testGetMenuProviderWithAdmin(array $adminGroups)
  73. {
  74. $this->pool->expects($this->once())
  75. ->method('getInstance')
  76. ->with($this->equalTo('sonata_admin_foo_service'))
  77. ->will($this->returnValue($this->getAdminMock()));
  78. $menu = $this->provider->get(
  79. 'providerFoo',
  80. array(
  81. 'name' => 'foo',
  82. 'group' => $adminGroups,
  83. )
  84. );
  85. $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
  86. $this->assertSame('foo', $menu->getName());
  87. $children = $menu->getChildren();
  88. $this->assertCount(2, $children);
  89. $this->assertArrayHasKey('foo_admin_label', $children);
  90. $this->assertArrayHasKey('route_label', $children);
  91. $this->assertInstanceOf('Knp\Menu\MenuItem', $menu['foo_admin_label']);
  92. $this->assertSame('foo_admin_label', $menu['foo_admin_label']->getLabel());
  93. }
  94. /**
  95. * @param array $adminGroups
  96. *
  97. * @dataProvider getAdminGroups
  98. */
  99. public function testGetKnpMenuWithListRoute(array $adminGroups)
  100. {
  101. $this->pool->expects($this->once())
  102. ->method('getInstance')
  103. ->with($this->equalTo('sonata_admin_foo_service'))
  104. ->will($this->returnValue($this->getAdminMock(false)));
  105. $menu = $this->provider->get(
  106. 'providerFoo',
  107. array(
  108. 'name' => 'foo',
  109. 'group' => $adminGroups,
  110. )
  111. );
  112. $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
  113. $this->assertArrayNotHasKey('foo_admin_label', $menu->getChildren());
  114. $this->assertArrayHasKey('route_label', $menu->getChildren());
  115. $this->assertCount(1, $menu->getChildren());
  116. }
  117. /**
  118. * @param array $adminGroups
  119. *
  120. * @dataProvider getAdminGroups
  121. */
  122. public function testGetKnpMenuWithGrantedList(array $adminGroups)
  123. {
  124. $this->pool->expects($this->once())
  125. ->method('getInstance')
  126. ->with($this->equalTo('sonata_admin_foo_service'))
  127. ->will($this->returnValue($this->getAdminMock(true, false)));
  128. $menu = $this->provider->get(
  129. 'providerFoo',
  130. array(
  131. 'name' => 'foo',
  132. 'group' => $adminGroups,
  133. )
  134. );
  135. $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
  136. $this->assertArrayNotHasKey('foo_admin_label', $menu->getChildren());
  137. $this->assertArrayHasKey('route_label', $menu->getChildren());
  138. $this->assertCount(1, $menu->getChildren());
  139. }
  140. /**
  141. * @return array
  142. */
  143. public function getAdminGroups()
  144. {
  145. return array(
  146. array(
  147. 'bar' => array(
  148. 'label' => 'foo',
  149. 'icon' => '<i class="fa fa-edit"></i>',
  150. 'label_catalogue' => 'SonataAdminBundle',
  151. 'items' => array(
  152. array(
  153. 'admin' => 'sonata_admin_foo_service',
  154. 'label' => 'fooLabel',
  155. 'route_absolute' => true,
  156. ),
  157. array(
  158. 'admin' => '',
  159. 'label' => 'route_label',
  160. 'route' => 'FooRoute',
  161. 'route_params' => array('foo' => 'bar'),
  162. 'route_absolute' => true,
  163. ),
  164. ),
  165. 'item_adds' => array(),
  166. 'roles' => array(),
  167. ),
  168. ),
  169. );
  170. }
  171. }