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

Commit c007cda

Browse files
committed
Merge pull request #148 from benglass/render_menu_absolute_path
Use absolutizePath to support absolute and relative paths.
2 parents ac988d3 + 8ecfcdc commit c007cda

File tree

2 files changed

+87
-3
lines changed

2 files changed

+87
-3
lines changed

Provider/PhpcrMenuProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Doctrine\Common\Persistence\ManagerRegistry;
1616
use Doctrine\Common\Persistence\ObjectManager;
1717
use Symfony\Component\HttpFoundation\Request;
18-
18+
use PHPCR\Util\PathHelper;
1919
use Knp\Menu\FactoryInterface;
2020
use Knp\Menu\NodeInterface;
2121
use Knp\Menu\Provider\MenuProviderInterface;
@@ -124,7 +124,7 @@ public function get($name, array $options = array())
124124
throw new \InvalidArgumentException('The menu name may not be empty');
125125
}
126126

127-
$menu = $this->getObjectManager()->find(null, $this->menuRoot . '/' . $name);
127+
$menu = $this->getObjectManager()->find(null, PathHelper::absolutizePath($name, $this->menuRoot));
128128
if ($menu === null) {
129129
throw new \InvalidArgumentException(sprintf('The menu "%s" is not defined.', $name));
130130
}
@@ -152,7 +152,7 @@ public function get($name, array $options = array())
152152
*/
153153
public function has($name, array $options = array())
154154
{
155-
$menu = $this->getObjectManager()->find(null, $this->menuRoot . '/' . $name);
155+
$menu = $this->getObjectManager()->find(null, PathHelper::absolutizePath($name, $this->menuRoot));
156156

157157
return $menu instanceof NodeInterface;
158158
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2013 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
13+
namespace Symfony\Cmf\Bundle\MenuBundle\Tests\Unit\Provider;
14+
15+
use Symfony\Cmf\Bundle\MenuBundle\Provider\PhpcrMenuProvider;
16+
17+
class PhpcrMenuProviderTest extends \PHPUnit_Framework_Testcase
18+
{
19+
/**
20+
* @dataProvider getMenuTests
21+
*/
22+
public function testGet($menuRoot, $name, $expectedPath)
23+
{
24+
$objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
25+
$objectManager->expects($this->once())
26+
->method('find')
27+
->with($this->equalTo(null), $this->equalTo($expectedPath))
28+
->will($this->returnValue($this->getMock('Knp\Menu\NodeInterface')));
29+
30+
$managerRegistry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
31+
$managerRegistry->expects($this->once())
32+
->method('getManager')
33+
->will($this->returnValue($objectManager));
34+
35+
$factory = $this->getMock('Knp\Menu\FactoryInterface');
36+
$factory->expects($this->once())
37+
->method('createFromNode')
38+
->will($this->returnValue($this->getMock('Knp\Menu\ItemInterface')));
39+
40+
$provider = new PhpcrMenuProvider(
41+
$factory,
42+
$managerRegistry,
43+
$menuRoot
44+
);
45+
46+
$provider->setRequest($this->getMock('Symfony\Component\HttpFoundation\Request'));
47+
48+
$provider->get($name);
49+
}
50+
51+
/**
52+
* @dataProvider getMenuTests
53+
*/
54+
public function testHas($menuRoot, $name, $expectedPath)
55+
{
56+
$objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
57+
$objectManager->expects($this->once())
58+
->method('find')
59+
->with($this->equalTo(null), $this->equalTo($expectedPath))
60+
->will($this->returnValue($this->getMock('Knp\Menu\NodeInterface')));
61+
62+
$managerRegistry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
63+
$managerRegistry->expects($this->once())
64+
->method('getManager')
65+
->will($this->returnValue($objectManager));
66+
67+
$provider = new PhpcrMenuProvider(
68+
$this->getMock('Knp\Menu\FactoryInterface'),
69+
$managerRegistry,
70+
$menuRoot
71+
);
72+
73+
$provider->has($name);
74+
}
75+
76+
public function getMenuTests()
77+
{
78+
return array(
79+
array('/test/menu', 'foo', '/test/menu/foo'),
80+
array('/test/menu', '/another/menu/path', '/another/menu/path'),
81+
);
82+
}
83+
84+
}

0 commit comments

Comments
 (0)