]> _ Git - physioassist-wordpress.git/blob
0cf4631754522043bbd08187d3862cf04e79fb4b
[physioassist-wordpress.git] /
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\Component\Console\Tests\DependencyInjection;
13
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;
20
21 class AddConsoleCommandPassTest extends TestCase
22 {
23     /**
24      * @dataProvider visibilityProvider
25      */
26     public function testProcess($public)
27     {
28         $container = new ContainerBuilder();
29         $container->addCompilerPass(new AddConsoleCommandPass());
30         $container->setParameter('my-command.class', 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand');
31
32         $definition = new Definition('%my-command.class%');
33         $definition->setPublic($public);
34         $definition->addTag('console.command');
35         $container->setDefinition('my-command', $definition);
36
37         $container->compile();
38
39         $alias = 'console.command.symfony_component_console_tests_dependencyinjection_mycommand';
40
41         if ($public) {
42             $this->assertFalse($container->hasAlias($alias));
43             $id = 'my-command';
44         } else {
45             $id = $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));
50         }
51
52         $this->assertTrue($container->hasParameter('console.command.ids'));
53         $this->assertSame(array($alias => $id), $container->getParameter('console.command.ids'));
54     }
55
56     public function visibilityProvider()
57     {
58         return array(
59             array(true),
60             array(false),
61         );
62     }
63
64     /**
65      * @expectedException \InvalidArgumentException
66      * @expectedExceptionMessage The service "my-command" tagged "console.command" must not be abstract.
67      */
68     public function testProcessThrowAnExceptionIfTheServiceIsAbstract()
69     {
70         $container = new ContainerBuilder();
71         $container->setResourceTracking(false);
72         $container->addCompilerPass(new AddConsoleCommandPass());
73
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);
78
79         $container->compile();
80     }
81
82     /**
83      * @expectedException \InvalidArgumentException
84      * @expectedExceptionMessage The service "my-command" tagged "console.command" must be a subclass of "Symfony\Component\Console\Command\Command".
85      */
86     public function testProcessThrowAnExceptionIfTheServiceIsNotASubclassOfCommand()
87     {
88         $container = new ContainerBuilder();
89         $container->setResourceTracking(false);
90         $container->addCompilerPass(new AddConsoleCommandPass());
91
92         $definition = new Definition('SplObjectStorage');
93         $definition->addTag('console.command');
94         $container->setDefinition('my-command', $definition);
95
96         $container->compile();
97     }
98
99     public function testProcessPrivateServicesWithSameCommand()
100     {
101         $container = new ContainerBuilder();
102         $className = 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand';
103
104         $definition1 = new Definition($className);
105         $definition1->addTag('console.command')->setPublic(false);
106
107         $definition2 = new Definition($className);
108         $definition2->addTag('console.command')->setPublic(false);
109
110         $container->setDefinition('my-command1', $definition1);
111         $container->setDefinition('my-command2', $definition2);
112
113         (new AddConsoleCommandPass())->process($container);
114
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));
119     }
120 }
121
122 class MyCommand extends Command
123 {
124 }
125
126 class ExtensionPresentBundle extends Bundle
127 {
128 }