You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

php artisan test命令执行失败,报DOMDocument未初始化错误

解决php artisan test抛出Typed property DOMDocument::$documentElement must not be accessed before initialization错误

这个问题大概率和PHP DOM扩展缺失、Laravel缓存冲突,或者依赖版本不兼容PHP8.1有关,以下是直接可行的解决步骤:

  • 检查并安装DOM扩展
    Ubuntu系统中,DOM扩展包含在php8.1-xml包内,先执行命令确认是否已安装:

    php -m | grep dom
    

    如果没有输出,执行以下命令安装:

    sudo apt update && sudo apt install php8.1-xml
    

    安装完成后重启PHP-FPM或web服务(比如sudo systemctl restart php8.1-fpm)。

  • 清除Laravel缓存
    缓存可能导致测试环境加载异常,执行以下命令清理:

    php artisan cache:clear
    php artisan config:clear
    rm -rf bootstrap/cache/*
    

    之后再带无缓存参数执行测试:

    php artisan test --no-cache
    
  • 更新依赖包
    旧版本的Laravel框架或PHPUnit可能对PHP8.1的类型属性支持不完善,执行更新命令:

    composer update
    

    更新完成后重新运行测试命令。

  • 验证PHPUnit配置
    确保项目根目录的phpunit.xml文件中,APP_ENV已设置为testing,且没有禁用必要的扩展配置。

内容的提问来源于stack exchange,提问作者coolguy123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 21:05:55