Skip to content

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

Merged
sebastianbergmann merged 4 commits intosebastianbergmann:12.3from
staabm:premature-exit
Aug 14, 2025
Merged

Detect premature end of PHPUnit's main PHP process#6319
sebastianbergmann merged 4 commits intosebastianbergmann:12.3from
staabm:premature-exit

Conversation

@staabm
Copy link
Copy Markdown
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.

Comment thread src/Framework/TestCase.php Outdated
if (!$this->shouldRunInSeparateProcess() || $this->requirementsNotSatisfied()) {
(new TestRunner)->run($this);
try {
ShutdownHandler::setMessage('Fatal error: Premature end of PHP process.');
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
return;
}

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

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
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
Copy Markdown
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
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 13, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

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