diff --git a/Provider/PhpcrMenuProvider.php b/Provider/PhpcrMenuProvider.php index 3e435768..bd6d7b3b 100644 --- a/Provider/PhpcrMenuProvider.php +++ b/Provider/PhpcrMenuProvider.php @@ -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; @@ -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)); } @@ -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; } diff --git a/Tests/Unit/Provider/PhpcrMenuProviderTest.php b/Tests/Unit/Provider/PhpcrMenuProviderTest.php new file mode 100644 index 00000000..83338e59 --- /dev/null +++ b/Tests/Unit/Provider/PhpcrMenuProviderTest.php @@ -0,0 +1,84 @@ +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'), + ); + } + +}