13

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?

1
  • I think an xdebug.log may be more explanatory. Add xdebug.remote_log=xdebug.log into your xdebug config and try to debug again a phpunit test. Then edid your question and place the xdebug.log into your answer. The xdebug.log will be in your project's root. Commented Oct 18, 2019 at 7:36

6 Answers 6

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer @mfort. I tried adding "${workspaceFolder}": "${workspaceFolder}" to my pathMappings, to no such luck. What exactly should I disable when I run a unit test?
@loxyboi Refer to this line "/var/www/mysite.local/": "${workspaceFolder}", replace with // "/var/www/mysite.local/": "${workspaceFolder}",
Thanks @mfort but that doesn't seem to have solved my problem.
0

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%" %*

2 Comments

The -c flag of phpunit is to load an XML file (from the help: -c|--configuration <file> Read configuration from XML file) but the php.ini file is not an XML document, so how did you get it to load?
That's the -c flag of php, not phpunit. I may have not been very clear, let me updated the answer.
0

Do you happen to have your Xdebug 2 set to start on trigger?

xdebug.trace_enable_trigger=1
xdebug.trace_enable=0

xdebug.profile_enable_trigger=1
xdebug.profile_enable=0

On Xdebug 3 with start on trigger (xdebug.start_with_request=trigger), I had to set the xdebug.idekey for vscode. It can also be set using the XDEBUG_CONFIG envvar:

XDEBUG_CONFIG="idekey=VSCODE" phpunit --filter myTestMethod path/to/my/test/file/myTest.php

It seems like vscode checks for any set idekey value; In my browser my Xdebug helper extension has had the idekey set to PHPSTORM, and debugging from there has been working just fine

1 Comment

You may need to append to the envvar instead if it isn't empty like mine was: XDEBUG_CONFIG="$XDEBUG_CONFIG \"idekey=VSCODE\""
0

I had the same issue and later found out somehow my Xdebug model is broken:

php -m

Didn't find the Xdebug model:

Warning: Failed to load D:\xampp\php\ext\php_xdebug-3.1.3-7.4-vc15-x86_64.dll, The system cannot find the file specified.

Reinstalled the XDebug fixed my problem.

Also need to restart Apache.

Comments

0

I had the same effect (breakpoints were ignored in Visual Studio Code).

  1. Ensure installation and loading the module worked by calling on a command line prompt

    php -m  
    

    This shows all loaded PHP extensions (Xdebug should be listed in the Zend Modules section)

  2. Ensure your configuration was applied by checking that the .ini files you were modifying are loaded

    php --ini
    

    (in my case, the ini file /etc/php/8.2/cli/conf.d/my_xdebug.ini was only readable by root and therefore ignored, making it user readable solved my problem)

  3. Show effective configuration options of xdebug

    php --re xdebug
    

The options I finally used for phpunit in the ini file were

xdebug.mode=debug
xdebug.idekey=VSCODE

Comments

0

For me, adding this in php.ini worked like a charm.

xdebug.start_with_request=yes

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.