AddConsoleCommandPassTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Tests\DependencyInjection;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
  14. use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass;
  15. use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
  16. use Symfony\Component\DependencyInjection\ChildDefinition;
  17. use Symfony\Component\DependencyInjection\Compiler\PassConfig;
  18. use Symfony\Component\DependencyInjection\ContainerBuilder;
  19. use Symfony\Component\DependencyInjection\Definition;
  20. use Symfony\Component\DependencyInjection\TypedReference;
  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(), PassConfig::TYPE_BEFORE_REMOVING);
  30. $container->setParameter('my-command.class', 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand');
  31. $definition = new Definition('%my-command.class%');
  32. $definition->setPublic($public);
  33. $definition->addTag('console.command');
  34. $container->setDefinition('my-command', $definition);
  35. $container->compile();
  36. $alias = 'console.command.symfony_component_console_tests_dependencyinjection_mycommand';
  37. if ($public) {
  38. $this->assertFalse($container->hasAlias($alias));
  39. $id = 'my-command';
  40. } else {
  41. $id = $alias;
  42. // The alias is replaced by a Definition by the ReplaceAliasByActualDefinitionPass
  43. // in case the original service is private
  44. $this->assertFalse($container->hasDefinition('my-command'));
  45. $this->assertTrue($container->hasDefinition($alias));
  46. }
  47. $this->assertTrue($container->hasParameter('console.command.ids'));
  48. $this->assertSame([$alias => $id], $container->getParameter('console.command.ids'));
  49. }
  50. public function testProcessRegistersLazyCommands()
  51. {
  52. $container = new ContainerBuilder();
  53. $command = $container
  54. ->register('my-command', MyCommand::class)
  55. ->setPublic(false)
  56. ->addTag('console.command', ['command' => 'my:command'])
  57. ->addTag('console.command', ['command' => 'my:alias'])
  58. ;
  59. (new AddConsoleCommandPass())->process($container);
  60. $commandLoader = $container->getDefinition('console.command_loader');
  61. $commandLocator = $container->getDefinition((string) $commandLoader->getArgument(0));
  62. $this->assertSame(ContainerCommandLoader::class, $commandLoader->getClass());
  63. $this->assertSame(['my:command' => 'my-command', 'my:alias' => 'my-command'], $commandLoader->getArgument(1));
  64. $this->assertEquals([['my-command' => new ServiceClosureArgument(new TypedReference('my-command', MyCommand::class))]], $commandLocator->getArguments());
  65. $this->assertSame(['console.command.symfony_component_console_tests_dependencyinjection_mycommand' => 'my-command'], $container->getParameter('console.command.ids'));
  66. $this->assertSame(['my-command' => true], $container->getParameter('console.lazy_command.ids'));
  67. $this->assertSame([['setName', ['my:command']], ['setAliases', [['my:alias']]]], $command->getMethodCalls());
  68. }
  69. public function testProcessFallsBackToDefaultName()
  70. {
  71. $container = new ContainerBuilder();
  72. $container
  73. ->register('with-default-name', NamedCommand::class)
  74. ->setPublic(false)
  75. ->addTag('console.command')
  76. ;
  77. $pass = new AddConsoleCommandPass();
  78. $pass->process($container);
  79. $commandLoader = $container->getDefinition('console.command_loader');
  80. $commandLocator = $container->getDefinition((string) $commandLoader->getArgument(0));
  81. $this->assertSame(ContainerCommandLoader::class, $commandLoader->getClass());
  82. $this->assertSame(['default' => 'with-default-name'], $commandLoader->getArgument(1));
  83. $this->assertEquals([['with-default-name' => new ServiceClosureArgument(new TypedReference('with-default-name', NamedCommand::class))]], $commandLocator->getArguments());
  84. $this->assertSame(['console.command.symfony_component_console_tests_dependencyinjection_namedcommand' => 'with-default-name'], $container->getParameter('console.command.ids'));
  85. $this->assertSame(['with-default-name' => true], $container->getParameter('console.lazy_command.ids'));
  86. $container = new ContainerBuilder();
  87. $container
  88. ->register('with-default-name', NamedCommand::class)
  89. ->setPublic(false)
  90. ->addTag('console.command', ['command' => 'new-name'])
  91. ;
  92. $pass->process($container);
  93. $this->assertSame(['new-name' => 'with-default-name'], $container->getDefinition('console.command_loader')->getArgument(1));
  94. }
  95. public function visibilityProvider()
  96. {
  97. return [
  98. [true],
  99. [false],
  100. ];
  101. }
  102. public function testProcessThrowAnExceptionIfTheServiceIsAbstract()
  103. {
  104. $this->expectException('InvalidArgumentException');
  105. $this->expectExceptionMessage('The service "my-command" tagged "console.command" must not be abstract.');
  106. $container = new ContainerBuilder();
  107. $container->setResourceTracking(false);
  108. $container->addCompilerPass(new AddConsoleCommandPass(), PassConfig::TYPE_BEFORE_REMOVING);
  109. $definition = new Definition('Symfony\Component\Console\Tests\DependencyInjection\MyCommand');
  110. $definition->addTag('console.command');
  111. $definition->setAbstract(true);
  112. $container->setDefinition('my-command', $definition);
  113. $container->compile();
  114. }
  115. public function testProcessThrowAnExceptionIfTheServiceIsNotASubclassOfCommand()
  116. {
  117. $this->expectException('InvalidArgumentException');
  118. $this->expectExceptionMessage('The service "my-command" tagged "console.command" must be a subclass of "Symfony\Component\Console\Command\Command".');
  119. $container = new ContainerBuilder();
  120. $container->setResourceTracking(false);
  121. $container->addCompilerPass(new AddConsoleCommandPass(), PassConfig::TYPE_BEFORE_REMOVING);
  122. $definition = new Definition('SplObjectStorage');
  123. $definition->addTag('console.command');
  124. $container->setDefinition('my-command', $definition);
  125. $container->compile();
  126. }
  127. public function testProcessPrivateServicesWithSameCommand()
  128. {
  129. $container = new ContainerBuilder();
  130. $className = 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand';
  131. $definition1 = new Definition($className);
  132. $definition1->addTag('console.command')->setPublic(false);
  133. $definition2 = new Definition($className);
  134. $definition2->addTag('console.command')->setPublic(false);
  135. $container->setDefinition('my-command1', $definition1);
  136. $container->setDefinition('my-command2', $definition2);
  137. (new AddConsoleCommandPass())->process($container);
  138. $alias1 = 'console.command.symfony_component_console_tests_dependencyinjection_mycommand';
  139. $alias2 = $alias1.'_my-command2';
  140. $this->assertTrue($container->hasAlias($alias1));
  141. $this->assertTrue($container->hasAlias($alias2));
  142. }
  143. public function testProcessOnChildDefinitionWithClass()
  144. {
  145. $container = new ContainerBuilder();
  146. $container->addCompilerPass(new AddConsoleCommandPass(), PassConfig::TYPE_BEFORE_REMOVING);
  147. $className = 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand';
  148. $parentId = 'my-parent-command';
  149. $childId = 'my-child-command';
  150. $parentDefinition = new Definition(/* no class */);
  151. $parentDefinition->setAbstract(true)->setPublic(false);
  152. $childDefinition = new ChildDefinition($parentId);
  153. $childDefinition->addTag('console.command')->setPublic(true);
  154. $childDefinition->setClass($className);
  155. $container->setDefinition($parentId, $parentDefinition);
  156. $container->setDefinition($childId, $childDefinition);
  157. $container->compile();
  158. $command = $container->get($childId);
  159. $this->assertInstanceOf($className, $command);
  160. }
  161. public function testProcessOnChildDefinitionWithParentClass()
  162. {
  163. $container = new ContainerBuilder();
  164. $container->addCompilerPass(new AddConsoleCommandPass(), PassConfig::TYPE_BEFORE_REMOVING);
  165. $className = 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand';
  166. $parentId = 'my-parent-command';
  167. $childId = 'my-child-command';
  168. $parentDefinition = new Definition($className);
  169. $parentDefinition->setAbstract(true)->setPublic(false);
  170. $childDefinition = new ChildDefinition($parentId);
  171. $childDefinition->addTag('console.command')->setPublic(true);
  172. $container->setDefinition($parentId, $parentDefinition);
  173. $container->setDefinition($childId, $childDefinition);
  174. $container->compile();
  175. $command = $container->get($childId);
  176. $this->assertInstanceOf($className, $command);
  177. }
  178. public function testProcessOnChildDefinitionWithoutClass()
  179. {
  180. $this->expectException('RuntimeException');
  181. $this->expectExceptionMessage('The definition for "my-child-command" has no class.');
  182. $container = new ContainerBuilder();
  183. $container->addCompilerPass(new AddConsoleCommandPass(), PassConfig::TYPE_BEFORE_REMOVING);
  184. $parentId = 'my-parent-command';
  185. $childId = 'my-child-command';
  186. $parentDefinition = new Definition();
  187. $parentDefinition->setAbstract(true)->setPublic(false);
  188. $childDefinition = new ChildDefinition($parentId);
  189. $childDefinition->addTag('console.command')->setPublic(true);
  190. $container->setDefinition($parentId, $parentDefinition);
  191. $container->setDefinition($childId, $childDefinition);
  192. $container->compile();
  193. }
  194. }
  195. class MyCommand extends Command
  196. {
  197. }
  198. class NamedCommand extends Command
  199. {
  200. protected static $defaultName = 'default';
  201. }