-
-
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
Make registration of PHPUnit's error handler configurable #5686
Comments
@sebastianbergmann IMO we should always register/override the error handler by default, because:
I have thought about this and it is quite challenging to be meaningful this way. Do you want user handler to handle deprecations or any warnings in general when they have been successfully detected by PHPStan? I belive the solution is simply to always regesister the PHPUnit handler and when unit test does not need it, |
I thought in modern PHP it was a pretty common practice within a project to be converting For example, we had to migrate the following code for 9.x -> 10.x. I believe this can possibly now be undone but I think it's a good example scenario) which works with Previously it was just: private static function decimalFromString(string $value): Decimal
{
try {
return new Decimal($value, self::PRECISION);
} catch (\Throwable $exception) {
throw new \InvalidArgumentException(
\sprintf(
'Failed to construct decimal with value "%s" and precision %d: %s',
$value,
self::PRECISION,
$exception->getMessage(),
),
previous: $exception,
);
}
} To support PHPUnit's error handler replacing ours, we now have: private static function decimalFromString(string $value): Decimal
{
\set_error_handler(
function (int $errorNumber, string $errorString, string $errorFile, int $errorLine) use ($value): bool {
if ($errorNumber !== \E_WARNING) {
return false;
}
throw new \InvalidArgumentException(
\sprintf(
'Failed to construct decimal with value "%s" and precision %d: %s',
$value,
self::PRECISION,
$errorString,
),
);
},
);
try {
return new Decimal($value, self::PRECISION);
} catch (\DomainException $exception) {
throw new \InvalidArgumentException(
\sprintf(
'Failed to construct decimal with value "%s" and precision %d: %s',
$value,
self::PRECISION,
$exception->getMessage(),
),
);
} finally {
\restore_error_handler();
}
} (Constructing a Which actually is quite a bit slower at runtime, as this now needs error handler juggling every time a decimal value is constructed from a string. We cannot simply use You of course have the other case where you're testing a library and you aren't in control of runtime error handling setup. However even in this case it's difficult as you really want to be running your test suite twice, once with no error handler set up, and once with an error handler that converts warnings etc to exceptions and ensures you are handling both situations as consistently as possible. |
PHPUnit’s test runner uses
set_error_handler()
to register an error handler for handlingE_*
issues before each test and unregisters it after each test.When PHPUnit’s test runner becomes aware (after it called
set_error_handler()
to register its error handler) that another error handler was registered then it immediately unregisters its error handler so that the previously registered error handler remains active.I would like to keep the current behaviour (see above) as default (at least for PHPUnit 11), but explore the following configurable options:
The text was updated successfully, but these errors were encountered: