-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
UnusedTagsPassTest.php
60 lines (49 loc) · 2.2 KB
/
UnusedTagsPassTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\UnusedTagsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* @requires PHP 7.0
*/
class UnusedTagsPassTest extends TestCase
{
public function testProcess()
{
$pass = new UnusedTagsPass();
$container = new ContainerBuilder();
$container->register('foo')
->addTag('kenrel.event_subscriber');
$container->register('bar')
->addTag('kenrel.event_subscriber');
$pass->process($container);
$this->assertSame([sprintf('%s: Tag "kenrel.event_subscriber" was defined on service(s) "foo", "bar", but was never used. Did you mean "kernel.event_subscriber"?', UnusedTagsPass::class)], $container->getCompiler()->getLog());
}
public function testMissingWhitelistTags()
{
if (\dirname((new \ReflectionClass(ContainerBuilder::class))->getFileName(), 3) !== \dirname(__DIR__, 5)) {
$this->markTestSkipped('Tests are not run from the root symfony/symfony metapackage.');
}
$this->assertSame(UnusedTagsPassUtils::getDefinedTags(), $this->getWhitelistTags(), 'The src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/UnusedTagsPass.php file must be updated; run src/Symfony/Bundle/FrameworkBundle/Resources/bin/check-unused-tags-whitelist.php.');
}
private function getWhitelistTags()
{
// get tags in UnusedTagsPass
$target = \dirname(__DIR__, 3).'/DependencyInjection/Compiler/UnusedTagsPass.php';
$contents = file_get_contents($target);
preg_match('{private \$whitelist = \[(.+?)\];}sm', $contents, $matches);
$tags = array_values(array_filter(array_map(function ($str) {
return trim(preg_replace('{^ +\'(.+)\',}', '$1', $str));
}, explode("\n", $matches[1]))));
sort($tags);
return $tags;
}
}