From c77433ddc6cdc689caf48065d9ea22ca0853fbd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Fri, 24 Feb 2023 13:02:50 -0500 Subject: [PATCH] [Console] Fix ApplicationTest::testSetSignalsToDispatchEvent() when ran alone --- Tests/ApplicationTest.php | 43 +++++++++++++++---- ...on.dont_run_alternative_namespace_name.txt | 8 ++++ 2 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 Tests/Fixtures/application.dont_run_alternative_namespace_name.txt diff --git a/Tests/ApplicationTest.php b/Tests/ApplicationTest.php index b331c665b..698a9679b 100644 --- a/Tests/ApplicationTest.php +++ b/Tests/ApplicationTest.php @@ -64,6 +64,16 @@ protected function tearDown(): void putenv('SHELL_VERBOSITY'); unset($_ENV['SHELL_VERBOSITY']); unset($_SERVER['SHELL_VERBOSITY']); + + if (\function_exists('pcntl_signal')) { + // We reset all signals to their default value to avoid side effects + for ($i = 1; $i <= 15; ++$i) { + if (9 === $i) { + continue; + } + pcntl_signal($i, SIG_DFL); + } + } } public static function setUpBeforeClass(): void @@ -508,15 +518,7 @@ public function testDontRunAlternativeNamespaceName() $application->setAutoExit(false); $tester = new ApplicationTester($application); $tester->run(['command' => 'foos:bar1'], ['decorated' => false]); - $this->assertSame(' - - There are no commands defined in the "foos" namespace. - - Did you mean this? - foo - - -', $tester->getDisplay(true)); + $this->assertStringEqualsFile(self::$fixturesPath.'/application.dont_run_alternative_namespace_name.txt', $tester->getDisplay(true)); } public function testCanRunAlternativeCommandName() @@ -1956,15 +1958,38 @@ public function testSetSignalsToDispatchEvent() $dispatcher = new EventDispatcher(); $dispatcher->addSubscriber($subscriber); + // Since there is no signal handler, and by default PHP will stop even + // on SIGUSR1, we need to register a blank handler to avoid the process + // being stopped. + $blankHandlerSignaled = false; + pcntl_signal(\SIGUSR1, function () use (&$blankHandlerSignaled) { + $blankHandlerSignaled = true; + }); + $application = $this->createSignalableApplication($command, $dispatcher); $application->setSignalsToDispatchEvent(\SIGUSR2); $this->assertSame(0, $application->run(new ArrayInput(['signal']))); $this->assertFalse($subscriber->signaled); + $this->assertTrue($blankHandlerSignaled); + + // We reset the blank handler to false to make sure it is called again + $blankHandlerSignaled = false; + + $application = $this->createSignalableApplication($command, $dispatcher); + $application->setSignalsToDispatchEvent(\SIGUSR1); + $this->assertSame(1, $application->run(new ArrayInput(['signal']))); + $this->assertTrue($subscriber->signaled); + $this->assertTrue($blankHandlerSignaled); + + // And now we test without the blank handler + $blankHandlerSignaled = false; + pcntl_signal(\SIGUSR1, SIG_DFL); $application = $this->createSignalableApplication($command, $dispatcher); $application->setSignalsToDispatchEvent(\SIGUSR1); $this->assertSame(1, $application->run(new ArrayInput(['signal']))); $this->assertTrue($subscriber->signaled); + $this->assertFalse($blankHandlerSignaled); } public function testSignalableCommandInterfaceWithoutSignals() diff --git a/Tests/Fixtures/application.dont_run_alternative_namespace_name.txt b/Tests/Fixtures/application.dont_run_alternative_namespace_name.txt new file mode 100644 index 000000000..430edde20 --- /dev/null +++ b/Tests/Fixtures/application.dont_run_alternative_namespace_name.txt @@ -0,0 +1,8 @@ + + + There are no commands defined in the "foos" namespace. + + Did you mean this? + foo + +