8000 added some tests · symfony/symfony@1db2d4f · GitHub
[go: up one dir, main page]

Skip to content

Commit 1db2d4f

Browse files
committed
added some tests
1 parent d3271e1 commit 1db2d4f

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,30 +60,32 @@ public function process(ContainerBuilder $container)
6060
{
6161
$compiler = $container->getCompiler();
6262
$formatter = $compiler->getLoggingFormatter();
63-
$tags = $container->findTags();
63+
$tags = array_merge($container->findTags(), $this->whitelist);
6464

65-
$unusedTags = $container->findUnusedTags();
66-
foreach ($unusedTags as $tag) {
65+
foreach ($container->findUnusedTags() as $tag) {
6766
// skip whitelisted tags
6867
if (in_array($tag, $this->whitelist)) {
6968
continue;
7069
}
70+
7171
// check for typos
7272
$candidates = array();
7373
foreach ($tags as $definedTag) {
7474
if ($definedTag === $tag) {
7575
continue;
7676
}
77+
7778
if (false !== strpos($definedTag, $tag) || levenshtein($tag, $definedTag) <= strlen($tag) / 3) {
7879
$candidates[] = $definedTag;
7980
}
8081
}
8182

8283
$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));
84+
$message = sprintf('Tag "%s" was defined on service(s) "%s", but was never used.', $tag, implode('", "', $services));
8485
if (!empty($candidates)) {
8586
$message .= sprintf(' Did you mean "%s"?', implode('", "', $candidates));
8687
}
88+
8789
$compiler->addLogMessage($formatter->format($this, $message));
8890
}
8991
}
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)

0 commit comments

Comments
 (0)
0