-
-
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
Optionally ignore PHPUnit deprecations when determining the test runner's shell exit code #5937
Comments
As the PHPUnit Polyfills, as of now, will officially support PHPUnit 11.x, with the exception of the TestListeners, the GH Actions workflow should be updated to reflect this. This commit: * Move the PHP 8.2/8.3 "high" (auto) PHPUnit version builds out of the matrix and run these without code coverage (see below). * Add builds for PHP 8.2 and 8.3 against low PHPUnit 11 versions for the Composer based tests. * Add builds for PHP 8.2 and 8.3 against high/low PHPUnit 11 for the PHAR based tests. * Add an extra experimental build in both test workflows against PHP "nightly" to ensure both PHPUnit 9.x, 10.x, as well as PHPUnit 11.x are tested with PHP 8.4. * Updates the experimental build against "future" PHPUnit to always run against the latest official PHP release. **Regarding PHPUnit 11 and running code coverage**: Since PHPUnit 10, PHPUnit does not distinguish between PHPUnit and PHP deprecation notices anymore. This means that when `failOnDeprecation` is enabled (as is done for this library to be ready early for new PHP versions), a test run will also fail if there are PHPUnit native deprecation notices. Now PHPUnit 11.2 deprecated the use of `#[CoversClass]` for traits and introduced a `#[CoversTrait]` attribute to replace this. However, it is currently impossible to action this deprecation notice in a PHPUnit cross-version compatible manner. This has been reported upstream and until that issue has been addressed in PHPUnit itself, the net-effect of this issue is that we can run the tests with code coverage on PHPUnit < 11.2, but not on PHPUnit 11.2 or higher. Ref: * sebastianbergmann/phpunit#5937
failOnDeprecation
setting can not be used (concrete use case inside)
Thank you for taking the time to research this issue and for explaining it so thoroughly. I only wish you had brought this up sooner, as my deliberate choice you mention was obviously (just not to me) wrong. The next versions of PHPUnit 10.5 and PHPUnit 11.3 will have a separation between PHPUnit deprecations and
|
@sebastianbergmann Sorry for that, but seeing that other people's issues about this were being dismissed, I didn't think opening yet another issue about this would be useful until there was a situation which could potentially convince you 😕
💞 🎉 Thank you! |
Summary
Use case
Given a test suite which:
How can I make the tests pass and code coverage be recorded on both PHPUnit 10 + 11 ?
The underlying problem
This is actually part of a larger problem which has existed since PHPUnit 10.0: deprecations coming from PHPUnit itself affecting the exit code.
Now, I know this was a deliberate choice (based on comments by @sebastianbergmann in various tickets over time) and as there have been work-arounds until now, I have not spoken up about this before, but with the deprecation of
#[CoversClass]
in favour of#[CoversTrait]
this is now an insurmountable problem.The typical use-case for the
failOn*
settings is (open source) libraries which need to run on multiple PHP versions and want to prevent (new) PHP deprecations/notices/warnings from creeping into their code.Up to now, the fact that PHPUnit deprecations affected the exit code meant that those libraries would need:
In contrast to the previous (PHPUnit <= 9) behaviour where those deprecations could be ignored until the next major PHPUnit release.
--migration-configuration
step in the CI (withcontinue-on-error
) just to be sure that their test run would not fail on an outdated (cross-version compatible) configuration.convertDeprecationsToExceptions
conversion todisplayDetailsOnTestsThatTriggerDeprecations
+failOnDeprecation
.To me, the fact that PHPUnit deprecations affect the exit code is in stark contrast to the changes made in PHPUnit 9.5.x to no longer fail tests on PHP deprecation notices by default and in PHPUnit 10 to do the same for PHP warnings and notices.
The argument for that change was that most packages should be able to ignore those deprecations (notices/warnings) until the next PHP major, so these notices should not be a reason to fail the tests.
By that same logic, I should be able to ignore PHPUnit deprecation notices until the next PHPUnit major, but I no longer can if I do want to see PHP deprecation notices.
Current behavior
As things are, the test run will always fail on PHPUnit 11.x due to the PHPUnit deprecation notice "Targeting a trait such as * with #[CoversClass] is deprecated, please refactor your test to use #[CoversTrait] instead." and exit with exit code 1.
How to reproduce
I've created a reproduction sample here: https://github.com/jrfnl/bug-report-reproduction-scenarios/commits/phpunit/5937-fail-on-deprecation/
CI run on the original test code: https://github.com/jrfnl/bug-report-reproduction-scenarios/actions/runs/10683893211
Things I've considered/tried to see if I could find a work-around (also available as commits in the reproduction sample):
#[CoversTrait]
(for PHPUnit 11) and#[CoversClass]
(for PHPUnit 10).Result: ❌ still getting the
#[CoversClass]
deprecation notice on PHPUnit 11, tests can no longer run on PHPUnit 10 as it fails hard on a "Attribute class not found" fatal.Commit: jrfnl/bug-report-reproduction-scenarios@dab7ee5
CI run: https://github.com/jrfnl/bug-report-reproduction-scenarios/actions/runs/10683909438
#[CoversClass]
with#[CoversTrait]
.Result: ❌ tests can no longer run on PHPUnit 10 as it fails hard on a "Attribute class not found" fatal.
Commit: jrfnl/bug-report-reproduction-scenarios@01d0beb
CI run: https://github.com/jrfnl/bug-report-reproduction-scenarios/actions/runs/10683924693
ignore*Deprecations
XML attributes in the config file.Result: ❌ PHPUnit 10 fails due to invalid configuration, PHPUnit 11 still fails on the
#[CoversClass]
deprecation notice.Commit: jrfnl/bug-report-reproduction-scenarios@3c51567
CI run: https://github.com/jrfnl/bug-report-reproduction-scenarios/actions/runs/10683935620
Commit: jrfnl/bug-report-reproduction-scenarios@d680037
CI run: https://github.com/jrfnl/bug-report-reproduction-scenarios/actions/runs/10683946857
Commit: jrfnl/bug-report-reproduction-scenarios@cc0c671
CI run: https://github.com/jrfnl/bug-report-reproduction-scenarios/actions/runs/10683957942
Commit: jrfnl/bug-report-reproduction-scenarios@f8eabfb
CI run: https://github.com/jrfnl/bug-report-reproduction-scenarios/actions/runs/10683968001
@
silence operator to the#[CoversClass]
attribute.Result: ❌ not supported by PHP, syntax error. Tried both
@#[CoversClass]
and#[@CoversClass]
.Commit: jrfnl/bug-report-reproduction-scenarios@bce2fe0
CI run: https://github.com/jrfnl/bug-report-reproduction-scenarios/actions/runs/10683977200
Result: ❌ not supported by PHP, syntax error.
Commit: jrfnl/bug-report-reproduction-scenarios@1e433e1
CI run: https://github.com/jrfnl/bug-report-reproduction-scenarios/actions/runs/10683985742
Maybe I'm overlooking yet another option and there is work-around, in which case: great! but I haven't been able to find one.
Expected behavior
The most optimal solution would be one where PHPUnit deprecation notices do not affect the exit code of the test run ever.
The second most optimal solution would be some sort of configuration setting in the XML file and via a CLI flag to ignore PHPUnit deprecation notices.
Yes, this would also need a CLI flag as if this is only available via the XML config, the XML file would cause failing builds on older PHPUnit versions due to invalid configuration.
A much less optimal, but sort-of-workable solution would be one where the deprecation notice for using
#[CoversClass]
on traits is not thrown when there is also a#[CoversTrait]
attribute on the same test class, in combination with ignoring attributes which are not available in older PHPUnit versions.Additional context
Closely related issues:
displayDetailsOnTestsThatTrigger*
andfailOn*
whenconvert*ToExceptions
was set #5406#[CoversTrait]
and#[UsesTrait]
attributes #5799The text was updated successfully, but these errors were encountered: