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

Commit 44818aa

Browse files
committed
Fix implementation of allowEmptyItems
1 parent cafca4e commit 44818aa

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

QuietFactory.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Symfony\Cmf\Bundle\MenuBundle;
44

5-
use Knp\Menu\Factory\ExtensionInterface;
65
use Knp\Menu\FactoryInterface;
7-
use Knp\Menu\ItemInterface;
86
use Psr\Log\LoggerInterface;
97
use Symfony\Component\Routing\Exception\RouteNotFoundException;
108

@@ -27,8 +25,9 @@ class QuietFactory implements FactoryInterface
2725
private $logger;
2826

2927
/**
30-
* Whether to return null or a MenuItem without any URL if no URL can be
31-
* found for a MenuNode.
28+
* Whether to return null (if value is false) or a MenuItem
29+
* without any URL (if value is true) if no URL can be found
30+
* for a MenuNode.
3231
*
3332
* @var bool
3433
*/
@@ -54,6 +53,12 @@ public function createItem($name, array $options = array())
5453
if (!$this->allowEmptyItems) {
5554
return null;
5655
}
56+
57+
// remove route and content options
58+
unset($options['route']);
59+
unset($options['content']);
60+
61+
return $this->innerFactory->createItem($name, $options);
5762
}
5863
}
5964
}

Tests/Unit/QuietFactoryTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\MenuBundle\Tests\Unit;
4+
5+
use Symfony\Cmf\Bundle\MenuBundle\QuietFactory;
6+
7+
class QuietFactoryTest extends \PHPUnit_Framework_TestCase
8+
{
9+
private $innerFactory;
10+
private $logger;
11+
12+
protected function setUp()
13+
{
14+
$this->innerFactory = $this->prophesize('Knp\Menu\FactoryInterface');
15+
$this->logger = $this->prophesize('Psr\Log\LoggerInterface');
16+
}
17+
18+
public function provideItemsWithNotExistingLinks()
19+
{
20+
return array(
21+
array(array('route' => 'not_existent')),
22+
array(array('content' => 'not_existent')),
23+
array(array('linkType' => 'route', 'route' => 'not_existent')),
24+
);
25+
}
26+
27+
/** @dataProvider provideItemsWithNotExistingLinks */
28+
public function testAllowEmptyItemsReturnsItemWithoutURL(array $options)
29+
{
30+
$this->innerFactory->createItem('Home', $options)
31+
->willThrow('Symfony\Component\Routing\Exception\RouteNotFoundException');
32+
33+
$homeMenuItem = new \stdClass();
34+
$this->innerFactory->createItem('Home', array())->willReturn($homeMenuItem);
35+
36+
$factory = new QuietFactory($this->innerFactory->reveal(), $this->logger->reveal(), true);
37+
38+
$this->assertEquals($homeMenuItem, $factory->createItem('Home', $options));
39+
}
40+
41+
public function testDisallowEmptyItemsReturnsNull()
42+
{
43+
$this->innerFactory->createItem('Home', array('route' => 'not_existent'))
44+
->willThrow('Symfony\Component\Routing\Exception\RouteNotFoundException');
45+
46+
$factory = new QuietFactory($this->innerFactory->reveal(), $this->logger->reveal(), false);
47+
48+
$this->assertEquals(null, $factory->createItem('Home', array('route' => 'not_existent')));
49+
}
50+
}

0 commit comments

Comments
 (0)