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 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ function __phpunit_run_isolated_test()

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

set_error_handler('__phpunit_error_handler');
Copy link
Contributor

@mvorisek mvorisek Jan 23, 2024

Choose a reason for hiding this comment

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

Suggested change
set_error_handler('__phpunit_error_handler');
set_error_handler(static fn() => false);

use anonymous function

and return false to keep error_get_last() set, the PHP error handlers does not support nesting, @ should supress any PHP output

Copy link
Author

@siketyan siketyan Jan 23, 2024

Choose a reason for hiding this comment

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

Fixed in a5f3908.


// 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
14 changes: 9 additions & 5 deletions src/Util/PHP/Template/TestCaseMethod.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ if ($composerAutoload) {
require $phar;
}

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

function __phpunit_run_isolated_test()
{
$dispatcher = Facade::instance()->initForIsolation(
Expand Down Expand Up @@ -66,6 +71,8 @@ function __phpunit_run_isolated_test()

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

set_error_handler('__phpunit_error_handler');

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

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

restore_error_handler();

file_put_contents(
'{processResultFile}',
serialize(
Expand All @@ -94,11 +103,6 @@ function __phpunit_run_isolated_test()
);
}

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

set_error_handler('__phpunit_error_handler');

{constants}
Expand Down
21 changes: 21 additions & 0 deletions tests/end-to-end/regression/5595.phpt
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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);
}
}