Skip to content

Commit 18d6ab6

Browse files
Closes #4793
1 parent 66178f5 commit 18d6ab6

6 files changed

Lines changed: 88 additions & 0 deletions

File tree

ChangeLog-12.5.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes of the PHPUnit 12.5 release series are documented in this fi
44

55
## [12.5.17] - 2026-MM-DD
66

7+
### Changed
8+
9+
* [#4793](https://github.com/sebastianbergmann/phpunit/issues/4793): Exit with non-zero exit code when `exit` was called from some test
10+
711
### Fixed
812

913
* [#6019](https://github.com/sebastianbergmann/phpunit/issues/6019): `--migrate-configuration` does not update schema location when XML content already validates against current schema

src/Runner/ShutdownHandler.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ static function () use ($pid): void
5555
}
5656

5757
print $message . PHP_EOL;
58+
59+
exit(2);
5860
},
5961
);
6062
}

tests/end-to-end/_files/WithExitTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,11 @@ public function testWithoutMessage(): void
2626

2727
exit(1);
2828
}
29+
30+
public function testWithExitZero(): void
31+
{
32+
$this->assertTrue(true);
33+
34+
exit(0);
35+
}
2936
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
Shutdown Handler: process exits with exit code 2
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
6+
$process = proc_open(
7+
[
8+
PHP_BINARY,
9+
__DIR__ . '/../../../phpunit',
10+
'--do-not-cache-result',
11+
'--no-configuration',
12+
'--filter',
13+
'testWithoutMessage',
14+
__DIR__ . '/../../_files/WithExitTest.php',
15+
],
16+
[
17+
1 => ['pipe', 'w'],
18+
2 => ['pipe', 'w'],
19+
],
20+
$pipes,
21+
);
22+
23+
stream_get_contents($pipes[1]);
24+
fclose($pipes[1]);
25+
26+
stream_get_contents($pipes[2]);
27+
fclose($pipes[2]);
28+
29+
$exitCode = proc_close($process);
30+
31+
print 'Exit code: ' . $exitCode . PHP_EOL;
32+
--EXPECT--
33+
Exit code: 2
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
Shutdown Handler: no output when message is reset before shutdown
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
6+
require __DIR__ . '/../../bootstrap.php';
7+
8+
PHPUnit\Runner\ShutdownHandler::setMessage('This should not be printed');
9+
PHPUnit\Runner\ShutdownHandler::resetMessage();
10+
11+
print 'done' . PHP_EOL;
12+
--EXPECT--
13+
done
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
Shutdown Handler: exit(0) still triggers shutdown message
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--do-not-cache-result';
6+
$_SERVER['argv'][] = '--no-configuration';
7+
$_SERVER['argv'][] = '--filter';
8+
$_SERVER['argv'][] = 'testWithExitZero';
9+
$_SERVER['argv'][] = __DIR__ . '/../_files/WithExitTest.php';
10+
11+
require __DIR__ . '/../../bootstrap.php';
12+
13+
// Destructs are called after register_shutdown_function callback
14+
$x = new class ()
15+
{
16+
function __destruct()
17+
{
18+
print '----';
19+
}
20+
};
21+
22+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
23+
--EXPECTF--
24+
PHPUnit %s by Sebastian Bergmann and contributors.
25+
26+
Runtime: %s
27+
28+
Fatal error: Premature end of PHP process when running PHPUnit\TestFixture\WithExitTest::testWithExitZero.
29+
----

0 commit comments

Comments
 (0)