4

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

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
...

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. 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
mysti9uemirage
  • 317
  • 3
  • 9
  • 1
    I think I'm running into this issue, but I don't know how to get past this so that I can write unit tests for the WP plugin. https://github.com/felixfbecker/vscode-php-debug/issues/164 – mysti9uemirage Mar 08 '18 at 05:40

2 Answers2

7

Answer to #2: https://github.com/felixfbecker/vscode-php-debug/issues/164: The root problem described was that when two php processes were ran, it confused VS Code's debugger. When I read this, I thought that because whenever php is ran, it reads from php.ini and the file tells xdebug to listen to port 9000, that's why if two php processes are ran, XDebug was confused. From that, I saw that phpunit launches php with phpunit option. I then wanted to launch this specific php with a different remote port to see if it solved the hanging problem, and it did.

Answer to #1: So, in VS Code's launch.json,

a. Add another configuration to listen for XDebug on another port (8000)

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

b. Use this command when launching from command line

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

Or, change it in phpunit.bat to be able to just type phpunit in command line

NOTES:

  1. Trying --stderr, multiple_sessions for xdebugSettings in launch.json, -d xdebug.profiler_enable=on -d xdebug.idekey=VSCODE, etc. didn't work.
  2. Make sure that -d options are before the phpunit option, else, it will think it's the argument for phpunit instead of for php
mysti9uemirage
  • 317
  • 3
  • 9
  • 2
    I have master and client type two php cms on same dev server. Based on this answer I added `php_value xdebug.remote_port 9001` in client directory's .htaccess and default is running on port 9000 for master. According to https://github.com/Microsoft/vscode/issues/1616#issuecomment-204303358 I created dummy project in vscode setup debug config for port 9001, and it all started working. Thanks a lot you saved my day. – itsoft3g May 16 '18 at 18:04
  • 1
    This answer works! To make it easier to open up the multiple configurations, vscode now has https://code.visualstudio.com/docs/editor/debugging#_multitarget-debugging. To run phpunit, I haven't been able to improve on this answer, which on linux is typically 'php -d xdebug.remote_port=8000 "vendor/bin/phpunit" "anyphpunitargument"' – Jonathan Jul 17 '18 at 21:54
6

FWIW - setting up the following in launch.json works for me on Ubuntu (with XDebug installed via apt):

    "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
    }
]
Jason
  • 941
  • 1
  • 10
  • 19