-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Attribute #[WithoutErrorHandler]
to disable PHPUnit's error handler for a test method
#5428
Comments
The `error_get_last()` function can return `null` if no error occurred, in which case, the `throw new Exception()` statement will run into two new errors: * `Trying to access array offset on value of type null` for accessing `$error['message']`. * ... which then leads to a `Exception::__construct(): Passing null to parameter #1 ($message) of type string is deprecated`. This commit adds some defensive coding to handle the hypothetical situation that `error_get_last()` would return `null` when a file could not be opened. Note: this is actually a bug in PHPUnit 10 which breaks `error_get_last()`. We should be able to remove the extra defensive coding once the upstream bug has been fixed. Upstream bug report: sebastianbergmann/phpunit#5428
The `error_get_last()` function can return `null` if no error occurred, in which case, the `throw new Exception()` statement will run into two new errors: * `Trying to access array offset on value of type null` for accessing `$error['message']`. * ... which then leads to a `Exception::__construct(): Passing null to parameter #1 ($message) of type string is deprecated`. This commit adds some defensive coding to handle the hypothetical situation that `error_get_last()` would return `null` when a file could not be opened. Note: this is actually a bug in PHPUnit 10 which breaks `error_get_last()`. We should be able to remove the extra defensive coding once the upstream bug has been fixed. Upstream bug report: sebastianbergmann/phpunit#5428
I understand that this is a problem and I am sorry that it exists. However, supporting "code which relies on retrieving the last error message and acting on it" is a low priority for me. I will, of course, consider a pull request that aims to solve this problem (without negative impact for PHPUnit's functionality that is based on handling |
PHPUnit's error handler returns Without patch
Patchdiff --git a/src/Runner/ErrorHandler.php b/src/Runner/ErrorHandler.php
index 517f8472e..725eb0157 100644
--- a/src/Runner/ErrorHandler.php
+++ b/src/Runner/ErrorHandler.php
@@ -130,7 +130,7 @@ public function __invoke(int $errorNumber, string $errorString, string $errorFil
return false;
}
- return true;
+ return false;
}
public function enable(): void With patch
However ...
|
As I wrote before, I consider testing code that interacts with PHP's error handling (custom error handler, using That being said, I think there could be a "compromise" in the form of an option to disable PHPUnit's error handler. I am imagining a |
@sebastianbergmann Thanks for looking into this. I've applied a quick & dirty patch for the tests through which I discovered the issue. I think an attribute, like you are proposing, would be a cleaner solution and may also help other people running into this issue. |
error_get_last()
#[WithoutErrorHandler]
to disable PHPUnit's error handler for a test method
Summary
Code which relies on retrieving the last error message and acting on it can no longer be tested as
error_get_last()
will always returnnull
.This can also lead to additional PHP notices being reported by PHPUnit for errors which would never occur in a real-life situation.
Current behavior
Tests which are testing code which relies on
error_get_last()
are failing on PHPUnit 10 with notices along these lines:How to reproduce
Reproduction scenario available at: jrfnl/bug-report-reproduction-scenarios@b3d9366
GH actions build for the reproduction branch demonstrating the issue: https://github.com/jrfnl/bug-report-reproduction-scenarios/actions/runs/5378708821
Expected behavior
The
error_get_last()
will return the correct last error message so the code under test runs without problems and can be tested like before.The text was updated successfully, but these errors were encountered: