Skip to content
This repository has been archived by the owner on Sep 16, 2021. It is now read-only.

Commit

Permalink
Merge pull request #148 from benglass/render_menu_absolute_path
Browse files Browse the repository at this point in the history
Use absolutizePath to support absolute and relative paths.
  • Loading branch information
lsmith77 committed Oct 9, 2013
2 parents ac988d3 + 8ecfcdc commit c007cda
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Provider/PhpcrMenuProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\HttpFoundation\Request;

use PHPCR\Util\PathHelper;
use Knp\Menu\FactoryInterface;
use Knp\Menu\NodeInterface;
use Knp\Menu\Provider\MenuProviderInterface;
Expand Down Expand Up @@ -124,7 +124,7 @@ public function get($name, array $options = array())
throw new \InvalidArgumentException('The menu name may not be empty');
}

$menu = $this->getObjectManager()->find(null, $this->menuRoot . '/' . $name);
$menu = $this->getObjectManager()->find(null, PathHelper::absolutizePath($name, $this->menuRoot));
if ($menu === null) {
throw new \InvalidArgumentException(sprintf('The menu "%s" is not defined.', $name));
}
Expand Down Expand Up @@ -152,7 +152,7 @@ public function get($name, array $options = array())
*/
public function has($name, array $options = array())
{
$menu = $this->getObjectManager()->find(null, $this->menuRoot . '/' . $name);
$menu = $this->getObjectManager()->find(null, PathHelper::absolutizePath($name, $this->menuRoot));

return $menu instanceof NodeInterface;
}
Expand Down
84 changes: 84 additions & 0 deletions Tests/Unit/Provider/PhpcrMenuProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

/*
* This file is part of the Symfony CMF package.
*
* (c) 2011-2013 Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/


namespace Symfony\Cmf\Bundle\MenuBundle\Tests\Unit\Provider;

use Symfony\Cmf\Bundle\MenuBundle\Provider\PhpcrMenuProvider;

class PhpcrMenuProviderTest extends \PHPUnit_Framework_Testcase
{
/**
* @dataProvider getMenuTests
*/
public function testGet($menuRoot, $name, $expectedPath)
{
$objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
$objectManager->expects($this->once())
->method('find')
->with($this->equalTo(null), $this->equalTo($expectedPath))
->will($this->returnValue($this->getMock('Knp\Menu\NodeInterface')));

$managerRegistry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
$managerRegistry->expects($this->once())
->method('getManager')
->will($this->returnValue($objectManager));

$factory = $this->getMock('Knp\Menu\FactoryInterface');
$factory->expects($this->once())
->method('createFromNode')
->will($this->returnValue($this->getMock('Knp\Menu\ItemInterface')));

$provider = new PhpcrMenuProvider(
$factory,
$managerRegistry,
$menuRoot
);

$provider->setRequest($this->getMock('Symfony\Component\HttpFoundation\Request'));

$provider->get($name);
}

/**
* @dataProvider getMenuTests
*/
public function testHas($menuRoot, $name, $expectedPath)
{
$objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
$objectManager->expects($this->once())
->method('find')
->with($this->equalTo(null), $this->equalTo($expectedPath))
->will($this->returnValue($this->getMock('Knp\Menu\NodeInterface')));

$managerRegistry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
$managerRegistry->expects($this->once())
->method('getManager')
->will($this->returnValue($objectManager));

$provider = new PhpcrMenuProvider(
$this->getMock('Knp\Menu\FactoryInterface'),
$managerRegistry,
$menuRoot
);

$provider->has($name);
}

public function getMenuTests()
{
return array(
array('/test/menu', 'foo', '/test/menu/foo'),
array('/test/menu', '/another/menu/path', '/another/menu/path'),
);
}

}

0 comments on commit c007cda

Please sign in to comment.