簡體   English   中英

如何使用 VSCode 在 Windows 的 CLI 上使用 PHPUnit 運行 XDebug?

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

我運行了這個命令:

"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...語句被打印出來(但不是正在打印的read ECONNRESET Installing...語句)

問題

  1. 我想了解當我嘗試運行單元測試時如何讓調試工作?

  2. 為什么我會看到上面描述的掛起行為? 我不明白什么?

參考資料

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

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

a. 添加另一個配置以在另一個端口 (8000) 上偵聽 XDebug

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

從命令行啟動時使用此命令

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

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

注意事項:

  1. 在launch.json 中嘗試--stderr、multiple_sessions for xdebugSettings、-d xdebug.profiler_enable=on -d xdebug.idekey=VSCODE 等不起作用。
  2. 確保 -d 選項在 phpunit 選項之前,否則,它會認為它是 phpunit 而不是 php 的參數

FWIW - 在 launch.json 中設置以下內容適用於 Ubuntu(通過 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
    }
]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM