|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\CLI; |
| 15 | + |
| 16 | +use CodeIgniter\Test\CIUnitTestCase; |
| 17 | +use CodeIgniter\Test\StreamFilterTrait; |
| 18 | +use PHPUnit\Framework\Attributes\Group; |
| 19 | +use Tests\Support\Commands\Legacy\SignalCommandNoPcntl; |
| 20 | +use Tests\Support\Commands\Legacy\SignalCommandWindows; |
| 21 | + |
| 22 | +/** |
| 23 | + * Signal handling through the Windows console control handler. |
| 24 | + * |
| 25 | + * It does not need PCNTL, nor Windows: the handler is simulated, and the SIG* |
| 26 | + * constants are not used, as they do not exist on Windows. |
| 27 | + * |
| 28 | + * @internal |
| 29 | + */ |
| 30 | +#[Group('Others')] |
| 31 | +final class SignalWindowsTest extends CIUnitTestCase |
| 32 | +{ |
| 33 | + use StreamFilterTrait; |
| 34 | + |
| 35 | + private const SIGINT_NUMBER = 2; |
| 36 | + |
| 37 | + private SignalCommandWindows $command; |
| 38 | + |
| 39 | + protected function setUp(): void |
| 40 | + { |
| 41 | + $this->resetServices(); |
| 42 | + parent::setUp(); |
| 43 | + |
| 44 | + $this->command = new SignalCommandWindows(service('logger'), service('commands')); |
| 45 | + } |
| 46 | + |
| 47 | + protected function tearDown(): void |
| 48 | + { |
| 49 | + CLI::reset(); |
| 50 | + |
| 51 | + parent::tearDown(); |
| 52 | + } |
| 53 | + |
| 54 | + public function testRegisterAddsTheCtrlHandler(): void |
| 55 | + { |
| 56 | + $this->command->testRegisterSignals([]); |
| 57 | + |
| 58 | + $this->assertCount(1, $this->command->ctrlHandlerCalls); |
| 59 | + $this->assertTrue($this->command->ctrlHandlerCalls[0]['add']); |
| 60 | + $this->assertTrue($this->command->testHasSignals()); |
| 61 | + $this->assertSame([self::SIGINT_NUMBER], $this->command->testGetSignals()); |
| 62 | + } |
| 63 | + |
| 64 | + public function testRegisterWithTheInterruptSignal(): void |
| 65 | + { |
| 66 | + $this->command->testRegisterSignals([self::SIGINT_NUMBER], [self::SIGINT_NUMBER => 'testCustomHandler']); |
| 67 | + |
| 68 | + $this->assertCount(1, $this->command->ctrlHandlerCalls); |
| 69 | + $this->assertSame([self::SIGINT_NUMBER], $this->command->testGetSignals()); |
| 70 | + |
| 71 | + $state = $this->command->testGetProcessState(); |
| 72 | + $this->assertSame(1, $state['explicit_mappings']); |
| 73 | + $this->assertSame(['SIGINT'], $state['registered_signals_names']); |
| 74 | + } |
| 75 | + |
| 76 | + public function testRegisterIgnoresOtherSignals(): void |
| 77 | + { |
| 78 | + $this->command->testRegisterSignals([15, 1]); |
| 79 | + |
| 80 | + $this->assertSame([], $this->command->ctrlHandlerCalls); |
| 81 | + $this->assertFalse($this->command->testHasSignals()); |
| 82 | + } |
| 83 | + |
| 84 | + public function testRegisterTwiceAddsTheHandlerOnce(): void |
| 85 | + { |
| 86 | + $this->command->testRegisterSignals([]); |
| 87 | + $this->command->testRegisterSignals([self::SIGINT_NUMBER]); |
| 88 | + |
| 89 | + $this->assertCount(1, $this->command->ctrlHandlerCalls); |
| 90 | + $this->assertSame([self::SIGINT_NUMBER], $this->command->testGetSignals()); |
| 91 | + } |
| 92 | + |
| 93 | + public function testRegisterReportsAFailureToAddTheHandler(): void |
| 94 | + { |
| 95 | + $this->resetStreamFilterBuffer(); |
| 96 | + |
| 97 | + $this->command->ctrlHandlerSucceeds = false; |
| 98 | + $this->command->testRegisterSignals([]); |
| 99 | + |
| 100 | + $this->assertFalse($this->command->testHasSignals()); |
| 101 | + $this->assertStringContainsString('SIGINT', $this->getStreamFilterBuffer()); |
| 102 | + } |
| 103 | + |
| 104 | + public function testCtrlEventRequestsTermination(): void |
| 105 | + { |
| 106 | + $this->command->testRegisterSignals([]); |
| 107 | + |
| 108 | + $this->assertTrue($this->command->testIsRunning()); |
| 109 | + |
| 110 | + $this->command->testHandleWindowsCtrlEvent(0); |
| 111 | + |
| 112 | + $this->assertFalse($this->command->testIsRunning()); |
| 113 | + $this->assertTrue($this->command->testShouldTerminate()); |
| 114 | + } |
| 115 | + |
| 116 | + public function testCtrlBreakEventRequestsTermination(): void |
| 117 | + { |
| 118 | + $this->command->testRegisterSignals([]); |
| 119 | + $this->command->testHandleWindowsCtrlEvent(1); |
| 120 | + |
| 121 | + $this->assertTrue($this->command->testShouldTerminate()); |
| 122 | + } |
| 123 | + |
| 124 | + public function testCtrlEventCallsTheMappedHandler(): void |
| 125 | + { |
| 126 | + $this->command->testRegisterSignals([], [self::SIGINT_NUMBER => 'testCustomHandler']); |
| 127 | + |
| 128 | + $this->command->testHandleWindowsCtrlEvent(0); |
| 129 | + |
| 130 | + $this->assertTrue($this->command->customHandlerCalled); |
| 131 | + $this->assertFalse($this->command->fallbackHandlerCalled); |
| 132 | + $this->assertSame(self::SIGINT_NUMBER, $this->command->lastSignalReceived); |
| 133 | + } |
| 134 | + |
| 135 | + public function testCtrlEventFallsBackToOnInterruption(): void |
| 136 | + { |
| 137 | + $this->command->testRegisterSignals([]); |
| 138 | + |
| 139 | + $this->command->testHandleWindowsCtrlEvent(0); |
| 140 | + |
| 141 | + $this->assertFalse($this->command->customHandlerCalled); |
| 142 | + $this->assertTrue($this->command->fallbackHandlerCalled); |
| 143 | + $this->assertSame(self::SIGINT_NUMBER, $this->command->lastSignalReceived); |
| 144 | + } |
| 145 | + |
| 146 | + public function testUnregisterRemovesTheCtrlHandler(): void |
| 147 | + { |
| 148 | + $this->command->testRegisterSignals([], [self::SIGINT_NUMBER => 'testCustomHandler']); |
| 149 | + $this->command->testUnregisterSignals(); |
| 150 | + |
| 151 | + $this->assertCount(2, $this->command->ctrlHandlerCalls); |
| 152 | + $this->assertFalse($this->command->ctrlHandlerCalls[1]['add']); |
| 153 | + $this->assertSame($this->command->ctrlHandlerCalls[0]['handler'], $this->command->ctrlHandlerCalls[1]['handler']); |
| 154 | + $this->assertFalse($this->command->testHasSignals()); |
| 155 | + $this->assertSame([], $this->command->testGetSignals()); |
| 156 | + $this->assertSame(0, $this->command->testGetProcessState()['explicit_mappings']); |
| 157 | + } |
| 158 | + |
| 159 | + public function testUnregisterWithoutRegisteringDoesNothing(): void |
| 160 | + { |
| 161 | + $this->command->testUnregisterSignals(); |
| 162 | + |
| 163 | + $this->assertSame([], $this->command->ctrlHandlerCalls); |
| 164 | + } |
| 165 | + |
| 166 | + public function testRegisterAgainAfterUnregister(): void |
| 167 | + { |
| 168 | + $this->command->testRegisterSignals([]); |
| 169 | + $this->command->testUnregisterSignals(); |
| 170 | + $this->command->testRegisterSignals([]); |
| 171 | + |
| 172 | + $this->assertCount(3, $this->command->ctrlHandlerCalls); |
| 173 | + $this->assertTrue($this->command->ctrlHandlerCalls[2]['add']); |
| 174 | + $this->assertTrue($this->command->testHasSignals()); |
| 175 | + } |
| 176 | + |
| 177 | + public function testBlockingSignalsIsANoOp(): void |
| 178 | + { |
| 179 | + $result = $this->command->testWithSignalsBlocked(function (): string { |
| 180 | + $this->assertFalse($this->command->testSignalsBlocked()); |
| 181 | + |
| 182 | + return 'done'; |
| 183 | + }); |
| 184 | + |
| 185 | + $this->assertSame('done', $result); |
| 186 | + } |
| 187 | + |
| 188 | + public function testSignalNamesWithoutPcntl(): void |
| 189 | + { |
| 190 | + $this->assertSame('SIGINT', $this->command->testGetSignalName(self::SIGINT_NUMBER)); |
| 191 | + $this->assertSame('Signal 15', $this->command->testGetSignalName(15)); |
| 192 | + } |
| 193 | + |
| 194 | + public function testProcessStateOnWindows(): void |
| 195 | + { |
| 196 | + $this->command->testRegisterSignals([]); |
| 197 | + |
| 198 | + $state = $this->command->testGetProcessState(); |
| 199 | + |
| 200 | + $this->assertFalse($state['pcntl_available']); |
| 201 | + $this->assertSame(1, $state['registered_signals']); |
| 202 | + $this->assertArrayNotHasKey('session_id', $state); |
| 203 | + } |
| 204 | + |
| 205 | + public function testNothingIsRegisteredWithoutAnyHandlerSupport(): void |
| 206 | + { |
| 207 | + // Neither PCNTL nor a console control handler: Unix without PCNTL, or Windows |
| 208 | + // before sapi_windows_set_ctrl_handler() existed. |
| 209 | + $command = new SignalCommandNoPcntl(service('logger'), service('commands')); |
| 210 | + $command->testRegisterSignals([]); |
| 211 | + |
| 212 | + $this->assertFalse($command->testHasSignals()); |
| 213 | + } |
| 214 | +} |
0 commit comments