|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace M6Web\Bundle\DaemonBundle\Tests\Integration\EventListener; |
| 6 | + |
| 7 | +use M6Web\Bundle\DaemonBundle\EventListener\ConsoleCommandListener; |
| 8 | +use M6Web\Bundle\DaemonBundle\Tests\Fixtures\Command\DaemonCommandConcrete; |
| 9 | +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
| 10 | +use Symfony\Component\Console\Command\Command; |
| 11 | +use Symfony\Component\Console\Event\ConsoleSignalEvent; |
| 12 | +use Symfony\Component\Console\Input\ArrayInput; |
| 13 | +use Symfony\Component\Console\Output\BufferedOutput; |
| 14 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 15 | + |
| 16 | +/** |
| 17 | + * Integration tests for ConsoleCommandListener |
| 18 | + * These tests verify the listener works correctly with Symfony's event dispatcher |
| 19 | + */ |
| 20 | +class ConsoleCommandListenerIntegrationTest extends KernelTestCase |
| 21 | +{ |
| 22 | + private EventDispatcher $dispatcher; |
| 23 | + private ConsoleCommandListener $listener; |
| 24 | + |
| 25 | + protected function setUp(): void |
| 26 | + { |
| 27 | + $this->dispatcher = new EventDispatcher(); |
| 28 | + $this->listener = new ConsoleCommandListener(); |
| 29 | + |
| 30 | + // Register the listener |
| 31 | + $this->dispatcher->addListener(ConsoleSignalEvent::class, [$this->listener, 'onConsoleSignal']); |
| 32 | + } |
| 33 | + |
| 34 | + public function testListenerIsInvokedBySigtermEvent(): void |
| 35 | + { |
| 36 | + $command = new DaemonCommandConcrete('test:daemon'); |
| 37 | + $input = new ArrayInput([]); |
| 38 | + $output = new BufferedOutput(); |
| 39 | + |
| 40 | + $event = new ConsoleSignalEvent($command, $input, $output, SIGTERM); |
| 41 | + |
| 42 | + $this->assertFalse($command->isShutdownRequested(), 'Shutdown should not be requested initially'); |
| 43 | + |
| 44 | + // Dispatch the event |
| 45 | + $this->dispatcher->dispatch($event, ConsoleSignalEvent::class); |
| 46 | + |
| 47 | + $this->assertTrue($command->isShutdownRequested(), 'Shutdown should be requested after event dispatch'); |
| 48 | + } |
| 49 | + |
| 50 | + public function testListenerIsInvokedBySigintEvent(): void |
| 51 | + { |
| 52 | + $command = new DaemonCommandConcrete('test:daemon'); |
| 53 | + $input = new ArrayInput([]); |
| 54 | + $output = new BufferedOutput(); |
| 55 | + |
| 56 | + $event = new ConsoleSignalEvent($command, $input, $output, SIGINT); |
| 57 | + |
| 58 | + $this->assertFalse($command->isShutdownRequested(), 'Shutdown should not be requested initially'); |
| 59 | + |
| 60 | + // Dispatch the event |
| 61 | + $this->dispatcher->dispatch($event, ConsoleSignalEvent::class); |
| 62 | + |
| 63 | + $this->assertTrue($command->isShutdownRequested(), 'Shutdown should be requested after event dispatch'); |
| 64 | + } |
| 65 | + |
| 66 | + public function testListenerIgnoresNonDaemonCommands(): void |
| 67 | + { |
| 68 | + $command = new Command('test:regular'); |
| 69 | + $input = new ArrayInput([]); |
| 70 | + $output = new BufferedOutput(); |
| 71 | + |
| 72 | + $event = new ConsoleSignalEvent($command, $input, $output, SIGTERM); |
| 73 | + |
| 74 | + // Should not throw any exception when dispatched |
| 75 | + $this->dispatcher->dispatch($event, ConsoleSignalEvent::class); |
| 76 | + |
| 77 | + // If we reach here, the test passes |
| 78 | + $this->assertTrue(true); |
| 79 | + } |
| 80 | + |
| 81 | + public function testListenerIgnoresUnhandledSignals(): void |
| 82 | + { |
| 83 | + $command = new DaemonCommandConcrete('test:daemon'); |
| 84 | + $input = new ArrayInput([]); |
| 85 | + $output = new BufferedOutput(); |
| 86 | + |
| 87 | + // Test with SIGHUP (not handled by the listener) |
| 88 | + $event = new ConsoleSignalEvent($command, $input, $output, SIGHUP); |
| 89 | + |
| 90 | + $this->assertFalse($command->isShutdownRequested(), 'Shutdown should not be requested initially'); |
| 91 | + |
| 92 | + // Dispatch the event |
| 93 | + $this->dispatcher->dispatch($event, ConsoleSignalEvent::class); |
| 94 | + |
| 95 | + $this->assertFalse($command->isShutdownRequested(), 'Shutdown should not be requested for unhandled signal'); |
| 96 | + } |
| 97 | + |
| 98 | + public function testMultipleSignalsCanBeHandled(): void |
| 99 | + { |
| 100 | + $command = new DaemonCommandConcrete('test:daemon'); |
| 101 | + $input = new ArrayInput([]); |
| 102 | + $output = new BufferedOutput(); |
| 103 | + |
| 104 | + // First, send an unhandled signal |
| 105 | + $event1 = new ConsoleSignalEvent($command, $input, $output, SIGHUP); |
| 106 | + $this->dispatcher->dispatch($event1, ConsoleSignalEvent::class); |
| 107 | + $this->assertFalse($command->isShutdownRequested(), 'Shutdown should not be requested after SIGHUP'); |
| 108 | + |
| 109 | + // Then send SIGTERM |
| 110 | + $event2 = new ConsoleSignalEvent($command, $input, $output, SIGTERM); |
| 111 | + $this->dispatcher->dispatch($event2, ConsoleSignalEvent::class); |
| 112 | + $this->assertTrue($command->isShutdownRequested(), 'Shutdown should be requested after SIGTERM'); |
| 113 | + } |
| 114 | + |
| 115 | + public function testListenerWorksWithMultipleListeners(): void |
| 116 | + { |
| 117 | + $called = false; |
| 118 | + |
| 119 | + // Add another listener to the same event |
| 120 | + $this->dispatcher->addListener(ConsoleSignalEvent::class, function(ConsoleSignalEvent $event) use (&$called) { |
| 121 | + $called = true; |
| 122 | + }); |
| 123 | + |
| 124 | + $command = new DaemonCommandConcrete('test:daemon'); |
| 125 | + $input = new ArrayInput([]); |
| 126 | + $output = new BufferedOutput(); |
| 127 | + |
| 128 | + $event = new ConsoleSignalEvent($command, $input, $output, SIGTERM); |
| 129 | + |
| 130 | + // Dispatch the event |
| 131 | + $this->dispatcher->dispatch($event, ConsoleSignalEvent::class); |
| 132 | + |
| 133 | + $this->assertTrue($command->isShutdownRequested(), 'Shutdown should be requested'); |
| 134 | + $this->assertTrue($called, 'Other listener should also be called'); |
| 135 | + } |
| 136 | +} |
| 137 | + |
0 commit comments