AddConsoleCommandPassTest.php 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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\CommandLoader\ContainerCommandLoader;
  13. use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass;
  14. use Symfony\Component\Console\Command\Command;
  15. use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
  16. use Symfony\Component\DependencyInjection\ContainerBuilder;
  17. use Symfony\Component\DependencyInjection\Definition;
  18. use Symfony\Component\DependencyInjection\TypedReference;
  19. class AddConsoleCommandPassTest extends TestCase
  20. {
  21. /**
  22. * @dataProvider visibilityProvider
  23. */
  24. public function testProcess($public)
  25. {
  26. $container = new ContainerBuilder();
  27. $container->addCompilerPass(new AddConsoleCommandPass());
  28. $container->setParameter('my-command.class', 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand');
  29. $id = 'my-command';
  30. $definition = new Definition('%my-command.class%');
  31. $definition->setPublic($public);
  32. $definition->addTag('console.command');
  33. $container->setDefinition($id, $definition);
  34. $container->compile();
  35. $alias = 'console.command.public_alias.my-command';
  36. if ($public) {
  37. $this->assertFalse($container->hasAlias($alias));
  38. } else {
  39. // The alias is replaced by a Definition by the ReplaceAliasByActualDefinitionPass
  40. // in case the original service is private
  41. $this->assertFalse($container->hasDefinition($id));
  42. $this->assertTrue($container->hasDefinition($alias));
  43. }
  44. $this->assertTrue($container->hasParameter('console.command.ids'));
  45. $this->assertSame(array($public ? $id : $alias), $container->getParameter('console.command.ids'));
  46. }
  47. public function testProcessRegistersLazyCommands()
  48. {
  49. $container = new ContainerBuilder();
  50. $command = $container
  51. ->register('my-command', MyCommand::class)
  52. ->setPublic(false)
  53. ->addTag('console.command', array('command' => 'my:command'))
  54. ->addTag('console.command', array('command' => 'my:alias'))
  55. ;
  56. (new AddConsoleCommandPass())->process($container);
  57. $commandLoader = $container->getDefinition('console.command_loader');
  58. $commandLocator = $container->getDefinition((string) $commandLoader->getArgument(0));
  59. $this->assertSame(ContainerCommandLoader::class, $commandLoader->getClass());
  60. $this->assertSame(array('my:command' => 'my-command', 'my:alias' => 'my-command'), $commandLoader->getArgument(1));
  61. $this->assertEquals(array(array('my-command' => new ServiceClosureArgument(new TypedReference('my-command', MyCommand::class)))), $commandLocator->getArguments());
  62. $this->assertSame(array(), $container->getParameter('console.command.ids'));
  63. $this->assertSame(array(array('setName', array('my:command')), array('setAliases', array(array('my:alias')))), $command->getMethodCalls());
  64. }
  65. public function testProcessFallsBackToDefaultName()
  66. {
  67. $container = new ContainerBuilder();
  68. $container
  69. ->register('with-default-name', NamedCommand::class)
  70. ->setPublic(false)
  71. ->addTag('console.command')
  72. ;
  73. $pass = new AddConsoleCommandPass();
  74. $pass->process($container);
  75. $commandLoader = $container->getDefinition('console.command_loader');
  76. $commandLocator = $container->getDefinition((string) $commandLoader->getArgument(0));
  77. $this->assertSame(ContainerCommandLoader::class, $commandLoader->getClass());
  78. $this->assertSame(array('default' => 'with-default-name'), $commandLoader->getArgument(1));
  79. $this->assertEquals(array(array('with-default-name' => new ServiceClosureArgument(new TypedReference('with-default-name', NamedCommand::class)))), $commandLocator->getArguments());
  80. $this->assertSame(array(), $container->getParameter('console.command.ids'));
  81. $container = new ContainerBuilder();
  82. $container
  83. ->register('with-default-name', NamedCommand::class)
  84. ->setPublic(false)
  85. ->addTag('console.command', array('command' => 'new-name'))
  86. ;
  87. $pass->process($container);
  88. $this->assertSame(array('new-name' => 'with-default-name'), $container->getDefinition('console.command_loader')->getArgument(1));
  89. }
  90. public function visibilityProvider()
  91. {
  92. return array(
  93. array(true),
  94. array(false),
  95. );
  96. }
  97. /**
  98. * @expectedException \InvalidArgumentException
  99. * @expectedExceptionMessage The service "my-command" tagged "console.command" must not be abstract.
  100. */
  101. public function testProcessThrowAnExceptionIfTheServiceIsAbstract()
  102. {
  103. $container = new ContainerBuilder();
  104. $container->setResourceTracking(false);
  105. $container->addCompilerPass(new AddConsoleCommandPass());
  106. $definition = new Definition('Symfony\Component\Console\Tests\DependencyInjection\MyCommand');
  107. $definition->addTag('console.command');
  108. $definition->setAbstract(true);
  109. $container->setDefinition('my-command', $definition);
  110. $container->compile();
  111. }
  112. /**
  113. * @expectedException \InvalidArgumentException
  114. * @expectedExceptionMessage The service "my-command" tagged "console.command" must be a subclass of "Symfony\Component\Console\Command\Command".
  115. */
  116. public function testProcessThrowAnExceptionIfTheServiceIsNotASubclassOfCommand()
  117. {
  118. $container = new ContainerBuilder();
  119. $container->setResourceTracking(false);
  120. $container->addCompilerPass(new AddConsoleCommandPass());
  121. $definition = new Definition('SplObjectStorage');
  122. $definition->addTag('console.command');
  123. $container->setDefinition('my-command', $definition);
  124. $container->compile();
  125. }
  126. public function testProcessPrivateServicesWithSameCommand()
  127. {
  128. $container = new ContainerBuilder();
  129. $className = 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand';
  130. $definition1 = new Definition($className);
  131. $definition1->addTag('console.command')->setPublic(false);
  132. $definition2 = new Definition($className);
  133. $definition2->addTag('console.command')->setPublic(false);
  134. $container->setDefinition('my-command1', $definition1);
  135. $container->setDefinition('my-command2', $definition2);
  136. (new AddConsoleCommandPass())->process($container);
  137. $aliasPrefix = 'console.command.public_alias.';
  138. $this->assertTrue($container->hasAlias($aliasPrefix.'my-command1'));
  139. $this->assertTrue($container->hasAlias($aliasPrefix.'my-command2'));
  140. }
  141. }
  142. class MyCommand extends Command
  143. {
  144. }
  145. class NamedCommand extends Command
  146. {
  147. protected static $defaultName = 'default';
  148. }