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

Commit 94c9460

Browse files
committed
Merge pull request #232 from symfony-cmf/issue_230
Fix implementation of allowEmptyItems in QuietFactory
2 parents cafca4e + a4d2346 commit 94c9460

File tree

4 files changed

+69
-9
lines changed

4 files changed

+69
-9
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ before_install:
3737
- if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then echo "memory_limit = -1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini; fi
3838
- composer self-update
3939
- composer require symfony/symfony:${SYMFONY_VERSION} --no-update
40-
40+
4141
install: composer update $COMPOSER_FLAGS --prefer-dist
4242

4343
script: phpunit --coverage-text

QuietFactory.php

Lines changed: 17 additions & 7 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

@@ -22,19 +20,20 @@ class QuietFactory implements FactoryInterface
2220
private $innerFactory;
2321

2422
/**
25-
* @var LoggerInterface
23+
* @var LoggerInterface|null
2624
*/
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
*/
3534
private $allowEmptyItems;
3635

37-
public function __construct(FactoryInterface $innerFactory, LoggerInterface $logger, $allowEmptyItems = false)
36+
public function __construct(FactoryInterface $innerFactory, LoggerInterface $logger = null, $allowEmptyItems = false)
3837
{
3938
$this->innerFactory = $innerFactory;
4039
$this->logger = $logger;
@@ -49,11 +48,22 @@ public function createItem($name, array $options = array())
4948
try {
5049
return $this->innerFactory->createItem($name, $options);
5150
} catch (RouteNotFoundException $e) {
52-
$this->logger->error(sprintf('%s : %s', $name, $e->getMessage()));
51+
if (null !== $this->logger) {
52+
$this->logger->error(
53+
sprintf('An exception was thrown while creating a menu item called "%s"', $name),
54+
array('exception' => $e)
55+
);
56+
}
5357

5458
if (!$this->allowEmptyItems) {
5559
return null;
5660
}
61+
62+
// remove route and content options
63+
unset($options['route']);
64+
unset($options['content']);
65+
66+
return $this->innerFactory->createItem($name, $options);
5767
}
5868
}
5969
}

Resources/config/menu.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<service id="cmf_menu.factory.quiet" class="Symfony\Cmf\Bundle\MenuBundle\QuietFactory" public="false">
1212
<argument type="service" id="cmf_menu.factory.quiet.inner" />
13-
<argument type="service" id="logger"/>
13+
<argument type="service" id="logger" on-invalid="ignore" />
1414
<argument>%cmf_menu.allow_empty_items%</argument>
1515
</service>
1616

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'), array('route' => 'not_existent')),
22+
array(array('content' => 'not_existent'), array('content' => 'not_existent')),
23+
array(array('linkType' => 'route', 'route' => 'not_existent'), array('linkType' => 'route')),
24+
);
25+
}
26+
27+
/** @dataProvider provideItemsWithNotExistingLinks */
28+
public function testAllowEmptyItemsReturnsItemWithoutURL(array $firstOptions, array $secondOptions)
29+
{
30+
$this->innerFactory->createItem('Home', $firstOptions)
31+
->willThrow('Symfony\Component\Routing\Exception\RouteNotFoundException');
32+
33+
$homeMenuItem = new \stdClass();
34+
$this->innerFactory->createItem('Home', $secondOptions)->willReturn($homeMenuItem);
35+
36+
$factory = new QuietFactory($this->innerFactory->reveal(), $this->logger->reveal(), true);
37+
38+
$this->assertEquals($homeMenuItem, $factory->createItem('Home', $firstOptions));
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)