|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Enqueue\AmqpTools\Tests; |
| 4 | + |
| 5 | +use Enqueue\AmqpTools\SignalSocketHelper; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | + |
| 8 | +class SignalSocketHelperTest extends TestCase |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @var SignalSocketHelper |
| 12 | + */ |
| 13 | + private $signalHelper; |
| 14 | + |
| 15 | + private $backupSigTermHandler; |
| 16 | + |
| 17 | + private $backupSigIntHandler; |
| 18 | + |
| 19 | + public function setUp() |
| 20 | + { |
| 21 | + parent::setUp(); |
| 22 | + |
| 23 | + if (false == function_exists('pcntl_signal_get_handler')) { |
| 24 | + $this->markTestSkipped('PHP 7.1 and higher'); |
| 25 | + } |
| 26 | + |
| 27 | + $this->backupSigTermHandler = pcntl_signal_get_handler(SIGTERM); |
| 28 | + $this->backupSigIntHandler = pcntl_signal_get_handler(SIGINT); |
| 29 | + |
| 30 | + pcntl_signal(SIGTERM, SIG_DFL); |
| 31 | + pcntl_signal(SIGINT, SIG_DFL); |
| 32 | + |
| 33 | + $this->signalHelper = new SignalSocketHelper(); |
| 34 | + } |
| 35 | + |
| 36 | + public function tearDown() |
| 37 | + { |
| 38 | + parent::tearDown(); |
| 39 | + |
| 40 | + if ($this->signalHelper) { |
| 41 | + $this->signalHelper->afterSocket(); |
| 42 | + } |
| 43 | + |
| 44 | + if ($this->backupSigTermHandler) { |
| 45 | + pcntl_signal(SIGTERM, $this->backupSigTermHandler); |
| 46 | + } |
| 47 | + |
| 48 | + if ($this->backupSigIntHandler) { |
| 49 | + pcntl_signal(SIGINT, $this->backupSigIntHandler); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public function testShouldReturnFalseByDefault() |
| 54 | + { |
| 55 | + $this->assertFalse($this->signalHelper->wasThereSignal()); |
| 56 | + } |
| 57 | + |
| 58 | + public function testShouldRegisterHandlerOnBeforeSocket() |
| 59 | + { |
| 60 | + $this->signalHelper->beforeSocket(); |
| 61 | + |
| 62 | + $this->assertAttributeSame(false, 'wasThereSignal', $this->signalHelper); |
| 63 | + $this->assertAttributeSame([], 'handlers', $this->signalHelper); |
| 64 | + } |
| 65 | + |
| 66 | + public function testShouldRegisterHandlerOnBeforeSocketAndBackupCurrentOne() |
| 67 | + { |
| 68 | + $handler = function () {}; |
| 69 | + |
| 70 | + pcntl_signal(SIGTERM, $handler); |
| 71 | + |
| 72 | + $this->signalHelper->beforeSocket(); |
| 73 | + |
| 74 | + $this->assertAttributeSame(false, 'wasThereSignal', $this->signalHelper); |
| 75 | + |
| 76 | + $handlers = $this->readAttribute($this->signalHelper, 'handlers'); |
| 77 | + |
| 78 | + $this->assertInternalType('array', $handlers); |
| 79 | + $this->assertArrayHasKey(SIGTERM, $handlers); |
| 80 | + $this->assertSame($handler, $handlers[SIGTERM]); |
| 81 | + } |
| 82 | + |
| 83 | + public function testRestoreDefaultPropertiesOnAfterSocket() |
| 84 | + { |
| 85 | + $this->signalHelper->beforeSocket(); |
| 86 | + $this->signalHelper->afterSocket(); |
| 87 | + |
| 88 | + $this->assertAttributeSame(null, 'wasThereSignal', $this->signalHelper); |
| 89 | + $this->assertAttributeSame([], 'handlers', $this->signalHelper); |
| 90 | + } |
| 91 | + |
| 92 | + public function testRestorePreviousHandlerOnAfterSocket() |
| 93 | + { |
| 94 | + $handler = function () {}; |
| 95 | + |
| 96 | + pcntl_signal(SIGTERM, $handler); |
| 97 | + |
| 98 | + $this->signalHelper->beforeSocket(); |
| 99 | + $this->signalHelper->afterSocket(); |
| 100 | + |
| 101 | + $this->assertSame($handler, pcntl_signal_get_handler(SIGTERM)); |
| 102 | + } |
| 103 | + |
| 104 | + public function testThrowsIfBeforeSocketCalledSecondTime() |
| 105 | + { |
| 106 | + $this->signalHelper->beforeSocket(); |
| 107 | + |
| 108 | + $this->expectException(\LogicException::class); |
| 109 | + $this->expectExceptionMessage('The wasThereSignal property should be null but it is not. The afterSocket method might not have been called.'); |
| 110 | + $this->signalHelper->beforeSocket(); |
| 111 | + } |
| 112 | + |
| 113 | + public function testShouldReturnTrueOnWasThereSignal() |
| 114 | + { |
| 115 | + $this->signalHelper->beforeSocket(); |
| 116 | + |
| 117 | + posix_kill(getmypid(), SIGINT); |
| 118 | + pcntl_signal_dispatch(); |
| 119 | + |
| 120 | + $this->assertTrue($this->signalHelper->wasThereSignal()); |
| 121 | + |
| 122 | + $this->signalHelper->afterSocket(); |
| 123 | + } |
| 124 | +} |
0 commit comments