VSCode中PHP使用Xdebug

本地环境

  • windows10
  • php8.2 nts
  • xdebug v3
  • thinkphp v8

下载Xdebug

Xdebug下载地址

从xdebug下载地址,下载最新的xdebug,解压后将php_xdebug.dll放入php目录的ext目录下

配置php.ini

bash 复制代码
[Xdebug]
zend_extension = php_xdebug
xdebug.client_host = 127.0.0.1
xdebug.client_port = 9003
xdebug.mode = debug,coverage
xdebug.start_with_request = yes
xdebug.idekey=VSCODE

安装扩展

配置launch.json

javascript 复制代码
{
    // 使用 IntelliSense 了解相关属性。
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch built-in server and debug",
            "type": "php",
            "request": "launch",
            "runtimeArgs": [
                "-S",
                "localhost:8000",
                "-t",
                "."
            ],
            "port": 9003,
            "serverReadyAction": {
                "action": "openExternally"
            }
        },
        {
            "name": "Debug current script in console",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "externalConsole": false,
            "port": 9003
        },
        {
            "name": "Listen for Xdebug",
            "type": "php",
            "request": "launch",
            "port": 9003
        }
    ]
}

运行调试

配合单元测试

安装phpunit

bash 复制代码
composer require phpunit/phpunit --dev

因为我用的是tp8,单元测试bootstrap需要启动tp的容器,所以安装一下我的扩展bingher/think-test

bash 复制代码
composer require bingher/think-test --dev

安装PHPUnit Test Explorer

扩展配置

javascript 复制代码
"phpunit.command": "php",
"phpunit.phpunit": "vendor/bin/phpunit",
"phpunit.showAfterExecution": "always",
"phpunit.debuggerConfig": "Listen for Xdebug",
"phpunit.xdebugPort": 9003

重点是phpunit.command,phpunit.phpunit,我原来仅配置phpunit.phpunit: vendor/bin/phpunit.bat 发现扩展执行了一直在转圈圈,调试模式报错: Error: spawn -dxdebug.mode=debug ENOENT at Process.ChildProcess._handle.onexit (node:internal/child_process:285:19) at onErrorNT (node:internal/child_process:483:16) at processTicksAndRejections (node:internal/process/task_queues:82:21)

根据以上配置后PHPUnit Test Explorer顺利运行

phpunit.xml

XML 复制代码
<phpunit colors="true" verbose="true" bootstrap="./vendor/bingher/think-test/src/init.php">
  <coverage cacheDirectory="./runtime/phpunit/" pathCoverage="true">
    <include>
      <directory suffix=".php">./app/</directory>
    </include>
    <report>
      <html outputDirectory="./runtime/phpunit/" lowUpperBound="50" highLowerBound="90"/>
    </report>
  </coverage>
  <testsuites>
    <testsuite name="test">
      <directory>tests</directory>
    </testsuite>
  </testsuites>
</phpunit>

phpunit.xml可以参考phpunit官网进行配置.

如果没有安装bingher/think-test, 可以不用配置bootstrap

单元测试

相关推荐
Q_Q19632884753 小时前
python的电影院座位管理可视化数据分析系统
开发语言·spring boot·python·django·flask·node.js·php
Ashlee_code5 小时前
香港券商智能櫃台系統技術解決方案——融合跨境清算與AI風控,助力券商把握滬港雙市爆發機遇**
java·科技·金融·重构·架构·系统架构·php
nightunderblackcat7 小时前
进阶向:人物关系三元组,解锁人物关系网络的钥匙
开发语言·python·开源·php
吐个泡泡v8 小时前
Maven 核心命令详解:compile、exec:java、package 与 IDE Reload 机制深度解析
java·ide·maven·mvn compile
细节处有神明9 小时前
Jupyter 中实现交互式图表:ipywidgets 从入门到部署
ide·python·jupyter
程序员陆通9 小时前
CloudBase AI ToolKit + VSCode Copilot:打造高效智能云端开发新体验
人工智能·vscode·copilot
任磊abc1 天前
vscode无法检测到typescript环境解决办法
ide·vscode·typescript
hfut02881 天前
【vscode使用说明】
vscode·编辑器·vim
向日葵.1 天前
fastdds.ignore_local_endpoints 属性
服务器·网络·php
weixin_307779131 天前
VS Code配置MinGW64编译GNU 科学库 (GSL)
开发语言·c++·vscode·算法