Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

catching warnings, notices and deprecations with phpunit 9.4 on php 8.1.0

Tags:

phpunit

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.4.0 on PHP 8.0.9 I get the following (irrelevant parts of the output have been removed):

PHPUnit 9.4.0 by Sebastian Bergmann and contributors.

R                                                                   1 / 1 (100%)

When I run vendor/bin/phpunit on that on PHPUnit 9.4.0 on PHP 8.1.0 I get the following:

Deprecated: PHPUnit\Runner\DefaultTestResultCache implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in C:\path\to\code\vendor\phpunit\phpunit\src\Runner\DefaultTestResultCache.php on line 34
PHPUnit 9.4.0 by Sebastian Bergmann and contributors.

R                                                                   1 / 1 (100%)

ie. in PHP 8.1 it's like I'm getting a full dump of the Throwable, stack trace included, whereas I'm not getting that in PHP 8.0.

Any ideas? 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 either

like image 544
neubert Avatar asked Dec 09 '25 04:12

neubert


1 Answers

This is an incompatibility of PHPUnit with PHP 8.1 and has already been fixed a while back in 9.5.5, so you need to update.

9.4 is not a supported version anymore.

like image 151
Tobias K. Avatar answered Dec 11 '25 21:12

Tobias K.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!