AddConsoleCommandPassTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\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. public function testProcess()
  20. {
  21. $container = new ContainerBuilder();
  22. $container->addCompilerPass(new AddConsoleCommandPass());
  23. $container->setParameter('my-command.class', 'Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\MyCommand');
  24. $definition = new Definition('%my-command.class%');
  25. $definition->addTag('console.command');
  26. $container->setDefinition('my-command', $definition);
  27. $container->compile();
  28. $alias = 'console.command.symfony_bundle_frameworkbundle_tests_dependencyinjection_compiler_mycommand';
  29. $this->assertTrue($container->hasAlias($alias));
  30. $this->assertSame('my-command', (string) $container->getAlias($alias));
  31. $this->assertTrue($container->hasParameter('console.command.ids'));
  32. $this->assertSame(array('my-command'), $container->getParameter('console.command.ids'));
  33. }
  34. /**
  35. * @expectedException \InvalidArgumentException
  36. * @expectedExceptionMessage The service "my-command" tagged "console.command" must be public.
  37. */
  38. public function testProcessThrowAnExceptionIfTheServiceIsNotPublic()
  39. {
  40. $container = new ContainerBuilder();
  41. $container->addCompilerPass(new AddConsoleCommandPass());
  42. $definition = new Definition('Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\MyCommand');
  43. $definition->addTag('console.command');
  44. $definition->setPublic(false);
  45. $container->setDefinition('my-command', $definition);
  46. $container->compile();
  47. }
  48. /**
  49. * @expectedException \InvalidArgumentException
  50. * @expectedExceptionMessage The service "my-command" tagged "console.command" must not be abstract.
  51. */
  52. public function testProcessThrowAnExceptionIfTheServiceIsAbstract()
  53. {
  54. $container = new ContainerBuilder();
  55. $container->addCompilerPass(new AddConsoleCommandPass());
  56. $definition = new Definition('Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\MyCommand');
  57. $definition->addTag('console.command');
  58. $definition->setAbstract(true);
  59. $container->setDefinition('my-command', $definition);
  60. $container->compile();
  61. }
  62. /**
  63. * @expectedException \InvalidArgumentException
  64. * @expectedExceptionMessage The service "my-command" tagged "console.command" must be a subclass of "Symfony\Component\Console\Command\Command".
  65. */
  66. public function testProcessThrowAnExceptionIfTheServiceIsNotASubclassOfCommand()
  67. {
  68. $container = new ContainerBuilder();
  69. $container->addCompilerPass(new AddConsoleCommandPass());
  70. $definition = new Definition('SplObjectStorage');
  71. $definition->addTag('console.command');
  72. $container->setDefinition('my-command', $definition);
  73. $container->compile();
  74. }
  75. }
  76. class MyCommand extends Command
  77. {
  78. }
  79. class ExtensionPresentBundle extends Bundle
  80. {
  81. }