【问题标题】:catching warnings, notices and deprecations with phpunit 9.4 on php 8.1.0在 php 8.1.0 上使用 phpunit 9.4 捕获警告、通知和弃用
【发布时间】:2022-01-16 03:35:34
【问题描述】:

引用https://phpunit.readthedocs.io/en/9.5/writing-tests-for-phpunit.html#testing-php-errors-warnings-and-notices,“默认情况下,PHPUnit 会将测试执行期间触发的 PHP 错误、警告和通知转换为异常”。考虑到这一点,这是我的单元测试:

<?php

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

当我在 PHP 8.0.9 上使用 PHPUnit 9.4.0 运行 vendor/bin/phpunit 时,我得到以下信息(输出的不相关部分已被删除):

PHPUnit 9.4.0 by Sebastian Bergmann and contributors.

R                                                                   1 / 1 (100%)

当我在 PHP 8.1.0 上的 PHPUnit 9.4.0 上运行 vendor/bin/phpunit 时,我得到以下信息:

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%)

即。在 PHP 8.1 中,就像我得到了 Throwable 的完整转储,包括堆栈跟踪,而我在 PHP 8.0 中没有。

有什么想法吗?我不想在输出中看到错误,如果没有抛出异常,那么 $this-&gt;expectException('PHPUnit\\Framework\\Error\\Deprecated') 也不适合我

【问题讨论】:

  • 从表面上看,有人可能认为升级到 PHPUnit 9.5 可以解决问题,但我在该版本中遇到了非常相似的问题:stackoverflow.com/q/70321543/569976 唯一的区别是在 9.5 中我在 每个版本的PHP而不是只是 PHP 8.1.0。

标签: phpunit


【解决方案1】:

这是 PHPUnit 与 PHP 8.1 的不兼容问题,并且已经在 9.5.5 中修复了一段时间,因此您需要更新。

9.4 不再是受支持的版本。

【讨论】:

    猜你喜欢
    • 2022-01-16
    • 2017-09-29
    • 2020-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多