我正在尝试通过 PhpStorm (v2022.3.2) 使用 Xdebug (v3) 从 Docker 容器内部调试 PHPUnit 测试方法。
Xdebug 设置正确 PhpStorm 中的服务器也是。
事实上,如果我从命令行运行测试,Xdebug 会在断点处停止:
XDEBUG_SESSION=1 PHP_IDE_CONFIG="serverName=myapp.localhost" vendor/bin/phpunit --testdox --filter testProfileUpdate
如果我调试测试单击测试方法左侧的“错误”图标,相反,我收到以下错误:
连接未建立。可能 'xdebug.remote_host=host.docker.internal' 不正确。更改“xdebug.remote_host”。
查看PhpStorm执行的调试测试方法的命令,这是我看到的:
[docker://myapp:latest/]:php -dxdebug.mode=debug -dxdebug.client_port=9000 -dxdebug.client_host=host.docker.internal /opt/project/vendor/phpunit/phpunit/phpunit --configuration /opt/project/phpunit.xml --filter "/(ProfileTest::testProfileUpdate)( .*)?$/" --test-suffix ProfileTest.php /opt/project/tests --teamcity
我也尝试配置另外两个标志:
-dxdebug.idekey="serverName=myapp.localhost"
-dxdebug.session=1
结果命令是这样的:
[docker://myapp:latest/]:php -dxdebug.mode=debug -dxdebug.client_port=9000 -dxdebug.client_host=host.docker.internal -dxdebug.idekey="serverName=myapp.localhost" -dxdebug.session=1 /opt/project/vendor/phpunit/phpunit/phpunit --configuration /opt/project/phpunit.xml --filter "/(ProfileTest::testProfileUpdate)( .*)?$/" --test-suffix ProfileTest.php /opt/project/tests --teamcity
同样使用此配置,Xdebug 不会启动并且断点将被忽略(并弹出错误消息)。
我试图禁用 PhpStorm 自动传递的参数,转到“设置”>“PHP”>“调试”。
然后,在“高级设置”部分中,我取消标记“通过命令行传递所需的配置选项”选项。
现在,生成的命令是这样的:
[docker://myapp:latest/]:php /opt/project/vendor/phpunit/phpunit/phpunit --configuration /opt/project/phpunit.xml --filter "/(ProfileTest::testProfileUpdate)( .*)?$/" --test-suffix ProfileTest.php /opt/project/tests --teamcity
如您所见,没有提及
xdebug.client_host
或xdebug.remote_host
。
然而,PhpStorm 继续弹出同样的错误提及
xdebug.remote_host=host.docker.internal
.
本来以为应该是报错了,结果不一样,说是少了一个必填参数
关于如何配置 PhpStorm 以启动 Xdebug 会话以调试 PHPUnit 测试的任何想法?
-dxdebug.client_port=9000
Xdebug 3 现在默认使用
9003
端口而不是 9000
。尝试坚持这个。
在 PhpStorm 中,您很可能同时列出了新旧端口(
9000,9003
作为默认值)以涵盖新旧 Xdebug 版本。但是它使用列表中的第一个用于CLI脚本(PHPUnit是)...
要么删除它(首选;所以你只会在那里有
9003
)或让它持续(即9003,9000
)。这是每个项目的设置,因此如果需要,您可以在另一个项目中使用旧的 9000 端口。
-dxdebug.idekey="serverName=myapp.localhost"
这行不通(错误的做法)。您正在尝试将其作为 Xdebug IDEKey 参数传递...但它实际上应该是名为
ENV
.的PHP_IDE_CONFIG
变量的值
如果你想实际使用它:
Environment variables
字段:尝试将它用于此目的。除此之外:启用 Xdebug 日志 (
xdebug.log
),尝试调试(当它工作时和不工作时)并查看两次运行时日志必须说的内容(它连接到哪里,响应是什么等) .您应该发现应该提供线索的差异(在哪个方向进一步挖掘)。
这实际上应该是调查“为什么它在这里而不在那里起作用”情况的第一步。