From 4fe48ed51463255010b8c03dc6aa75dedd0982f4 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Mon, 22 Jan 2024 15:10:32 +0100 Subject: [PATCH] Closes #5676 --- ChangeLog-10.5.md | 7 +++ src/Runner/ErrorHandler.php | 8 +++- .../bootstrap.php | 16 +++++++ .../phpunit.xml | 11 +++++ .../tests/ExampleTest.php | 22 ++++++++++ ...otstrap-is-not-overwritten-by-phpunit.phpt | 44 +++++++++++++++++++ 6 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 tests/end-to-end/event/_files/custom-error-handler-registered-in-bootstrap-is-not-overwritten-by-phpunit/bootstrap.php create mode 100644 tests/end-to-end/event/_files/custom-error-handler-registered-in-bootstrap-is-not-overwritten-by-phpunit/phpunit.xml create mode 100644 tests/end-to-end/event/_files/custom-error-handler-registered-in-bootstrap-is-not-overwritten-by-phpunit/tests/ExampleTest.php create mode 100644 tests/end-to-end/event/custom-error-handler-registered-in-bootstrap-is-not-overwritten-by-phpunit.phpt diff --git a/ChangeLog-10.5.md b/ChangeLog-10.5.md index 067699db94d..528e5270765 100644 --- a/ChangeLog-10.5.md +++ b/ChangeLog-10.5.md @@ -2,6 +2,12 @@ All notable changes of the PHPUnit 10.5 release series are documented in this file using the [Keep a CHANGELOG](https://keepachangelog.com/) principles. +## [10.5.9] - 2024-MM-DD + +### Fixed + +* [#5676](https://github.com/sebastianbergmann/phpunit/issues/5676): PHPUnit's test runner overwrites custom error handler registered using `set_error_handler()` in bootstrap script + ## [10.5.8] - 2024-01-19 ### Fixed @@ -91,6 +97,7 @@ All notable changes of the PHPUnit 10.5 release series are documented in this fi * [#5563](https://github.com/sebastianbergmann/phpunit/issues/5563): `createMockForIntersectionOfInterfaces()` does not automatically register mock object for expectation verification +[10.5.9]: https://github.com/sebastianbergmann/phpunit/compare/10.5.8...10.5 [10.5.8]: https://github.com/sebastianbergmann/phpunit/compare/10.5.7...10.5.8 [10.5.7]: https://github.com/sebastianbergmann/phpunit/compare/10.5.6...10.5.7 [10.5.6]: https://github.com/sebastianbergmann/phpunit/compare/10.5.5...10.5.6 diff --git a/src/Runner/ErrorHandler.php b/src/Runner/ErrorHandler.php index ea3acfa5d29..e1c5b8a72b9 100644 --- a/src/Runner/ErrorHandler.php +++ b/src/Runner/ErrorHandler.php @@ -166,7 +166,13 @@ public function enable(): void return; } - set_error_handler($this); + $oldErrorHandler = set_error_handler($this); + + if ($oldErrorHandler !== null) { + restore_error_handler(); + + return; + } $this->enabled = true; $this->originalErrorReportingLevel = error_reporting(); diff --git a/tests/end-to-end/event/_files/custom-error-handler-registered-in-bootstrap-is-not-overwritten-by-phpunit/bootstrap.php b/tests/end-to-end/event/_files/custom-error-handler-registered-in-bootstrap-is-not-overwritten-by-phpunit/bootstrap.php new file mode 100644 index 00000000000..e0902853e18 --- /dev/null +++ b/tests/end-to-end/event/_files/custom-error-handler-registered-in-bootstrap-is-not-overwritten-by-phpunit/bootstrap.php @@ -0,0 +1,16 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\TestFixture\Event\ErrorHandlerIsNotOverwritten; + +set_error_handler( + static function (int $errorNumber, string $errorString, string $errorFile, int $errorLine): bool { + return true; + } +); diff --git a/tests/end-to-end/event/_files/custom-error-handler-registered-in-bootstrap-is-not-overwritten-by-phpunit/phpunit.xml b/tests/end-to-end/event/_files/custom-error-handler-registered-in-bootstrap-is-not-overwritten-by-phpunit/phpunit.xml new file mode 100644 index 00000000000..0e0f270cea9 --- /dev/null +++ b/tests/end-to-end/event/_files/custom-error-handler-registered-in-bootstrap-is-not-overwritten-by-phpunit/phpunit.xml @@ -0,0 +1,11 @@ + + + + + tests + + + diff --git a/tests/end-to-end/event/_files/custom-error-handler-registered-in-bootstrap-is-not-overwritten-by-phpunit/tests/ExampleTest.php b/tests/end-to-end/event/_files/custom-error-handler-registered-in-bootstrap-is-not-overwritten-by-phpunit/tests/ExampleTest.php new file mode 100644 index 00000000000..e8dcb78a823 --- /dev/null +++ b/tests/end-to-end/event/_files/custom-error-handler-registered-in-bootstrap-is-not-overwritten-by-phpunit/tests/ExampleTest.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\TestFixture\Event\ErrorHandlerIsNotOverwritten; + +use PHPUnit\Framework\TestCase; + +final class ExampleTest extends TestCase +{ + public function testOne(): void + { + trigger_error('message', E_USER_DEPRECATED); + + $this->assertTrue(true); + } +} diff --git a/tests/end-to-end/event/custom-error-handler-registered-in-bootstrap-is-not-overwritten-by-phpunit.phpt b/tests/end-to-end/event/custom-error-handler-registered-in-bootstrap-is-not-overwritten-by-phpunit.phpt new file mode 100644 index 00000000000..8f4cd00ebad --- /dev/null +++ b/tests/end-to-end/event/custom-error-handler-registered-in-bootstrap-is-not-overwritten-by-phpunit.phpt @@ -0,0 +1,44 @@ +--TEST-- +A custom error handler registered in the test suite's bootstrap script using set_error_handler() is not overwritten by PHPUnit by default +--FILE-- +run($_SERVER['argv']); + +print file_get_contents($traceFile); + +unlink($traceFile); +--EXPECTF-- +PHPUnit Started (PHPUnit %s using %s) +Test Runner Configured +Bootstrap Finished (%sbootstrap.php) +Test Suite Loaded (1 test) +Event Facade Sealed +Test Runner Started +Test Suite Sorted +Test Runner Execution Started (1 test) +Test Suite Started (%sphpunit.xml, 1 test) +Test Suite Started (default, 1 test) +Test Suite Started (PHPUnit\TestFixture\Event\ErrorHandlerIsNotOverwritten\ExampleTest, 1 test) +Test Preparation Started (PHPUnit\TestFixture\Event\ErrorHandlerIsNotOverwritten\ExampleTest::testOne) +Test Prepared (PHPUnit\TestFixture\Event\ErrorHandlerIsNotOverwritten\ExampleTest::testOne) +Assertion Succeeded (Constraint: is true, Value: true) +Test Passed (PHPUnit\TestFixture\Event\ErrorHandlerIsNotOverwritten\ExampleTest::testOne) +Test Finished (PHPUnit\TestFixture\Event\ErrorHandlerIsNotOverwritten\ExampleTest::testOne) +Test Suite Finished (PHPUnit\TestFixture\Event\ErrorHandlerIsNotOverwritten\ExampleTest, 1 test) +Test Suite Finished (default, 1 test) +Test Suite Finished (%sphpunit.xml, 1 test) +Test Runner Execution Finished +Test Runner Finished +PHPUnit Finished (Shell Exit Code: 0)