123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- /*
- * This file is part of the Sonata Project package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Sonata\AdminBundle\Tests\Menu;
- use Knp\Menu\MenuFactory;
- use Sonata\AdminBundle\Menu\MenuBuilder;
- class MenuBuilderTest extends \PHPUnit_Framework_TestCase
- {
- private $pool;
- private $provider;
- private $factory;
- private $eventDispatcher;
- /**
- * @var MenuBuilder
- */
- private $builder;
- protected function setUp()
- {
- $this->pool = $this->getMockBuilder('Sonata\AdminBundle\Admin\Pool')->disableOriginalConstructor()->getMock();
- $this->provider = $this->getMockForAbstractClass('Knp\Menu\Provider\MenuProviderInterface');
- $this->factory = new MenuFactory();
- $this->eventDispatcher = $this->getMockForAbstractClass('Symfony\Component\EventDispatcher\EventDispatcherInterface');
- $this->builder = new MenuBuilder($this->pool, $this->factory, $this->provider, $this->eventDispatcher);
- }
- public function testGetKnpMenuWithDefaultProvider()
- {
- $adminGroups = array(
- 'bar' => array(
- 'icon' => '<i class="fa fa-edit"></i>',
- 'label_catalogue' => '',
- 'roles' => array(),
- ),
- );
- $this->provider
- ->expects($this->once())
- ->method('get')
- ->with('sonata_group_menu')
- ->will($this->returnValue($this->factory->createItem('bar')->addChild('foo')->getParent()));
- $this->preparePool($adminGroups);
- $menu = $this->builder->createSidebarMenu();
- $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
- $this->assertArrayHasKey('bar', $menu->getChildren());
- foreach ($menu->getChildren() as $key => $child) {
- $this->assertInstanceOf('Knp\Menu\MenuItem', $child);
- $this->assertSame('bar', $child->getName());
- $this->assertSame('bar', $child->getLabel());
- // menu items
- $children = $child->getChildren();
- $this->assertCount(1, $children);
- $this->assertArrayHasKey('foo', $children);
- $this->assertInstanceOf('Knp\Menu\MenuItem', $child['foo']);
- $this->assertSame('foo', $child['foo']->getLabel());
- }
- }
- public function testGetKnpMenuWithSpecifiedProvider()
- {
- $adminGroups = array(
- 'bar' => array(
- 'provider' => 'my_menu',
- 'label_catalogue' => '',
- 'icon' => '<i class="fa fa-edit"></i>',
- 'roles' => array(),
- ),
- );
- $this->provider
- ->expects($this->once())
- ->method('get')
- ->with('my_menu')
- ->will($this->returnValue($this->factory->createItem('bar')->addChild('foo')->getParent()));
- $this->preparePool($adminGroups);
- $menu = $this->builder->createSidebarMenu();
- $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
- $this->assertArrayHasKey('bar', $menu->getChildren());
- foreach ($menu->getChildren() as $key => $child) {
- $this->assertInstanceOf('Knp\Menu\MenuItem', $child);
- $this->assertSame('bar', $child->getName());
- $this->assertSame('bar', $child->getLabel());
- // menu items
- $children = $child->getChildren();
- $this->assertCount(1, $children);
- $this->assertArrayHasKey('foo', $children);
- $this->assertInstanceOf('Knp\Menu\MenuItem', $child['foo']);
- $this->assertSame('foo', $child['foo']->getLabel());
- }
- }
- public function testGetKnpMenuAndDispatchEvent()
- {
- $adminGroups = array(
- 'bar' => array(
- 'label' => 'foo',
- 'icon' => '<i class="fa fa-edit"></i>',
- 'label_catalogue' => 'SonataAdminBundle',
- 'items' => array(),
- 'item_adds' => array(),
- 'roles' => array(),
- ),
- );
- $this->preparePool($adminGroups);
- $this->eventDispatcher
- ->expects($this->once())
- ->method('dispatch')
- ->with(
- $this->equalTo('sonata.admin.event.configure.menu.sidebar'),
- $this->isInstanceOf('Sonata\AdminBundle\Event\ConfigureMenuEvent')
- );
- $this->builder->createSidebarMenu();
- }
- private function preparePool($adminGroups, $admin = null)
- {
- $this->pool->expects($this->once())
- ->method('getAdminGroups')
- ->will($this->returnValue($adminGroups));
- if (null !== $admin) {
- $this->pool->expects($this->once())
- ->method('getInstance')
- ->with($this->equalTo('sonata_admin_foo_service'))
- ->will($this->returnValue($admin));
- }
- }
- }
|