sebastianbergmann / phpunit

The PHP Unit Testing framework.
https://phpunit.de/
BSD 3-Clause "New" or "Revised" License
19.69k stars 2.2k forks source link

When expecting a deprecation to occur using `expectUserDeprecationMessage`, the test should succeed, not fail #6018

Open ruudk opened 4 hours ago

ruudk commented 4 hours ago
Q A
PHPUnit version 11.4.3
PHP version 8.3.13
Installation Method Composer

Summary

When using expectUserDeprecationMessage, we noticed that the expectation is verified correctly but that the test will still fail afterwards because of our strict configuration in regards to deprecations.

We want this strict setup because we want to catch every deprecation that is produced.

But when we are testing/asserting them, we want to silence them obviously.

Current behavior

1 test triggered 1 deprecation:

1) SomethingThatTriggersDeprecation.php:23
Some Deprecation
Triggered by:

* SomeTest::it_should
  /Path/To/SomeTest.php:82

OK, but there were issues!
Tests: 1, Assertions: 8, Deprecations: 1.
Process finished with exit code 1

How to reproduce

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
         colors="true"
         displayDetailsOnSkippedTests="true"
         displayDetailsOnTestsThatTriggerDeprecations="true"
         displayDetailsOnTestsThatTriggerErrors="true"
         displayDetailsOnTestsThatTriggerNotices="true"
         displayDetailsOnTestsThatTriggerWarnings="true"
         failOnWarning="true"
         failOnRisky="true"
         bootstrap="tests/phpunit_bootstrap.php"
         failOnDeprecation="true"
         failOnPhpunitDeprecation="true"
>
<!-- .... --> 
 <source restrictNotices="true"
            restrictWarnings="true"
            ignoreSuppressionOfDeprecations="true"
            ignoreSuppressionOfPhpDeprecations="true"
            ignoreSuppressionOfErrors="true"
            ignoreSuppressionOfNotices="true"
            ignoreSuppressionOfPhpNotices="true"
            ignoreSuppressionOfWarnings="true"
            ignoreSuppressionOfPhpWarnings="true"
    >
        <include>
            <directory>src</directory>
            <directory>src-dev</directory>
            <directory>tests</directory>
            <directory>vendor</directory>
        </include>
        <deprecationTrigger>
            <function>trigger_deprecation</function>
        </deprecationTrigger>
    </source>
<!-- .... --> 
    #[Test]
    public function it_should_resolve_to_list() : void
    {
        $this->expectUserDeprecationMessage('Some Deprecation'); // this works, it verifies the expected deprecation

       $callSomethingThatTriggerIt();
    }

Expected behavior

No deprecation at the end of the test + no failing test.

sebastianbergmann commented 3 hours ago

Please provide a minimal, self-contained, reproducing test case that shows the problem you are reporting.

Without such a minimal, self-contained, reproducing test case I will not be able to investigate this issue.