ConsoleEventsTest.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Application;
  13. use Symfony\Component\Console\Command\Command;
  14. use Symfony\Component\Console\ConsoleEvents;
  15. use Symfony\Component\Console\Event\ConsoleCommandEvent;
  16. use Symfony\Component\Console\Event\ConsoleErrorEvent;
  17. use Symfony\Component\Console\Event\ConsoleTerminateEvent;
  18. use Symfony\Component\Console\Input\InputInterface;
  19. use Symfony\Component\Console\Output\OutputInterface;
  20. use Symfony\Component\Console\Tester\ApplicationTester;
  21. use Symfony\Component\DependencyInjection\ContainerBuilder;
  22. use Symfony\Component\DependencyInjection\Reference;
  23. use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
  24. use Symfony\Component\EventDispatcher\EventDispatcher;
  25. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  26. class ConsoleEventsTest extends TestCase
  27. {
  28. public function testEventAliases()
  29. {
  30. $container = new ContainerBuilder();
  31. $container->setParameter('event_dispatcher.event_aliases', ConsoleEvents::ALIASES);
  32. $container->addCompilerPass(new RegisterListenersPass());
  33. $container->register('event_dispatcher', EventDispatcher::class);
  34. $container->register('tracer', EventTraceSubscriber::class)
  35. ->setPublic(true)
  36. ->addTag('kernel.event_subscriber');
  37. $container->register('failing_command', FailingCommand::class);
  38. $container->register('application', Application::class)
  39. ->setPublic(true)
  40. ->addMethodCall('setAutoExit', [false])
  41. ->addMethodCall('setDispatcher', [new Reference('event_dispatcher')])
  42. ->addMethodCall('add', [new Reference('failing_command')])
  43. ;
  44. $container->compile();
  45. $tester = new ApplicationTester($container->get('application'));
  46. $tester->run(['fail']);
  47. $this->assertSame([ConsoleCommandEvent::class, ConsoleErrorEvent::class, ConsoleTerminateEvent::class], $container->get('tracer')->observedEvents);
  48. }
  49. }
  50. class EventTraceSubscriber implements EventSubscriberInterface
  51. {
  52. public $observedEvents = [];
  53. public static function getSubscribedEvents(): array
  54. {
  55. return [
  56. ConsoleCommandEvent::class => 'observe',
  57. ConsoleErrorEvent::class => 'observe',
  58. ConsoleTerminateEvent::class => 'observe',
  59. ];
  60. }
  61. public function observe(object $event): void
  62. {
  63. $this->observedEvents[] = get_debug_type($event);
  64. }
  65. }
  66. class FailingCommand extends Command
  67. {
  68. protected static $defaultName = 'fail';
  69. protected function execute(InputInterface $input, OutputInterface $output): int
  70. {
  71. throw new \RuntimeException('I failed. Sorry.');
  72. }
  73. }