Skip to content

Commit

Permalink
Merge f4f8c8c into 93b027d
Browse files Browse the repository at this point in the history
  • Loading branch information
deZinc committed Feb 12, 2019
2 parents 93b027d + f4f8c8c commit e57cec3
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 1 deletion.
Expand Up @@ -54,7 +54,7 @@ public function adaptChildren(MenuBuilder $menu, array &$children, MenuItem $par
->setUniqueId($item['route'])
->setParent($parent);

if ($request && stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
if ($request && null !== $parent && stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
$menuItem->setActive(true);
$parent->setActive(true);
}
Expand Down
@@ -0,0 +1,104 @@
<?php

namespace Kunstmaan\AdminBundle\Tests\Helper\Menu;

use Kunstmaan\AdminBundle\Helper\Menu\MenuAdaptorInterface;
use Kunstmaan\AdminBundle\Helper\Menu\MenuBuilder;
use Kunstmaan\AdminBundle\Helper\Menu\SimpleMenuAdaptor;
use Kunstmaan\AdminBundle\Helper\Menu\TopMenuItem;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

class SimpleMenuAdaptorTest extends TestCase
{
/** @var AuthorizationCheckerInterface (mock) */
private $authorizationCheckerInterface;

/** @var array */
private $menuItems;

public function setUp()
{
$this->authorizationCheckerInterface = $this->createMock(AuthorizationCheckerInterface::class);
$this->menuItems = [];
}

/**
* @return \PHPUnit\Framework\MockObject\MockObject|SimpleMenuAdaptor
*/
public function setUpSimpleMenuAdaptorMock()
{
$simpleMenuAdaptorMock = $this->getMockBuilder(SimpleMenuAdaptor::class)
->setConstructorArgs([$this->authorizationCheckerInterface, $this->menuItems])
->getMock()
;

return $simpleMenuAdaptorMock;
}

/**
* @dataProvider provider
*
* @param TopMenuItem|null $parent
*/
public function testAdaptChildren(?TopMenuItem $parent, ?string $itemParent)
{
$this->menuItems[] = ['parent' => 'not_test_route'];

$this->menuItems[] = [
'role' => 'some_role',
'parent' => $itemParent,
];

$this->menuItems[] = [
'role' => 'some_role',
'parent' => $itemParent,
'route' => 'KunstmaanAdminBundle_menu_item',
'params' => [],
'label' => 'menu_item',
];

$children = [];

/** @var MenuBuilder $menuBuilderMock */
$menuBuilderMock = $this->createMock(MenuBuilder::class);

/** @var Request $request */
$request = new Request([], [], ['_route' => 'KunstmaanAdminBundle_menu_item']);

$this->authorizationCheckerInterface
->expects($this->exactly(2))
->method('isGranted')
->will($this->onConsecutiveCalls(false, true))
;
$simpleMenuAdaptorMock = new SimpleMenuAdaptor($this->authorizationCheckerInterface, $this->menuItems);
$simpleMenuAdaptorMock->adaptChildren($menuBuilderMock, $children, $parent, $request);

$this->assertCount(1, $children);
$this->assertContainsOnlyInstancesOf(TopMenuItem::class, $children);
$this->assertEquals('menu_item', $children[0]->getLabel());
}

public function testHasInterface()
{
$simpleMenuAdaptorMock = $this->setUpSimpleMenuAdaptorMock();
$this->assertInstanceOf(MenuAdaptorInterface::class, $simpleMenuAdaptorMock);
}

public function provider()
{
/** @var TopMenuItem $parent */
$parent = $this->createMock(TopMenuItem::class);
$parent
->expects($this->any())
->method('getRoute')
->willReturn('test_route')
;

return [
'with no parent' => [null, null],
'with parent' => [$parent, 'test_route'],
];
}
}

0 comments on commit e57cec3

Please sign in to comment.