How can I get XDebug to run with PHPUnit on the CLI in Windows using VSCode?

mys*_*age 3 php xdebug visual-studio-code

I ran this command:

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

C:\xampp\php\php.ini has the following:

[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
Run Code Online (Sandbox Code Playgroud)

If I run the command without Listen for XDebug, I will see the following in the command line window:

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
...
Run Code Online (Sandbox Code Playgroud)

I went into VSCode and Listen for XDebug and when I run the command, I see Request 1 and 2 appears in the Call Stack but no unit test got run (it seems to be hung somewhere because Installing... doesn't even show up in the command line window). If I stop listening for Xdebug, I will see read ECONNRESET in the debug console, and then I will see the Running as single site... statement being printed out (but not the Installing... statement)

Questions:

  1. I would like to understand how to get debugging to work when I try to run the unit test?

  2. Why did I see the hung behavior described above? What am I not understanding?

References:

  1. https://pippinsplugins.com/unit-tests-wordpress-plugins-writing-tests/
  2. 如何在CLI上使XDebug与PHPUnit一起运行?
  3. https://tighten.co/blog/configure-vscode-to-debug-phpunit-tests-with-xdebug
  4. https://xdebug.org/docs/remote

mys*_*age 6

#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中,

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

    "name": "PHPUnit",
    "type": "php",
    "request": "launch",
    "port": 8000
Run Code Online (Sandbox Code Playgroud)

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

"C:\xampp\php\.\php.exe" -d xdebug.remote_port=8000 "C:\xampp\php\phpunit"
Run Code Online (Sandbox Code Playgroud)

或者,在phpunit.bat中对其进行更改,使其能够仅在命令行中输入phpunit

笔记:

  1. 尝试--stderr,launch.json中xdebugSettings的多会话,-d xdebug.profiler_enable = on -d xdebug.idekey = VSCODE等不起作用。
  2. 确保-d选项在phpunit选项之前,否则,它将认为这是phpunit而不是php的参数

  • 我在同一台开发服务器上拥有主服务器和客户端类型的两个php cms。基于此答案,我在客户端目录的.htaccess中添加了php_value xdebug.remote_port 9001,默认情况下,该端口在master的端口9000上运行。根据https://github.com/Microsoft/vscode/issues/1616#issuecomment-204303358,我在vscode设置调试配置中为端口9001创建了虚拟项目,并且所有这些都开始工作。非常感谢您保存了我的一天。 (2认同)

Jas*_*son 6

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
    }
]
Run Code Online (Sandbox Code Playgroud)