Skip to content

Commit b97c111

Browse files
staabmsebastianbergmann
authored andcommitted
Don't fiddle with php.ini settings at runtime
1 parent ddef4f9 commit b97c111

File tree

5 files changed

+80
-2
lines changed

5 files changed

+80
-2
lines changed

src/Framework/TestCase.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use function getcwd;
2727
use function implode;
2828
use function in_array;
29+
use function ini_get;
2930
use function ini_set;
3031
use function is_array;
3132
use function is_callable;
@@ -83,6 +84,7 @@
8384
use PHPUnit\Runner\BackedUpEnvironmentVariable;
8485
use PHPUnit\Runner\DeprecationCollector\Facade as DeprecationCollector;
8586
use PHPUnit\Runner\HookMethodCollection;
87+
use PHPUnit\Runner\ShutdownHandler;
8688
use PHPUnit\TestRunner\TestResult\PassedTests;
8789
use PHPUnit\TextUI\Configuration\Registry as ConfigurationRegistry;
8890
use PHPUnit\Util\Exporter;
@@ -1297,7 +1299,10 @@ private function runTest(): mixed
12971299

12981300
$capture = tmpfile();
12991301
assert($capture !== false);
1300-
ini_set('display_errors', 1);
1302+
1303+
if (ini_get('display_errors') === '0') {
1304+
ShutdownHandler::setMessage('Fatal error: Premature end of PHP process. Use display_errors=On to see the error message.');
1305+
}
13011306
$errorLogPrevious = ini_set('error_log', stream_get_meta_data($capture)['uri']);
13021307

13031308
try {
@@ -1332,6 +1337,7 @@ private function runTest(): mixed
13321337

13331338
return null;
13341339
} finally {
1340+
ShutdownHandler::resetMessage();
13351341
fclose($capture);
13361342

13371343
ini_set('error_log', $errorLogPrevious);

src/Framework/TestRunner/TestRunner.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use PHPUnit\Runner\CodeCoverage;
2525
use PHPUnit\Runner\ErrorHandler;
2626
use PHPUnit\Runner\Exception;
27+
use PHPUnit\Runner\ShutdownHandler;
2728
use PHPUnit\TextUI\Configuration\Configuration;
2829
use PHPUnit\TextUI\Configuration\Registry as ConfigurationRegistry;
2930
use SebastianBergmann\CodeCoverage\Exception as CodeCoverageException;
@@ -83,6 +84,7 @@ public function run(TestCase $test): void
8384
if ($this->shouldErrorHandlerBeUsed($test)) {
8485
ErrorHandler::instance()->enable();
8586
}
87+
ShutdownHandler::register();
8688

8789
$collectCodeCoverage = CodeCoverage::instance()->isActive() &&
8890
$shouldCodeCoverageBeCollected;

src/Runner/ShutdownHandler.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\Runner;
11+
12+
use function register_shutdown_function;
13+
14+
/**
15+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
16+
*
17+
* @internal This class is not covered by the backward compatibility promise for PHPUnit
18+
*/
19+
final class ShutdownHandler
20+
{
21+
public static bool $registered = false;
22+
private static string $message = '';
23+
24+
public static function setMessage(string $message): void
25+
{
26+
self::$message = $message;
27+
}
28+
29+
public static function resetMessage(): void
30+
{
31+
self::$message = '';
32+
}
33+
34+
public static function register(): void
35+
{
36+
if (self::$registered) {
37+
return;
38+
}
39+
40+
register_shutdown_function(static function (): void
41+
{
42+
print self::$message;
43+
});
44+
}
45+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Silent failure of PHP fatal errors
3+
4+
https://github.com/sebastianbergmann/phpunit/issues/6294
5+
--SKIPIF--
6+
<?php declare(strict_types=1);
7+
if (version_compare(PHP_VERSION, '8.5.0-dev', '>=')) {
8+
print 'skip: PHP < 8.5 is required.';
9+
}
10+
--INI--
11+
display_errors=0
12+
--FILE--
13+
<?php declare(strict_types=1);
14+
$_SERVER['argv'][] = '--do-not-cache-result';
15+
$_SERVER['argv'][] = '--no-configuration';
16+
$_SERVER['argv'][] = __DIR__ . '/6294/IssueTest6294.php';
17+
18+
require_once __DIR__ . '/../../bootstrap.php';
19+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
20+
--EXPECTF--
21+
PHPUnit %s by Sebastian Bergmann and contributors.
22+
23+
Runtime: %s
24+
25+
Fatal error: Premature end of PHP process. Use display_errors=On to see the error message.

tests/end-to-end/regression/6294.phpt renamed to tests/end-to-end/regression/6294-display-errors-on.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ if (version_compare(PHP_VERSION, '8.5.0-dev', '>=')) {
88
print 'skip: PHP < 8.5 is required.';
99
}
1010
--INI--
11-
display_errors=0
11+
display_errors=1
1212
--FILE--
1313
<?php declare(strict_types=1);
1414
$_SERVER['argv'][] = '--do-not-cache-result';

0 commit comments

Comments
 (0)