Skip to content

Commit de6c3eb

Browse files
committed
added some tests
1 parent d3271e1 commit de6c3eb

File tree

5 files changed

+91
-45
lines changed

5 files changed

+91
-45
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/UnusedTagsPass.php

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,69 +21,66 @@
2121
*/
2222
class UnusedTagsPass implements CompilerPassInterface
2323
{
24-
/**
25-
* whitelisted tags
26-
*
27-
* @var array
28-
*/
29-
protected $whitelist = array(
30-
"console.command",
31-
"config_cache.resource_checker",
32-
"data_collector",
33-
"form.type",
34-
"form.type_extension",
35-
"form.type_guesser",
36-
"kernel.cache_clearer",
37-
"kernel.cache_warmer",
38-
"kernel.event_listener",
39-
"kernel.event_subscriber",
40-
"kernel.fragment_renderer",
41-
"monolog.logger",
42-
"routing.expression_language_provider",
43-
"routing.loader",
44-
"security.expression_language_provider",
45-
"security.remember_me_aware",
46-
"security.voter",
47-
"serializer.encoder",
48-
"serializer.normalizer",
49-
"templating.helper",
50-
"translation.dumper",
51-
"translation.extractor",
52-
"translation.loader",
53-
"twig.extension",
54-
"twig.loader",
55-
"validator.constraint_validator",
56-
"validator.initializer",
24+
private $whitelist = array(
25+
'console.command',
26+
'config_cache.resource_checker',
27+
'data_collector',
28+
'form.type',
29+
'form.type_extension',
30+
'form.type_guesser',
31+
'kernel.cache_clearer',
32+
'kernel.cache_warmer',
33+
'kernel.event_listener',
34+
'kernel.event_subscriber',
35+
'kernel.fragment_renderer',
36+
'monolog.logger',
37+
'routing.expression_language_provider',
38+
'routing.loader',
39+
'security.expression_language_provider',
40+
'security.remember_me_aware',
41+
'security.voter',
42+
'serializer.encoder',
43+
'serializer.normalizer',
44+
'templating.helper',
45+
'translation.dumper',
46+
'translation.extractor',
47+
'translation.loader',
48+
'twig.extension',
49+
'twig.loader',
50+
'validator.constraint_validator',
51+
'validator.initializer',
5752
);
5853

5954
public function process(ContainerBuilder $container)
6055
{
6156
$compiler = $container->getCompiler();
6257
$formatter = $compiler->getLoggingFormatter();
63-
$tags = $container->findTags();
58+
$tags = array_unique(array_merge($container->findTags(), $this->whitelist));
6459

65-
$unusedTags = $container->findUnusedTags();
66-
foreach ($unusedTags as $tag) {
60+
foreach ($container->findUnusedTags() as $tag) {
6761
// skip whitelisted tags
6862
if (in_array($tag, $this->whitelist)) {
6963
continue;
7064
}
65+
7166
// check for typos
7267
$candidates = array();
7368
foreach ($tags as $definedTag) {
7469
if ($definedTag === $tag) {
7570
continue;
7671
}
72+
7773
if (false !== strpos($definedTag, $tag) || levenshtein($tag, $definedTag) <= strlen($tag) / 3) {
7874
$candidates[] = $definedTag;
7975
}
8076
}
8177

8278
$services = array_keys($container->findTaggedServiceIds($tag));
83-
$message = sprintf('Tag "%s" was defined on the service(s) %s, but was never used.', $tag, implode(',', $services));
79+
$message = sprintf('Tag "%s" was defined on service(s) "%s", but was never used.', $tag, implode('", "', $services));
8480
if (!empty($candidates)) {
8581
$message .= sprintf(' Did you mean "%s"?', implode('", "', $candidates));
8682
}
83+
8784
$compiler->addLogMessage($formatter->format($this, $message));
8885
}
8986
}

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ public function build(ContainerBuilder $container)
9090
$container->addCompilerPass(new TranslationDumperPass());
9191
$container->addCompilerPass(new FragmentRendererPass(), PassConfig::TYPE_AFTER_REMOVING);
9292
$container->addCompilerPass(new SerializerPass());
93-
$container->addCompilerPass(new UnusedTagsPass(), PassConfig::TYPE_AFTER_REMOVING);
9493

9594
if ($container->getParameter('kernel.debug')) {
95+
$container->addCompilerPass(new UnusedTagsPass(), PassConfig::TYPE_AFTER_REMOVING);
9696
$container->addCompilerPass(new ContainerBuilderDebugDumpPass(), PassConfig::TYPE_AFTER_REMOVING);
9797
$container->addCompilerPass(new CompilerDebugDumpPass(), PassConfig::TYPE_AFTER_REMOVING);
9898
$container->addCompilerPass(new ConfigCachePass());
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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+
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
13+
14+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\UnusedTagsPass;
15+
16+
class UnusedTagsPassTest extends \PHPUnit_Framework_TestCase
17+
{
18+
public function testProcess()
19+
{
20+
$pass = new UnusedTagsPass();
21+
22+
$formatter = $this->getMock('Symfony\Component\DependencyInjection\Compiler\LoggingFormatter');
23+
$formatter
24+
->expects($this->at(0))
25+
->method('format')
26+
->with($pass, 'Tag "kenrel.event_subscriber" was defined on service(s) "foo", "bar", but was never used. Did you mean "kernel.event_subscriber"?')
27+
;
28+
29+
$compiler = $this->getMock('Symfony\Component\DependencyInjection\Compiler\Compiler');
30+
$compiler->expects($this->once())->method('getLoggingFormatter')->will($this->returnValue($formatter));
31+
32+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder',
33+
array('findTaggedServiceIds', 'getCompiler', 'findUnusedTags', 'findTags')
34+
);
35+
$container->expects($this->once())->method('getCompiler')->will($this->returnValue($compiler));
36+
$container->expects($this->once())
37+
->method('findTags')
38+
->will($this->returnValue(array('kenrel.event_subscriber')));
39+
$container->expects($this->once())
40+
->method('findUnusedTags')
41+
->will($this->returnValue(array('kenrel.event_subscriber', 'form.type')));
42+
$container->expects($this->once())
43+
->method('findTaggedServiceIds')
44+
->with('kenrel.event_subscriber')
45+
->will($this->returnValue(array(
46+
'foo' => array(),
47+
'bar' => array(),
48+
)));
49+
50+
$pass->process($container);
51+
}
52+
}

src/Symfony/Component/DependencyInjection/Compiler/LoggingFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class LoggingFormatter
2020
{
2121
public function formatRemoveService(CompilerPassInterface $pass, $id, $reason)
2222
{
23-
return $this->format($pass, sprintf('Removed service "%s"; reason: %s', $id, $reason));
23+
return $this->format($pass, sprintf('Removed service "%s"; reason: %s.', $id, $reason));
2424
}
2525

2626
public function formatInlineService(CompilerPassInterface $pass, $id, $target)

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
9191
private $expressionLanguageProviders = array();
9292

9393
/**
94-
* @var array with tag names used by findTaggedServiceIds
94+
* @var string[] with tag names used by findTaggedServiceIds
9595
*/
9696
private $usedTags = array();
9797

@@ -1098,14 +1098,11 @@ public function findTags()
10981098
/**
10991099
* Returns all tags not queried by findTaggedServiceIds
11001100
*
1101-
* @return array An array of tags
1101+
* @return string[] An array of tags
11021102
*/
11031103
public function findUnusedTags()
11041104
{
1105-
$tags = array_values(array_diff($this->findTags(), $this->usedTags));
1106-
$tags = array_unique($tags);
1107-
1108-
return $tags;
1105+
return array_values(array_diff($this->findTags(), $this->usedTags));
11091106
}
11101107

11111108
public function addExpressionLanguageProvider(ExpressionFunctionProviderInterface $provider)

0 commit comments

Comments
 (0)