AddConsoleCommandPassTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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(array($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', array('command' => 'my:command'))
  57. ->addTag('console.command', array('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(array('my:command' => 'my-command', 'my:alias' => 'my-command'), $commandLoader->getArgument(1));
  64. $this->assertEquals(array(array('my-command' => new ServiceClosureArgument(new TypedReference('my-command', MyCommand::class)))), $commandLocator->getArguments());
  65. $this->assertSame(array('console.command.symfony_component_console_tests_dependencyinjection_mycommand' => 'my-command'), $container->getParameter('console.command.ids'));
  66. $this->assertSame(array('my-command' => true), $container->getParameter('console.lazy_command.ids'));
  67. $this->assertSame(array(array('setName', array('my:command')), array('setAliases', array(array('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(array('default' => 'with-default-name'), $commandLoader->getArgument(1));
  83. $this->assertEquals(array(array('with-default-name' => new ServiceClosureArgument(new TypedReference('with-default-name', NamedCommand::class)))), $commandLocator->getArguments());
  84. $this->assertSame(array('console.command.symfony_component_console_tests_dependencyinjection_namedcommand' => 'with-default-name'), $container->getParameter('console.command.ids'));
  85. $this->assertSame(array('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', array('command' => 'new-name'))
  91. ;
  92. $pass->process($container);
  93. $this->assertSame(array('new-name' => 'with-default-name'), $container->getDefinition('console.command_loader')->getArgument(1));
  94. }
  95. public function visibilityProvider()
  96. {
  97. return array(
  98. array(true),
  99. array(false),
  100. );
  101. }
  102. /**
  103. * @expectedException \InvalidArgumentException
  104. * @expectedExceptionMessage The service "my-command" tagged "console.command" must not be abstract.
  105. */
  106. public function testProcessThrowAnExceptionIfTheServiceIsAbstract()
  107. {
  108. $container = new ContainerBuilder();
  109. $container->setResourceTracking(false);
  110. $container->addCompilerPass(new AddConsoleCommandPass(), PassConfig::TYPE_BEFORE_REMOVING);
  111. $definition = new Definition('Symfony\Component\Console\Tests\DependencyInjection\MyCommand');
  112. $definition->addTag('console.command');
  113. $definition->setAbstract(true);
  114. $container->setDefinition('my-command', $definition);
  115. $container->compile();
  116. }
  117. /**
  118. * @expectedException \InvalidArgumentException
  119. * @expectedExceptionMessage The service "my-command" tagged "console.command" must be a subclass of "Symfony\Component\Console\Command\Command".
  120. */
  121. public function testProcessThrowAnExceptionIfTheServiceIsNotASubclassOfCommand()
  122. {
  123. $container = new ContainerBuilder();
  124. $container->setResourceTracking(false);
  125. $container->addCompilerPass(new AddConsoleCommandPass(), PassConfig::TYPE_BEFORE_REMOVING);
  126. $definition = new Definition('SplObjectStorage');
  127. $definition->addTag('console.command');
  128. $container->setDefinition('my-command', $definition);
  129. $container->compile();
  130. }
  131. public function testProcessPrivateServicesWithSameCommand()
  132. {
  133. $container = new ContainerBuilder();
  134. $className = 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand';
  135. $definition1 = new Definition($className);
  136. $definition1->addTag('console.command')->setPublic(false);
  137. $definition2 = new Definition($className);
  138. $definition2->addTag('console.command')->setPublic(false);
  139. $container->setDefinition('my-command1', $definition1);
  140. $container->setDefinition('my-command2', $definition2);
  141. (new AddConsoleCommandPass())->process($container);
  142. $alias1 = 'console.command.symfony_component_console_tests_dependencyinjection_mycommand';
  143. $alias2 = $alias1.'_my-command2';
  144. $this->assertTrue($container->hasAlias($alias1));
  145. $this->assertTrue($container->hasAlias($alias2));
  146. }
  147. public function testProcessOnChildDefinitionWithClass()
  148. {
  149. $container = new ContainerBuilder();
  150. $container->addCompilerPass(new AddConsoleCommandPass(), PassConfig::TYPE_BEFORE_REMOVING);
  151. $className = 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand';
  152. $parentId = 'my-parent-command';
  153. $childId = 'my-child-command';
  154. $parentDefinition = new Definition(/* no class */);
  155. $parentDefinition->setAbstract(true)->setPublic(false);
  156. $childDefinition = new ChildDefinition($parentId);
  157. $childDefinition->addTag('console.command')->setPublic(true);
  158. $childDefinition->setClass($className);
  159. $container->setDefinition($parentId, $parentDefinition);
  160. $container->setDefinition($childId, $childDefinition);
  161. $container->compile();
  162. $command = $container->get($childId);
  163. $this->assertInstanceOf($className, $command);
  164. }
  165. public function testProcessOnChildDefinitionWithParentClass()
  166. {
  167. $container = new ContainerBuilder();
  168. $container->addCompilerPass(new AddConsoleCommandPass(), PassConfig::TYPE_BEFORE_REMOVING);
  169. $className = 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand';
  170. $parentId = 'my-parent-command';
  171. $childId = 'my-child-command';
  172. $parentDefinition = new Definition($className);
  173. $parentDefinition->setAbstract(true)->setPublic(false);
  174. $childDefinition = new ChildDefinition($parentId);
  175. $childDefinition->addTag('console.command')->setPublic(true);
  176. $container->setDefinition($parentId, $parentDefinition);
  177. $container->setDefinition($childId, $childDefinition);
  178. $container->compile();
  179. $command = $container->get($childId);
  180. $this->assertInstanceOf($className, $command);
  181. }
  182. /**
  183. * @expectedException \RuntimeException
  184. * @expectedExceptionMessage The definition for "my-child-command" has no class.
  185. */
  186. public function testProcessOnChildDefinitionWithoutClass()
  187. {
  188. $container = new ContainerBuilder();
  189. $container->addCompilerPass(new AddConsoleCommandPass(), PassConfig::TYPE_BEFORE_REMOVING);
  190. $parentId = 'my-parent-command';
  191. $childId = 'my-child-command';
  192. $parentDefinition = new Definition();
  193. $parentDefinition->setAbstract(true)->setPublic(false);
  194. $childDefinition = new ChildDefinition($parentId);
  195. $childDefinition->addTag('console.command')->setPublic(true);
  196. $container->setDefinition($parentId, $parentDefinition);
  197. $container->setDefinition($childId, $childDefinition);
  198. $container->compile();
  199. }
  200. }
  201. class MyCommand extends Command
  202. {
  203. }
  204. class NamedCommand extends Command
  205. {
  206. protected static $defaultName = 'default';
  207. }