|
14 | 14 | use PHPUnit\Framework\TestCase;
|
15 | 15 | use Symfony\Component\Console\Application;
|
16 | 16 | use Symfony\Component\Console\Command\Command;
|
| 17 | +use Symfony\Component\Console\Command\SignalableCommandInterface; |
17 | 18 | use Symfony\Component\Console\CommandLoader\FactoryCommandLoader;
|
18 | 19 | use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass;
|
19 | 20 | use Symfony\Component\Console\Event\ConsoleCommandEvent;
|
@@ -1808,6 +1809,39 @@ public function testCommandNameMismatchWithCommandLoaderKeyThrows()
|
1808 | 1809 | $app->setCommandLoader($loader);
|
1809 | 1810 | $app->get('test');
|
1810 | 1811 | }
|
| 1812 | + |
| 1813 | + /** |
| 1814 | + * @requires extension pcntl |
| 1815 | + */ |
| 1816 | + public function testSignal() |
| 1817 | + { |
| 1818 | + $command = new SignableCommand(); |
| 1819 | + |
| 1820 | + $dispatcherCalled = false; |
| 1821 | + $dispatcher = new EventDispatcher(); |
| 1822 | + $dispatcher->addListener('console.signal', function () use (&$dispatcherCalled) { |
| 1823 | + $dispatcherCalled = true; |
| 1824 | + }); |
| 1825 | + |
| 1826 | + $application = new Application(); |
| 1827 | + $application->setAutoExit(false); |
| 1828 | + $application->setDispatcher($dispatcher); |
| 1829 | + $application->setSignalsToDispatchEvent(SIGALRM); |
| 1830 | + $application->add($command); |
| 1831 | + |
| 1832 | + $this->assertFalse($command->signaled); |
| 1833 | + $this->assertFalse($dispatcherCalled); |
| 1834 | + |
| 1835 | + $this->assertSame(0, $application->run(new ArrayInput(['signal']))); |
| 1836 | + $this->assertFalse($command->signaled); |
| 1837 | + $this->assertFalse($dispatcherCalled); |
| 1838 | + |
| 1839 | + $command->loop = 100000; |
| 1840 | + pcntl_alarm(1); |
| 1841 | + $this->assertSame(1, $application->run(new ArrayInput(['signal']))); |
| 1842 | + $this->assertTrue($command->signaled); |
| 1843 | + $this->assertTrue($dispatcherCalled); |
| 1844 | + } |
1811 | 1845 | }
|
1812 | 1846 |
|
1813 | 1847 | class CustomApplication extends Application
|
@@ -1865,3 +1899,33 @@ public function isEnabled(): bool
|
1865 | 1899 | return false;
|
1866 | 1900 | }
|
1867 | 1901 | }
|
| 1902 | + |
| 1903 | +class SignableCommand extends Command implements SignalableCommandInterface |
| 1904 | +{ |
| 1905 | + public $signaled = false; |
| 1906 | + public $loop = 100; |
| 1907 | + |
| 1908 | + protected static $defaultName = 'signal'; |
| 1909 | + |
| 1910 | + public function getSubscribedSignals(): array |
| 1911 | + { |
| 1912 | + return [SIGALRM]; |
| 1913 | + } |
| 1914 | + |
| 1915 | + public function handleSignal(int $signal): void |
| 1916 | + { |
| 1917 | + $this->signaled = true; |
| 1918 | + } |
| 1919 | + |
| 1920 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 1921 | + { |
| 1922 | + for ($i = 0; $i < $this->loop; ++$i) { |
| 1923 | + usleep(100); |
| 1924 | + if ($this->signaled) { |
| 1925 | + return 1; |
| 1926 | + } |
| 1927 | + } |
| 1928 | + |
| 1929 | + return 0; |
| 1930 | + } |
| 1931 | +} |
0 commit comments