Skip to content
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

fix: Setting error handler inside an isolated test may cause test failure on PHP 8.3 #5677

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Util/PHP/Template/TestCaseClass.tpl
Expand Up @@ -66,6 +66,8 @@ function __phpunit_run_isolated_test()

ini_set('xdebug.scream', '0');

set_error_handler(static fn() => false);

// Not every STDOUT target stream is rewindable
@rewind(STDOUT);

Expand All @@ -79,6 +81,8 @@ function __phpunit_run_isolated_test()
}
}

restore_error_handler();

file_put_contents(
'{processResultFile}',
serialize(
Expand Down
6 changes: 5 additions & 1 deletion src/Util/PHP/Template/TestCaseMethod.tpl
Expand Up @@ -66,6 +66,8 @@ function __phpunit_run_isolated_test()

ini_set('xdebug.scream', '0');

set_error_handler(static fn() => false);

// Not every STDOUT target stream is rewindable
@rewind(STDOUT);

Expand All @@ -79,6 +81,8 @@ function __phpunit_run_isolated_test()
}
}

restore_error_handler();

file_put_contents(
'{processResultFile}',
serialize(
Expand All @@ -96,7 +100,7 @@ function __phpunit_run_isolated_test()

function __phpunit_error_handler($errno, $errstr, $errfile, $errline)
{
return true;
return true;
}

set_error_handler('__phpunit_error_handler');
Expand Down
21 changes: 21 additions & 0 deletions tests/end-to-end/regression/5595.phpt
@@ -0,0 +1,21 @@
--TEST--
https://github.com/sebastianbergmann/phpunit/issues/5595
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--no-configuration';
$_SERVER['argv'][] = '--process-isolation';
$_SERVER['argv'][] = __DIR__ . '/5595/Issue5595Test.php';

require_once __DIR__ . '/../../bootstrap.php';
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

Runtime: %s

. 1 / 1 (100%)

Time: %s, Memory: %s

OK (1 test, 1 assertion)
31 changes: 31 additions & 0 deletions tests/end-to-end/regression/5595/Issue5595Test.php
@@ -0,0 +1,31 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\TestFixture;

use function set_error_handler;
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
use PHPUnit\Framework\TestCase;

class Issue5595Test extends TestCase
{
protected function setUp(): void
{
set_error_handler(static function (): bool
{
return true;
});
}

#[RunInSeparateProcess]
public function test(): void
{
$this->assertTrue(true);
}
}