How can I get XDebug to run with PHPUnit on the CLI?

The xdebug.profiler_enable setting can't be changed at runtime but only at the start of script.

Running phpunit -d foo=bar will just lead to phpunit calling ini_set("foo", "bar"); and that doesn't work since the value can't change at runtime.

See: xdebug.profiler_enable

Enables Xdebug's profiler which creates files in the profile output directory. Those files can be read by KCacheGrind to visualize your data. This setting can not be set in your script with ini_set(). If you want to selectively enable the profiler, please set xdebug.profiler_enable_trigger to 1 instead of using this setting.

Solution:

php -d xdebug.profiler_enable=on /usr/bin/phpunit XYZTestCase.php

By applying the setting directly to the PHP runtime and not phpunit it will be set before the script starts and should work.


Spent ages trying to get this to work. Think this may change my life though!

I originally was trying to do this (i.e. run phpunit) inside a vagrant box but realised it was easier (and faster performance wise) running it outside the vagrant box.

First off I used brew install php55 php55-xdebugusing homebrew on a mac (but your configuration may be different and it should still work). My site is a symfony2 project.

I was trying to follow this: phpunit vagrant xdebug to get it working from inside a vagrant box (almost got there but with some issues).

These settings worked for me (running site from a vagrant box, but phpunit outside vagrant box):

#xdebug.ini (parent machine, not inside vagrant box).
[xdebug]
zend_extension="/usr/local/Cellar/php55-xdebug/2.2.6/xdebug.so" #this will be different on your machine and will probably already be set

xdebug.max_nesting_level = 250 
xdebug.default_enable = 1
xdebug.idekey = "PHPSTORM" #seems to work without this too
xdebug.remote_enable = 1

Then running this at the command line (here I am using a download of phpunit instead of the one linked to in /usr/local/bin (which doesn't seem to work))

XDEBUG_CONFIG="idekey=PHPSTORM" bin/phpunit -c app

Or you can create a file called phpunit-debug (to store the XDEBUG_CONFIG environment variable) as outlined here: phpunit xdebug


Did you try to :

  1. Set your xdebug.idekey in your php.ini to wathever you want (eg: blacktie)
  2. restart your server

  3. Call you script by adding -d xdebug.idekey=blacktie

    phpunit -d xdebug.profiler_enable=on -d xdebug.idekey=blacktie XYZTestCase.php

Hope that help.


The correct name of the setting is xdebug.profiler_enable with an underscore. Change your command to this:

phpunit -d xdebug.profiler_enable=on XYZTestCase.php

You can run Xdebug from the command line by setting an environment variable beforehand, e.g.:

export XDEBUG_CONFIG="idekey=YOUR_IDE_KEY remote_host=localhost remote_enable=1"

This worked for me.

More information on the Xdebug documentation.