php 8.6.0 尚未发布,当前最新稳定版为 php 8.3.x,phpunit 10/11 系列已完全适配 php 8.2+。若你指的是未来版本或笔误(如 php 8.3),兼容性调整的核心在于:确保 phpunit.xml 中 phpversion 约束正确,移除对已废弃断言方法的调用,并检查第三方库是否声明了 ^8.2 || ^8.3 的兼容要求。通常无需代码修改,只需更新依赖和配置即可。

如何配置 phpunit.xml 以支持 PHP 8.3+
在 phpunit.xml 中设置 <php></php> 标签内的 <ini name="error_reporting" value="E_ALL"></ini> 并确保 <testsuites></testsuites> 路径正确。对于 PHP 8.3+,重点是将 <php></php> 版本检测从旧版的 8.0 更新为 8.3,避免 PHPUnit 拒绝运行。

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
cacheDirectory=".phpunit.cache">
<php>
<ini name="error_reporting" value="E_ALL"/>
<ini name="display_errors" value="1"/>
<ini name="memory_limit" value="256M"/>
</php>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
如何处理 PHPUnit 10/11 中的断言弃用警告
PHPUnit 10 移除了 assertArraySubset() 等过时方法,替换为 assertArrayHasKey() 或 assertEquals()。在 PHP 8.3+ 环境下,需全局搜索并替换这些调用,否则测试将失败。同时,@covers 注解现在要求更严格的类名匹配。
修复 Core bailout 状态损坏安全问题,以及 BZ2、DOM、PDO、Opcache/JIT 内存与执行错误,改进 Unicode 字素处理、URL 解析、TLS 流状态、信号分发及会话错误处理。
立即学习“PHP免费学习笔记(深入)”;

<?php
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
class ExampleTest extends TestCase
{
public function testArrayKeyExists()
{
$data = ['key' => 'value'];
// 旧写法 (PHPUnit 9): assertArraySubset(['key' => 'value'], $data);
// 新写法 (PHPUnit 10+):
$this->assertArrayHasKey('key', $data);
$this->assertEquals('value', $data['key']);
}
}
PHP 8.3 只读属性在测试中的影响
PHP 8.3 引入的只读属性(Readonly Properties)在单元测试中可能导致 ReflectionProperty::setValue() 抛出异常。若测试中使用反射修改对象状态,需捕获 Error 或使用 clone 创建副本而非直接修改。例如,对 readonly 属性的反射赋值会触发 Error: Cannot modify readonly property,需在测试中预先判断属性是否为只读。


















