From 92cc4f145dd9a94831e0c91cb11e013705a8e512 Mon Sep 17 00:00:00 2001 From: Natsuki Ikeguchi Date: Tue, 23 Jan 2024 20:16:52 +0900 Subject: [PATCH 1/4] test: Add regression test for GH-5595 Signed-off-by: Natsuki Ikeguchi --- tests/end-to-end/regression/5595.phpt | 50 +++++++++++++++++++ .../regression/5595/Issue5595Test.php | 29 +++++++++++ 2 files changed, 79 insertions(+) create mode 100644 tests/end-to-end/regression/5595.phpt create mode 100644 tests/end-to-end/regression/5595/Issue5595Test.php diff --git a/tests/end-to-end/regression/5595.phpt b/tests/end-to-end/regression/5595.phpt new file mode 100644 index 00000000000..6f7281fa71a --- /dev/null +++ b/tests/end-to-end/regression/5595.phpt @@ -0,0 +1,50 @@ +--TEST-- +https://github.com/sebastianbergmann/phpunit/issues/5595 +--SKIPIF-- +')) { + print 'skip: PHP 8.3 is required.'; +} +--FILE-- +run($_SERVER['argv']); +--EXPECTF-- +PHPUnit %s by Sebastian Bergmann and contributors. + +Runtime: %s + +E 1 / 1 (100%) + +Time: %s, Memory: %s + +There was 1 error: + +1) PHPUnit\TestFixture\Issue5595Test::test +PHPUnit\Framework\Exception: PHP Fatal error: Uncaught PHPUnit\Event\Code\NoTestCaseObjectOnCallStackException: Cannot find TestCase object on call stack in %s%esrc%eEvent%eValue%eTest%eTestMethodBuilder.php:%d +Stack trace: +#0 %s%esrc%eRunner%eErrorHandler.php(%d): PHPUnit\Event\Code\TestMethodBuilder::fromCallStack() +#1 [internal function]: PHPUnit\Runner\ErrorHandler->__invoke(2, 'rewind(): Strea...', 'Standard input ...', %d) +#2 Standard input code(%d): rewind(Resource id #2) +#3 Standard input code(%d): __phpunit_run_isolated_test() +#4 {main} + thrown in %s%esrc%eEvent%eValue%eTest%eTestMethodBuilder.php on line %d + +Fatal error: Uncaught PHPUnit\Event\Code\NoTestCaseObjectOnCallStackException: Cannot find TestCase object on call stack in %s%esrc%eEvent%eValue%eTest%eTestMethodBuilder.php on line %d + +PHPUnit\Event\Code\NoTestCaseObjectOnCallStackException: Cannot find TestCase object on call stack in %s%esrc%eEvent%eValue%eTest%eTestMethodBuilder.php on line %d + +Call Stack: + %s + %s + %s + %s + %s + +ERRORS! +Tests: 1, Assertions: 0, Errors: 1. diff --git a/tests/end-to-end/regression/5595/Issue5595Test.php b/tests/end-to-end/regression/5595/Issue5595Test.php new file mode 100644 index 00000000000..1aec4a41d1c --- /dev/null +++ b/tests/end-to-end/regression/5595/Issue5595Test.php @@ -0,0 +1,29 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\TestFixture; + +use PHPUnit\Framework\Attributes\RunInSeparateProcess; +use PHPUnit\Framework\TestCase; + +class Issue5595Test extends TestCase +{ + public function setUp(): void + { + set_error_handler(function (): bool { + return true; + }); + } + + #[RunInSeparateProcess] + public function test(): void + { + $this->assertTrue(true); + } +} From 507e1d9402837fc127dd34f96246f2cc28c6e884 Mon Sep 17 00:00:00 2001 From: Natsuki Ikeguchi Date: Tue, 23 Jan 2024 19:55:30 +0900 Subject: [PATCH 2/4] fix: Setting error handler inside an isolated test may cause test failure on PHP 8.3 Signed-off-by: Natsuki Ikeguchi --- src/Util/PHP/Template/TestCaseClass.tpl | 4 +++ src/Util/PHP/Template/TestCaseMethod.tpl | 14 ++++++---- tests/end-to-end/regression/5595.phpt | 33 ++---------------------- 3 files changed, 15 insertions(+), 36 deletions(-) diff --git a/src/Util/PHP/Template/TestCaseClass.tpl b/src/Util/PHP/Template/TestCaseClass.tpl index f6d06695e0a..eba0a0d00e2 100644 --- a/src/Util/PHP/Template/TestCaseClass.tpl +++ b/src/Util/PHP/Template/TestCaseClass.tpl @@ -66,6 +66,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); @@ -79,6 +81,8 @@ function __phpunit_run_isolated_test() } } + restore_error_handler(); + file_put_contents( '{processResultFile}', serialize( diff --git a/src/Util/PHP/Template/TestCaseMethod.tpl b/src/Util/PHP/Template/TestCaseMethod.tpl index b9eaef7362b..bf395baf21b 100644 --- a/src/Util/PHP/Template/TestCaseMethod.tpl +++ b/src/Util/PHP/Template/TestCaseMethod.tpl @@ -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( @@ -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); @@ -79,6 +86,8 @@ function __phpunit_run_isolated_test() } } + restore_error_handler(); + file_put_contents( '{processResultFile}', serialize( @@ -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} diff --git a/tests/end-to-end/regression/5595.phpt b/tests/end-to-end/regression/5595.phpt index 6f7281fa71a..ca9adec0010 100644 --- a/tests/end-to-end/regression/5595.phpt +++ b/tests/end-to-end/regression/5595.phpt @@ -1,10 +1,5 @@ --TEST-- https://github.com/sebastianbergmann/phpunit/issues/5595 ---SKIPIF-- -')) { - print 'skip: PHP 8.3 is required.'; -} --FILE-- __invoke(2, 'rewind(): Strea...', 'Standard input ...', %d) -#2 Standard input code(%d): rewind(Resource id #2) -#3 Standard input code(%d): __phpunit_run_isolated_test() -#4 {main} - thrown in %s%esrc%eEvent%eValue%eTest%eTestMethodBuilder.php on line %d - -Fatal error: Uncaught PHPUnit\Event\Code\NoTestCaseObjectOnCallStackException: Cannot find TestCase object on call stack in %s%esrc%eEvent%eValue%eTest%eTestMethodBuilder.php on line %d - -PHPUnit\Event\Code\NoTestCaseObjectOnCallStackException: Cannot find TestCase object on call stack in %s%esrc%eEvent%eValue%eTest%eTestMethodBuilder.php on line %d - -Call Stack: - %s - %s - %s - %s - %s - -ERRORS! -Tests: 1, Assertions: 0, Errors: 1. +OK (1 test, 1 assertion) From 9dacf8310e26b3a141b018a796453bbc3b31fac8 Mon Sep 17 00:00:00 2001 From: Natsuki Ikeguchi Date: Tue, 23 Jan 2024 20:45:43 +0900 Subject: [PATCH 3/4] style: Fix CS Signed-off-by: Natsuki Ikeguchi --- tests/end-to-end/regression/5595/Issue5595Test.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/end-to-end/regression/5595/Issue5595Test.php b/tests/end-to-end/regression/5595/Issue5595Test.php index 1aec4a41d1c..59b64dec5ed 100644 --- a/tests/end-to-end/regression/5595/Issue5595Test.php +++ b/tests/end-to-end/regression/5595/Issue5595Test.php @@ -9,14 +9,16 @@ */ namespace PHPUnit\TestFixture; +use function set_error_handler; use PHPUnit\Framework\Attributes\RunInSeparateProcess; use PHPUnit\Framework\TestCase; class Issue5595Test extends TestCase { - public function setUp(): void + protected function setUp(): void { - set_error_handler(function (): bool { + set_error_handler(static function (): bool + { return true; }); } From a5f39084535c0d7d8811f64c64961c3867a0dfcf Mon Sep 17 00:00:00 2001 From: Natsuki Ikeguchi Date: Tue, 23 Jan 2024 21:31:04 +0900 Subject: [PATCH 4/4] refactor: Use closure for no-op error handlers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Michael Voříšek Signed-off-by: Natsuki Ikeguchi --- src/Util/PHP/Template/TestCaseClass.tpl | 2 +- src/Util/PHP/Template/TestCaseMethod.tpl | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Util/PHP/Template/TestCaseClass.tpl b/src/Util/PHP/Template/TestCaseClass.tpl index eba0a0d00e2..5350fe4990f 100644 --- a/src/Util/PHP/Template/TestCaseClass.tpl +++ b/src/Util/PHP/Template/TestCaseClass.tpl @@ -66,7 +66,7 @@ function __phpunit_run_isolated_test() ini_set('xdebug.scream', '0'); - set_error_handler('__phpunit_error_handler'); + set_error_handler(static fn() => false); // Not every STDOUT target stream is rewindable @rewind(STDOUT); diff --git a/src/Util/PHP/Template/TestCaseMethod.tpl b/src/Util/PHP/Template/TestCaseMethod.tpl index bf395baf21b..6c2f0bc71da 100644 --- a/src/Util/PHP/Template/TestCaseMethod.tpl +++ b/src/Util/PHP/Template/TestCaseMethod.tpl @@ -31,11 +31,6 @@ if ($composerAutoload) { require $phar; } -function __phpunit_error_handler($errno, $errstr, $errfile, $errline) -{ - return true; -} - function __phpunit_run_isolated_test() { $dispatcher = Facade::instance()->initForIsolation( @@ -71,7 +66,7 @@ function __phpunit_run_isolated_test() ini_set('xdebug.scream', '0'); - set_error_handler('__phpunit_error_handler'); + set_error_handler(static fn() => false); // Not every STDOUT target stream is rewindable @rewind(STDOUT); @@ -103,6 +98,11 @@ function __phpunit_run_isolated_test() ); } +function __phpunit_error_handler($errno, $errstr, $errfile, $errline) +{ + return true; +} + set_error_handler('__phpunit_error_handler'); {constants}