菜谱项目

AddConsoleCommandPassTest.php 4.3KB

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