Skip to content

Detect premature end of PHPUnit's main PHP process #6319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 14, 2025

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Aug 14, 2025

when a test calls exit() phpunit main process halted and printed nothing.
since PHPUnit 12.3 we have a ShutdownHandler which we utilize with this PR to handle this situation and print a more usefull error message.

before this PR:

➜  phpunit git:(11.5) ✗ php phpunit Test.php
PHPUnit 11.5.32-19-g48f14e340 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.3.24
Configuration: /Users/staabm/workspace/phpunit/phpunit.xml

after this PR:

➜  phpunit git:(11.5) ✗ php phpunit Test.php
PHPUnit 11.5.32-19-g48f14e340 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.3.24
Configuration: /Users/staabm/workspace/phpunit/phpunit.xml

Fatal error: Premature end of PHP process when running PHPUnit\TestFixture\Issue6234Test::testExitWithoutProcessIsolation.

similar to #6234
-> in a separate PR I will add expectation handling for subprocesses.

@@ -352,7 +352,12 @@ final public function run(): void
}

if (!$this->shouldRunInSeparateProcess() || $this->requirementsNotSatisfied()) {
(new TestRunner)->run($this);
try {
ShutdownHandler::setMessage('Fatal error: Premature end of PHP process.');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure whether the error is precise enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added the test-name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm maybe even PHPUnit process instead of PHP process?

staabm added 2 commits August 14, 2025 10:36
the test itself might not call exit() directly but it might be called from code down the line
@staabm staabm marked this pull request as ready for review August 14, 2025 08:40
@@ -39,6 +39,7 @@ private static function register(): void
return;
}

self::$registered = true;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PHPStan should have detected this problem: phpstan/phpstan#13384

$this->assertTrue(true);
$this->assertTrue(true);

exit(1);
Copy link
Contributor Author

@staabm staabm Aug 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just realized there is a even more critical case, namely a test calling exit(0), from non-isolated tests.
this would make PHPUnit return a 0 exit-code and noone would notice (besides the newly added error message), that tests did not run successfully.

stuff for a separate issue though. maybe even stuff for phpstan-phpunit as I can't think of how we can make phpunit return a non-zero exit code, when a test itself calls already exit(0).

@sebastianbergmann sebastianbergmann added type/enhancement A new idea that should be implemented feature/test-runner CLI test runner labels Aug 14, 2025
@sebastianbergmann sebastianbergmann merged commit 0a545f3 into sebastianbergmann:12.3 Aug 14, 2025
25 checks passed
@sebastianbergmann sebastianbergmann changed the title Detect premature end of phpunit main process Detect premature end of PHPUnit's main PHP process Aug 14, 2025
@staabm staabm deleted the premature-exit branch August 14, 2025 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/test-runner CLI test runner type/enhancement A new idea that should be implemented
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants