简体   繁体   中英

Debugging PHPUnit tests in VS Code?

I have recently configured VS code to debug PHP with xdebug. It works reliably with my application code but when I am running unit tests with PHPunit, my breakpoints are ignored.

My server is run inside a vagrant box.

My php.ini file contains the following lines:

[xdebug]
zend_extension="/usr/lib/xdebug/xdebug-2.2.7/modules/xdebug.so" 
xdebug.remote_enable=1
xdebug.remote_port=9000
xdebug.remote_connect_back=1
xdebug.remote_autostart=1

I use the PHP Debug VS Code extension.

This is my launch.json config:

{
    "name": "Listen for XDebug",
    "type": "php",
    "request": "launch",
    "pathMappings": {
        "/var/www/mysite.local/": "${workspaceFolder}"
    },
    "port": 9000,
    "log": true
}

My unit tests run fine, for example, from within /var/www/mysite.local , I can run:

phpunit --filter myTestMethod path/to/my/test/file/myTest.php

but while the test will run, a breakpoint I have within the test itself is consistently ignored and I cannot figure out why.

Has anybody had a similar issue? Is there a difference between how debugging works in the context of a normal application request and a unit test?

Your problem is happening because of pathMappings settings when you run a unit test with an absolute path, my suggestion is to replace the

   "pathMappings": {
        "/var/www/mysite.local/": "${workspaceFolder}"
    },

with

   "pathMappings": {
        "/var/www/mysite.local/": "${workspaceFolder}",
        "${workspaceFolder}": "${workspaceFolder}"
    },

or disabled it when you run unit test

In my case, phpunit was calling the php cli and this latter was not using the php.ini conf file used by my test server which had xdebug activated.

Be sure the right php.ini file is used. You can specify it when calling php with the -c argument.

You can pass this argument to php by editing the launch script of phpunit that can be found in vendor/bin/phpunit.bat (for windows).

The file looks like this:

@ECHO OFF
setlocal DISABLEDELAYEDEXPANSION
SET BIN_TARGET=%~dp0/../phpunit/phpunit/phpunit
php "%BIN_TARGET%" %*

Just add the -c behind php with the path to php.ini directory:

php -c PATH_TO_PHP.INI_DIRECTORY "%BIN_TARGET%" %*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM