AddConsoleCommandPassTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. $id = 'my-command';
  32. $definition = new Definition('%my-command.class%');
  33. $definition->setPublic($public);
  34. $definition->addTag('console.command');
  35. $container->setDefinition($id, $definition);
  36. $container->compile();
  37. $alias = 'console.command.public_alias.my-command';
  38. if ($public) {
  39. $this->assertFalse($container->hasAlias($alias));
  40. } else {
  41. // The alias is replaced by a Definition by the ReplaceAliasByActualDefinitionPass
  42. // in case the original service is private
  43. $this->assertFalse($container->hasDefinition($id));
  44. $this->assertTrue($container->hasDefinition($alias));
  45. }
  46. $this->assertTrue($container->hasParameter('console.command.ids'));
  47. $this->assertSame([$public ? $id : $alias], $container->getParameter('console.command.ids'));
  48. }
  49. public function testProcessRegistersLazyCommands()
  50. {
  51. $container = new ContainerBuilder();
  52. $command = $container
  53. ->register('my-command', MyCommand::class)
  54. ->setPublic(false)
  55. ->addTag('console.command', ['command' => 'my:command'])
  56. ->addTag('console.command', ['command' => 'my:alias'])
  57. ;
  58. (new AddConsoleCommandPass())->process($container);
  59. $commandLoader = $container->getDefinition('console.command_loader');
  60. $commandLocator = $container->getDefinition((string) $commandLoader->getArgument(0));
  61. $this->assertSame(ContainerCommandLoader::class, $commandLoader->getClass());
  62. $this->assertSame(['my:command' => 'my-command', 'my:alias' => 'my-command'], $commandLoader->getArgument(1));
  63. $this->assertEquals([['my-command' => new ServiceClosureArgument(new TypedReference('my-command', MyCommand::class))]], $commandLocator->getArguments());
  64. $this->assertSame([], $container->getParameter('console.command.ids'));
  65. $this->assertSame([['setName', ['my:command']], ['setAliases', [['my:alias']]]], $command->getMethodCalls());
  66. }
  67. public function testProcessFallsBackToDefaultName()
  68. {
  69. $container = new ContainerBuilder();
  70. $container
  71. ->register('with-default-name', NamedCommand::class)
  72. ->setPublic(false)
  73. ->addTag('console.command')
  74. ;
  75. $pass = new AddConsoleCommandPass();
  76. $pass->process($container);
  77. $commandLoader = $container->getDefinition('console.command_loader');
  78. $commandLocator = $container->getDefinition((string) $commandLoader->getArgument(0));
  79. $this->assertSame(ContainerCommandLoader::class, $commandLoader->getClass());
  80. $this->assertSame(['default' => 'with-default-name'], $commandLoader->getArgument(1));
  81. $this->assertEquals([['with-default-name' => new ServiceClosureArgument(new TypedReference('with-default-name', NamedCommand::class))]], $commandLocator->getArguments());
  82. $this->assertSame([], $container->getParameter('console.command.ids'));
  83. $container = new ContainerBuilder();
  84. $container
  85. ->register('with-default-name', NamedCommand::class)
  86. ->setPublic(false)
  87. ->addTag('console.command', ['command' => 'new-name'])
  88. ;
  89. $pass->process($container);
  90. $this->assertSame(['new-name' => 'with-default-name'], $container->getDefinition('console.command_loader')->getArgument(1));
  91. }
  92. public function visibilityProvider()
  93. {
  94. return [
  95. [true],
  96. [false],
  97. ];
  98. }
  99. /**
  100. * @expectedException \InvalidArgumentException
  101. * @expectedExceptionMessage The service "my-command" tagged "console.command" must not be abstract.
  102. */
  103. public function testProcessThrowAnExceptionIfTheServiceIsAbstract()
  104. {
  105. $container = new ContainerBuilder();
  106. $container->setResourceTracking(false);
  107. $container->addCompilerPass(new AddConsoleCommandPass(), PassConfig::TYPE_BEFORE_REMOVING);
  108. $definition = new Definition('Symfony\Component\Console\Tests\DependencyInjection\MyCommand');
  109. $definition->addTag('console.command');
  110. $definition->setAbstract(true);
  111. $container->setDefinition('my-command', $definition);
  112. $container->compile();
  113. }
  114. /**
  115. * @expectedException \InvalidArgumentException
  116. * @expectedExceptionMessage The service "my-command" tagged "console.command" must be a subclass of "Symfony\Component\Console\Command\Command".
  117. */
  118. public function testProcessThrowAnExceptionIfTheServiceIsNotASubclassOfCommand()
  119. {
  120. $container = new ContainerBuilder();
  121. $container->setResourceTracking(false);
  122. $container->addCompilerPass(new AddConsoleCommandPass(), PassConfig::TYPE_BEFORE_REMOVING);
  123. $definition = new Definition('SplObjectStorage');
  124. $definition->addTag('console.command');
  125. $container->setDefinition('my-command', $definition);
  126. $container->compile();
  127. }
  128. public function testProcessPrivateServicesWithSameCommand()
  129. {
  130. $container = new ContainerBuilder();
  131. $className = 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand';
  132. $definition1 = new Definition($className);
  133. $definition1->addTag('console.command')->setPublic(false);
  134. $definition2 = new Definition($className);
  135. $definition2->addTag('console.command')->setPublic(false);
  136. $container->setDefinition('my-command1', $definition1);
  137. $container->setDefinition('my-command2', $definition2);
  138. (new AddConsoleCommandPass())->process($container);
  139. $aliasPrefix = 'console.command.public_alias.';
  140. $this->assertTrue($container->hasAlias($aliasPrefix.'my-command1'));
  141. $this->assertTrue($container->hasAlias($aliasPrefix.'my-command2'));
  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. /**
  179. * @expectedException \RuntimeException
  180. * @expectedExceptionMessage The definition for "my-child-command" has no class.
  181. */
  182. public function testProcessOnChildDefinitionWithoutClass()
  183. {
  184. $container = new ContainerBuilder();
  185. $container->addCompilerPass(new AddConsoleCommandPass(), PassConfig::TYPE_BEFORE_REMOVING);
  186. $parentId = 'my-parent-command';
  187. $childId = 'my-child-command';
  188. $parentDefinition = new Definition();
  189. $parentDefinition->setAbstract(true)->setPublic(false);
  190. $childDefinition = new ChildDefinition($parentId);
  191. $childDefinition->addTag('console.command')->setPublic(true);
  192. $container->setDefinition($parentId, $parentDefinition);
  193. $container->setDefinition($childId, $childDefinition);
  194. $container->compile();
  195. }
  196. }
  197. class MyCommand extends Command
  198. {
  199. }
  200. class NamedCommand extends Command
  201. {
  202. protected static $defaultName = 'default';
  203. }