Trying to debug phpunit unit test in cli using xdebug not working

I use PHPStorm, which is configured to use xDebug (I can debug through a web browser just fine)

I am running a debugger in PHPStorm which has idekey 11854 and I am trying to debug unit test and I set breakpoints correctly

so I ran this command via cli:

phpunit -d xdebug.profiler_enable=on -d xdebug.idekey=11854 --filter testFunction s_function/sFunctionTest.php 

However, it will not debug at the breakpoint accordingly ...

when I tried to execute this in a test script:

 error_log(ini_get('xdebug.profiler_enable')); error_log(ini_get('xdebug.idekey')); 

it would show that xdebug.profiler_enable is 0, and xdebug.idekey is just my username.

What i did wrong and how can i get xdebug to work on phpunit via cli

+6
source share
4 answers

You just set the arguments for phpunit, not PHP. The following should do what you want:

 php -d xdebug.profiler_enable=on -d xdebug.idekey=11854 `which phpunit` --filter testFunction s_function/sFunctionTest.php 
+7
source

xdebug docs give a solution that looks simpler ...

 export XDEBUG_CONFIG="idekey=session_name" php myscript.php 

Once this variable is set, you can run your scripts from the command line as usual (for this SSH session), and PHP will use this configuration.

Also note:

You can also configure xdebug.remote_host, xdebug.remote_port, xdebug.remote_mode and xdebug.remote_handler in the same environment variable, as long as you separate the values ​​with a space

+2
source

In Ubuntu, for debugging the CLI, I found the only way to make it work, to add the following to your ~ / .bashrc file

 export PHP_IDE_CONFIG='serverName=localhost' export XDEBUG_CONFIG='idekey=PHPSTORM' 

Replace idekey = ?? to 11854 in your example. Remember to start a new console session to use vars.

0
source

I try a completely different setting here. I run my applications inside dockers, and I have a container very similar to containers running in production. So, in order to run php with xdebug in my development environment, I had to configure an alias with xdebug parameters and the PHPStorm variable.

 $ alias php=`which php`' \ -d xdebug.idekey=xdbg \ -d xdebug.remote_enable=1 \ -d xdebug.remote_connect_back=1 \ -d xdebug.remote_autostart=1 \ -d xdebug.remote_port=9000 \ -d xdebug.remote_host=172.17.0.1 \ -d xdebug.remote_handler=dbgp' $ export PHP_IDE_CONFIG="serverName=localapp.docker" 

After this not beautiful trick, I could set breakpoints on PHPStorm and run phpunit from the command line.

 $ php ../../bin/phpunit --verbose 

All the commands above were run inside the container. localapp.docker is the address of my container and is hardcoded on the /etc/hosts host.

0
source

Source: https://habr.com/ru/post/947320/


All Articles