尝试在cli上使用xdebug调试phpunit单元测试失败


Trying to debug phpunit unit test on cli using xdebug fails

我正在使用PHPStorm,它被配置为使用xDebug(我可以通过web浏览器调试)

我在PHPStorm中运行调试器,它的idekey为11854,我试图调试单元测试,并且我已经正确设置了断点

所以我通过cli执行了这个命令:

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

尽管如此,它不会相应地在断点处进行调试…

当我尝试在测试脚本中执行这个时:

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

会显示xdebug。Profiler_enable为0和xdebug。Idekey只是我的用户名。

我做错了什么,我怎么能让xdebug工作在phpunit通过cli

您只是在为phpunit设置参数,而不是PHP。下面的代码可以满足你的要求:

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

xdebug文档给出了一个看起来更简单的解决方案…

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

一旦设置了该变量,您就可以像往常一样从命令行运行脚本(对于SSH会话),PHP将使用此配置。

另需注意:

您还可以配置xdebug。remote_host xdebug.remote_port,xdebug。Remote_mode和xdebug。Remote_handler在同一环境中变量,只要用空格

分隔值即可。

在Ubuntu调试命令行中,我发现让它工作的唯一方法是将以下内容添加到您的~/。bashrc文件(

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

替换idekey = ? ?到11854。请确保启动一个新的控制台会话,以便使用这些变量。

我在这里尝试一个非常不同的设置。我在dockers中运行我的应用程序,并且我有一个与在生产环境中运行的容器非常相似的容器。因此,为了在我的开发环境中使用xdebug运行php,我必须使用xdebug参数和PHPStorm变量设置别名。

$ 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"
在这个不漂亮的技巧之后,我可以在PHPStorm上设置断点并从命令行运行phpunit。
$ php ../../bin/phpunit --verbose

以上命令均在容器内运行。localapp.docker是我的容器地址,是主机的/etc/hosts硬编码。