diff --git a/src/Runner/ShutdownHandler.php b/src/Runner/ShutdownHandler.php index faecf088c3..129ec9f868 100644 --- a/src/Runner/ShutdownHandler.php +++ b/src/Runner/ShutdownHandler.php @@ -10,6 +10,7 @@ namespace PHPUnit\Runner; use const PHP_EOL; +use function getmypid; use function register_shutdown_function; use function rtrim; @@ -42,13 +43,14 @@ private static function register(): void } self::$registered = true; + $pid = getmypid(); register_shutdown_function( - static function (): void + static function () use ($pid): void { $message = rtrim(self::$message); - if ($message === '') { + if ($message === '' || $pid !== getmypid()) { return; } diff --git a/tests/end-to-end/_files/ChildProcessTest.php b/tests/end-to-end/_files/ChildProcessTest.php new file mode 100644 index 0000000000..bf8270c819 --- /dev/null +++ b/tests/end-to-end/_files/ChildProcessTest.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\TestFixture; + +use function pcntl_fork; +use function pcntl_wait; +use PHPUnit\Framework\TestCase; + +final class ChildProcessTest extends TestCase +{ + public function testChildProcessOutput(): void + { + $child = pcntl_fork(); + $this->assertGreaterThan(-1, $child); + + if ($child) { + pcntl_wait($child); + $this->assertTrue(true); + } else { + exit(0); + } + } +} diff --git a/tests/end-to-end/cli/child-process-output.phpt b/tests/end-to-end/cli/child-process-output.phpt new file mode 100644 index 0000000000..3624916670 --- /dev/null +++ b/tests/end-to-end/cli/child-process-output.phpt @@ -0,0 +1,26 @@ +--TEST-- +ShutdownHandler does not output when child process exits +--SKIPIF-- +run($_SERVER['argv']); +--EXPECTF-- +PHPUnit %s by Sebastian Bergmann and contributors. + +Runtime: %s + +. 1 / 1 (100%) + +Time: %s, Memory: %s + +OK (1 test, 2 assertions)