MenuBuilderTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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;
  11. use Knp\Menu\MenuFactory;
  12. use Sonata\AdminBundle\Menu\MenuBuilder;
  13. class MenuBuilderTest extends \PHPUnit_Framework_TestCase
  14. {
  15. private $pool;
  16. private $provider;
  17. private $factory;
  18. private $eventDispatcher;
  19. /**
  20. * @var MenuBuilder
  21. */
  22. private $builder;
  23. protected function setUp()
  24. {
  25. $this->pool = $this->getMockBuilder('Sonata\AdminBundle\Admin\Pool')->disableOriginalConstructor()->getMock();
  26. $this->provider = $this->getMockForAbstractClass('Knp\Menu\Provider\MenuProviderInterface');
  27. $this->factory = new MenuFactory();
  28. $this->eventDispatcher = $this->getMockForAbstractClass('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  29. $this->builder = new MenuBuilder($this->pool, $this->factory, $this->provider, $this->eventDispatcher);
  30. }
  31. public function testGetKnpMenuWithDefaultProvider()
  32. {
  33. $adminGroups = array(
  34. 'bar' => array(
  35. 'icon' => '<i class="fa fa-edit"></i>',
  36. 'label_catalogue' => '',
  37. 'roles' => array(),
  38. ),
  39. );
  40. $this->provider
  41. ->expects($this->once())
  42. ->method('get')
  43. ->with('sonata_group_menu')
  44. ->will($this->returnValue($this->factory->createItem('bar')->addChild('foo')->getParent()));
  45. $this->preparePool($adminGroups);
  46. $menu = $this->builder->createSidebarMenu();
  47. $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
  48. $this->assertArrayHasKey('bar', $menu->getChildren());
  49. foreach ($menu->getChildren() as $key => $child) {
  50. $this->assertInstanceOf('Knp\Menu\MenuItem', $child);
  51. $this->assertSame('bar', $child->getName());
  52. $this->assertSame('bar', $child->getLabel());
  53. // menu items
  54. $children = $child->getChildren();
  55. $this->assertCount(1, $children);
  56. $this->assertArrayHasKey('foo', $children);
  57. $this->assertInstanceOf('Knp\Menu\MenuItem', $child['foo']);
  58. $this->assertSame('foo', $child['foo']->getLabel());
  59. }
  60. }
  61. public function testGetKnpMenuWithSpecifiedProvider()
  62. {
  63. $adminGroups = array(
  64. 'bar' => array(
  65. 'provider' => 'my_menu',
  66. 'label_catalogue' => '',
  67. 'icon' => '<i class="fa fa-edit"></i>',
  68. 'roles' => array(),
  69. ),
  70. );
  71. $this->provider
  72. ->expects($this->once())
  73. ->method('get')
  74. ->with('my_menu')
  75. ->will($this->returnValue($this->factory->createItem('bar')->addChild('foo')->getParent()));
  76. $this->preparePool($adminGroups);
  77. $menu = $this->builder->createSidebarMenu();
  78. $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
  79. $this->assertArrayHasKey('bar', $menu->getChildren());
  80. foreach ($menu->getChildren() as $key => $child) {
  81. $this->assertInstanceOf('Knp\Menu\MenuItem', $child);
  82. $this->assertSame('bar', $child->getName());
  83. $this->assertSame('bar', $child->getLabel());
  84. // menu items
  85. $children = $child->getChildren();
  86. $this->assertCount(1, $children);
  87. $this->assertArrayHasKey('foo', $children);
  88. $this->assertInstanceOf('Knp\Menu\MenuItem', $child['foo']);
  89. $this->assertSame('foo', $child['foo']->getLabel());
  90. }
  91. }
  92. public function testGetKnpMenuAndDispatchEvent()
  93. {
  94. $adminGroups = array(
  95. 'bar' => array(
  96. 'label' => 'foo',
  97. 'icon' => '<i class="fa fa-edit"></i>',
  98. 'label_catalogue' => 'SonataAdminBundle',
  99. 'items' => array(),
  100. 'item_adds' => array(),
  101. 'roles' => array(),
  102. ),
  103. );
  104. $this->preparePool($adminGroups);
  105. $this->eventDispatcher
  106. ->expects($this->once())
  107. ->method('dispatch')
  108. ->with(
  109. $this->equalTo('sonata.admin.event.configure.menu.sidebar'),
  110. $this->isInstanceOf('Sonata\AdminBundle\Event\ConfigureMenuEvent')
  111. );
  112. $this->builder->createSidebarMenu();
  113. }
  114. private function preparePool($adminGroups, $admin = null)
  115. {
  116. $this->pool->expects($this->once())
  117. ->method('getAdminGroups')
  118. ->will($this->returnValue($adminGroups));
  119. if (null !== $admin) {
  120. $this->pool->expects($this->once())
  121. ->method('getInstance')
  122. ->with($this->equalTo('sonata_admin_foo_service'))
  123. ->will($this->returnValue($admin));
  124. }
  125. }
  126. }