Skip to content

Attribute #[WithoutErrorHandler] to disable PHPUnit's error handler for a test method #5428

@jrfnl

Description

@jrfnl
Q A
PHPUnit version 10*
PHP version 8.2 (though irrelevant)
Installation Method Composer / PHAR (either)

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 return null.

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:

There were 2 failures:

1) Jrf\PHPUnit10\Scenario\Tests\FooTest::testMethodA
Failed asserting that exception message '' contains 'Failed to open stream'.

2) Jrf\PHPUnit10\Scenario\Tests\FooTest::testMethodB
Failed asserting that null is identical to 'Triggering'.

path/to/bug-report-reproduction-scenarios/tests/FooTest.php:22

--

2 tests triggered 2 PHP warnings:

1) Jrf\PHPUnit10\Scenario\Tests\FooTest::testMethodA
Trying to access array offset on value of type null
path/to/bug-report-reproduction-scenarios/src/Foo.php:17

path/to/bug-report-reproduction-scenarios/tests/FooTest.php:12

2) Jrf\PHPUnit10\Scenario\Tests\FooTest::testMethodB
Trying to access array offset on value of type null
path/to/bug-report-reproduction-scenarios/tests/FooTest.php:22

path/to/bug-report-reproduction-scenarios/tests/FooTest.php:21

--

1 test triggered 1 PHP deprecation:

1) Jrf\PHPUnit10\Scenario\Tests\FooTest::testMethodA
Exception::__construct(): Passing null to parameter #1 ($message) of type string is deprecated
path/to/bug-report-reproduction-scenarios/src/Foo.php:17

path/to/bug-report-reproduction-scenarios/tests/FooTest.php:12

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.

Metadata

Metadata

Labels

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions