AddConsoleCommandPassTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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\DependencyInjection\AddConsoleCommandPass;
  13. use Symfony\Component\Console\Command\Command;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\DependencyInjection\Definition;
  16. use Symfony\Component\HttpKernel\Bundle\Bundle;
  17. class AddConsoleCommandPassTest extends TestCase
  18. {
  19. /**
  20. * @dataProvider visibilityProvider
  21. */
  22. public function testProcess($public)
  23. {
  24. $container = new ContainerBuilder();
  25. $container->addCompilerPass(new AddConsoleCommandPass());
  26. $container->setParameter('my-command.class', 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand');
  27. $definition = new Definition('%my-command.class%');
  28. $definition->setPublic($public);
  29. $definition->addTag('console.command');
  30. $container->setDefinition('my-command', $definition);
  31. $container->compile();
  32. $alias = 'console.command.symfony_component_console_tests_dependencyinjection_mycommand';
  33. if ($public) {
  34. $this->assertFalse($container->hasAlias($alias));
  35. $id = 'my-command';
  36. } else {
  37. $id = $alias;
  38. // The alias is replaced by a Definition by the ReplaceAliasByActualDefinitionPass
  39. // in case the original service is private
  40. $this->assertFalse($container->hasDefinition('my-command'));
  41. $this->assertTrue($container->hasDefinition($alias));
  42. }
  43. $this->assertTrue($container->hasParameter('console.command.ids'));
  44. $this->assertSame(array($alias => $id), $container->getParameter('console.command.ids'));
  45. }
  46. public function visibilityProvider()
  47. {
  48. return array(
  49. array(true),
  50. array(false),
  51. );
  52. }
  53. /**
  54. * @expectedException \InvalidArgumentException
  55. * @expectedExceptionMessage The service "my-command" tagged "console.command" must not be abstract.
  56. */
  57. public function testProcessThrowAnExceptionIfTheServiceIsAbstract()
  58. {
  59. $container = new ContainerBuilder();
  60. $container->setResourceTracking(false);
  61. $container->addCompilerPass(new AddConsoleCommandPass());
  62. $definition = new Definition('Symfony\Component\Console\Tests\DependencyInjection\MyCommand');
  63. $definition->addTag('console.command');
  64. $definition->setAbstract(true);
  65. $container->setDefinition('my-command', $definition);
  66. $container->compile();
  67. }
  68. /**
  69. * @expectedException \InvalidArgumentException
  70. * @expectedExceptionMessage The service "my-command" tagged "console.command" must be a subclass of "Symfony\Component\Console\Command\Command".
  71. */
  72. public function testProcessThrowAnExceptionIfTheServiceIsNotASubclassOfCommand()
  73. {
  74. $container = new ContainerBuilder();
  75. $container->setResourceTracking(false);
  76. $container->addCompilerPass(new AddConsoleCommandPass());
  77. $definition = new Definition('SplObjectStorage');
  78. $definition->addTag('console.command');
  79. $container->setDefinition('my-command', $definition);
  80. $container->compile();
  81. }
  82. public function testProcessPrivateServicesWithSameCommand()
  83. {
  84. $container = new ContainerBuilder();
  85. $className = 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand';
  86. $definition1 = new Definition($className);
  87. $definition1->addTag('console.command')->setPublic(false);
  88. $definition2 = new Definition($className);
  89. $definition2->addTag('console.command')->setPublic(false);
  90. $container->setDefinition('my-command1', $definition1);
  91. $container->setDefinition('my-command2', $definition2);
  92. (new AddConsoleCommandPass())->process($container);
  93. $alias1 = 'console.command.symfony_component_console_tests_dependencyinjection_mycommand';
  94. $alias2 = $alias1.'_my-command2';
  95. $this->assertTrue($container->hasAlias($alias1));
  96. $this->assertTrue($container->hasAlias($alias2));
  97. }
  98. }
  99. class MyCommand extends Command
  100. {
  101. }
  102. class ExtensionPresentBundle extends Bundle
  103. {
  104. }