【问题标题】:How can I get XDebug to run with PHPUnit on the CLI in Windows using VSCode?如何使用 VSCode 在 Windows 的 CLI 上让 XDebug 与 PHPUnit 一起运行?
【发布时间】:2018-03-07 09:38:07
【问题描述】:

我运行了这个命令:

"C:\xampp\php\.\php.exe" "C:\xampp\php\phpunit" -d xdebug.profiler_enable=on -d xdebug.idekey=VSCODE

C:\xampp\php\php.ini 有以下内容:

[XDebug]
zend_extension = "C:\xampp\php\ext\php_xdebug-2.5.0-7.1-vc14.dll"
xdebug.idekey=VSCODE
xdebug.profiler_enable=1
xdebug.default_enable=1
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.remote_autostart=1

如果我在没有 Listen for XDebug 的情况下运行命令,我将在命令行窗口中看到以下内容:

Installing...
Running as single site... To run multisite, use -c tests/phpunit/multisite.xml
Not running ajax tests. To execute these, use --group ajax.
Not running ms-files tests. To execute these, use --group ms-files.
Not running external-http tests. To execute these, use --group external-http.
PHPUnit 3.7.21 by Sebastian Bergmann.

Configuration read from C:\xampp\apps\summit\htdocs\wp-content\plugins\hl-market-tracker\phpunit.xml.dist
...

我进入 VSCode 并监听 XDebug,当我运行命令时,我看到请求 1 和 2 出现在调用堆栈中,但没有运行单元测试(它似乎挂在某个地方,因为 Installing... 甚至没有显示在命令行窗口中)。如果我停止监听 Xdebug,我会在调试控制台中看到read ECONNRESET,然后我会看到打印出Running as single site... 语句(但不是Installing... 语句)

问题

  1. 我想了解当我尝试运行单元测试时如何让调试工作?

  2. 为什么我看到了上述挂起行为?我不明白什么?

参考文献

  1. https://pippinsplugins.com/unit-tests-wordpress-plugins-writing-tests/
  2. How can I get XDebug to run with PHPUnit on the CLI?
  3. https://tighten.co/blog/configure-vscode-to-debug-phpunit-tests-with-xdebug
  4. https://xdebug.org/docs/remote

【问题讨论】:

标签: php visual-studio-code xdebug


【解决方案1】:

#2 的答案https://github.com/felixfbecker/vscode-php-debug/issues/164:所描述的根本问题是,当运行两个 php 进程时,它会混淆 VS Code 的调试器。当我读到这篇文章时,我认为因为每当运行 php 时,它都会从 php.ini 中读取,并且该文件告诉 xdebug 监听端口 9000,这就是为什么如果运行两个 php 进程,XDebug 会感到困惑。从那,我看到 phpunit 使用 phpunit 选项启动 php。然后我想用不同的远程端口启动这个特定的 php,看看它是否解决了挂起问题,它确实解决了。

回答#1:所以,在 VS Code 的 launch.json 中,

一个。添加另一个配置以在另一个端口 (8000) 上侦听 XDebug

    "name": "PHPUnit",
    "type": "php",
    "request": "launch",
    "port": 8000

b.从命令行启动时使用此命令

"C:\xampp\php\.\php.exe" -d xdebug.remote_port=8000 "C:\xampp\php\phpunit"

或者,在 phpunit.bat 中更改它以便能够在命令行中键入 phpunit

注意事项:

  1. 正在尝试 --stderr, multiple_sessions 中的 xdebugSettings 启动.json,-d xdebug.profiler_enable=on -d xdebug.idekey=VSCODE, 等没有工作。
  2. 确保 -d 选项在 phpunit 选项之前,否则,它会认为这是 phpunit 而不是 php 的参数

【讨论】:

  • 我在同一个开发服务器上有主控和客户端类型两个 php cms。基于这个答案,我在客户端目录的 .htaccess 中添加了php_value xdebug.remote_port 9001,默认情况下在端口 9000 上为 master 运行。根据github.com/Microsoft/vscode/issues/1616#issuecomment-204303358,我在 vscode setup debug config 中为端口 9001 创建了虚拟项目,这一切都开始工作了。非常感谢你拯救了我的一天。
  • 这个答案有效!为了更容易打开多个配置,vscode 现在有code.visualstudio.com/docs/editor/…。要运行 phpunit,我无法改进这个答案,在 linux 上通常是 'php -d xdebug.remote_port=8000 "vendor/bin/phpunit" "anyphpunitargument"'
【解决方案2】:

FWIW - 在 Ubuntu 上为我在 launch.json 中设置以下内容(通过 apt 安装了 XDebug):

    "configurations": [
    {
        "name": "Launch Unit Tests",
        "type": "php",
        "request": "launch",
        "program": "${workspaceFolder}/vendor/bin/phpunit",
        "cwd": "${workspaceFolder}",
        "env": {
            "XDEBUG_CONFIG": "idekey=VSCODE remote_enable=1 profile_enable=1"
        },
        "args": [
            // "--filter",
            // "testCanCreateValid"
        ],
        "port": 9000
    }
]

【讨论】:

    猜你喜欢
    • 2012-08-19
    • 2013-05-04
    • 1970-01-01
    • 2010-12-26
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    • 2015-01-06
    • 2011-03-09
    相关资源
    最近更新 更多