2

Quoting https://phpunit.readthedocs.io/en/9.5/writing-tests-for-phpunit.html#testing-php-errors-warnings-and-notices , "By default, PHPUnit converts PHP errors, warnings, and notices that are triggered during the execution of a test to an exception". Bearing that in mind, here's my unit test:

<?php

class DemoTest extends PHPUnit\Framework\TestCase
{
    public function testDemo()
    {
        try {
            trigger_error('zzz', E_USER_DEPRECATED);
        } catch (\Throwable $e) {}
    }
}

When I run vendor/bin/phpunit on that with PHPUnit 9.5.10 (on either PHP 8.0.9 or PHP 8.1.0) I get the following:

PHPUnit 9.5.10 by Sebastian Bergmann and contributors.

R                                                                   1 / 1 (100%)
Deprecated: zzz in C:\path\to\code\tests\DemoTest.php on line 8


Time: 00:00.007, Memory: 6.00 MB

There was 1 risky test:

1) DemoTest::testDemo
This test did not perform any assertions

C:\path\to\code\tests\DemoTest.php:5

OK, but incomplete, skipped, or risky tests!
Tests: 1, Assertions: 0, Risky: 1.

I don't want to see the error in the output and, if an exception isn't being thrown, then $this->expectException('PHPUnit\\Framework\\Error\\Deprecated') isn't gonna work for me.

Any ideas?

neubert
  • 15,947
  • 24
  • 120
  • 212
  • The difference between this question and https://stackoverflow.com/q/70321515/569976 is that that other question is about PHPUnit 9.4 vs PHPUnit 9.5. And the issue in PHPUnit 9.4 only manifests itself on PHP 8.1 whereas this one has consistent behavior across at least both PHP 8.0 and 8.1. – neubert Dec 12 '21 at 06:53

3 Answers3

3

In the changelog of 9.5.10 it is stated that:

PHPUnit no longer converts PHP deprecations to exceptions by default (configure convertDeprecationsToExceptions="true" to enable this)

So this is expected. Technically one could even argue that the statement about "errors, warnings, and notices" is correct, as deprecations are their own category.

You typically configure this in a XML configuration file, provided with -c.

Also mind the note on the documentation page about the error_reporting ini/runtime setting: It must not exclude deprecations aswell (E_ALL is fine).

Tobias K.
  • 2,997
  • 2
  • 12
  • 29
0

In case someone else arrives here and the convertDeprecationsToExceptions = "true" setting won't work then also check if you don't have @ in front of the trigger_error() statement like for example the Drupal's deprecation policy suggests.

That is on purpose excluded from PHPUNIT v9.5 error handler:

/*
 * Do not raise an exception when the error suppression operator (@) was used.
 *
 * @see https://github.com/sebastianbergmann/phpunit/issues/3739
 */
if (!($errorNumber & error_reporting())) {
    return false;
}
John Linhart
  • 1,746
  • 19
  • 23
-2

it seems that intention was to throw and catch an exception, but using trigger_error is not the same (it is not throw an exception). There is a great answer explaining difference. If your intention is to handle deprecation errors, then I suggest to use set_error_handler where there is a possibility to custom handle E_DEPRECATED messages.

  • 2
    Quoting https://phpunit.readthedocs.io/en/9.5/writing-tests-for-phpunit.html#testing-php-errors-warnings-and-notices , "_By default, PHPUnit converts PHP errors, warnings, and notices that are triggered during the execution of a test to an exception_". PHPUnit no doubt does this through the use of it's own custom error handler. Remember, I am not trying to do this in a regular PHP script - I am trying to do this in the context of a PHPUnit unit test. – neubert Dec 16 '21 at 14:36