4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Tests\DependencyInjection;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass;
16 use Symfony\Component\Console\Command\Command;
17 use Symfony\Component\DependencyInjection\ContainerBuilder;
18 use Symfony\Component\DependencyInjection\Definition;
19 use Symfony\Component\HttpKernel\Bundle\Bundle;
21 class AddConsoleCommandPassTest extends TestCase
24 * @dataProvider visibilityProvider
26 public function testProcess($public)
28 $container = new ContainerBuilder();
29 $container->addCompilerPass(new AddConsoleCommandPass());
30 $container->setParameter('my-command.class', 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand');
32 $definition = new Definition('%my-command.class%');
33 $definition->setPublic($public);
34 $definition->addTag('console.command');
35 $container->setDefinition('my-command', $definition);
37 $container->compile();
39 $alias = 'console.command.symfony_component_console_tests_dependencyinjection_mycommand';
42 $this->assertFalse($container->hasAlias($alias));
46 // The alias is replaced by a Definition by the ReplaceAliasByActualDefinitionPass
47 // in case the original service is private
48 $this->assertFalse($container->hasDefinition('my-command'));
49 $this->assertTrue($container->hasDefinition($alias));
52 $this->assertTrue($container->hasParameter('console.command.ids'));
53 $this->assertSame(array($alias => $id), $container->getParameter('console.command.ids'));
56 public function visibilityProvider()
65 * @expectedException \InvalidArgumentException
66 * @expectedExceptionMessage The service "my-command" tagged "console.command" must not be abstract.
68 public function testProcessThrowAnExceptionIfTheServiceIsAbstract()
70 $container = new ContainerBuilder();
71 $container->setResourceTracking(false);
72 $container->addCompilerPass(new AddConsoleCommandPass());
74 $definition = new Definition('Symfony\Component\Console\Tests\DependencyInjection\MyCommand');
75 $definition->addTag('console.command');
76 $definition->setAbstract(true);
77 $container->setDefinition('my-command', $definition);
79 $container->compile();
83 * @expectedException \InvalidArgumentException
84 * @expectedExceptionMessage The service "my-command" tagged "console.command" must be a subclass of "Symfony\Component\Console\Command\Command".
86 public function testProcessThrowAnExceptionIfTheServiceIsNotASubclassOfCommand()
88 $container = new ContainerBuilder();
89 $container->setResourceTracking(false);
90 $container->addCompilerPass(new AddConsoleCommandPass());
92 $definition = new Definition('SplObjectStorage');
93 $definition->addTag('console.command');
94 $container->setDefinition('my-command', $definition);
96 $container->compile();
99 public function testProcessPrivateServicesWithSameCommand()
101 $container = new ContainerBuilder();
102 $className = 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand';
104 $definition1 = new Definition($className);
105 $definition1->addTag('console.command')->setPublic(false);
107 $definition2 = new Definition($className);
108 $definition2->addTag('console.command')->setPublic(false);
110 $container->setDefinition('my-command1', $definition1);
111 $container->setDefinition('my-command2', $definition2);
113 (new AddConsoleCommandPass())->process($container);
115 $alias1 = 'console.command.symfony_component_console_tests_dependencyinjection_mycommand';
116 $alias2 = $alias1.'_my-command2';
117 $this->assertTrue($container->hasAlias($alias1));
118 $this->assertTrue($container->hasAlias($alias2));
122 class MyCommand extends Command
126 class ExtensionPresentBundle extends Bundle