如何在主机上访问 xdebug 会话,以便在 docker 容器内运行的单元测试


How to access xdebug session on host machine for unit tests that run inside docker container?

我有一个使用 hhvm 在 docker 容器内运行的 php 应用程序。

我的IDE是phpstorm,它在我的本地主机上运行,我设置了xdebug。

/etc/hhvm/php.ini的崇高部分看起来像:

; xdebug
hhvm.debug.server_error_message = true
xdebug.enable=1
xdebug.remote_enable=1
xdebug.idekey="PHPSTORM"
xdebug.remote_port = 9999
xdebug.remote_autostart=1
xdebug.remote_connect_back=1
xdebug.remote_handler=dbgp
xdebug.remote_log="/var/log/xdebug/xdebug.log"

对于我的 nginx 会议,我必须添加指定服务器名称以匹配我的反向代理。反向代理在我的本地主机上运行并将容器的 IP(例如127.0.0.1:8080映射到人类可读的 http://my_app.local

nginx conf 条目如下所示:

cat nginx/conf.d/my_app.conf 
server {
listen 80 default_server;
server_name my_app.local;
root /var/www/my_app/web;
index index.php index.html index.htm;
include hhvm.conf;
error_log  /var/log/nginx/sandbox/error.log;
access_log  /var/log/nginx/sandbox/access.log  main;
location /status {
  return 200;
  access_log off;
}
location ~/(assets|img|html|src|docs|bower_components|dist)/ {
   try_files $uri =404;
}
location / {
    try_files $uri /app.php$is_args$args;
}
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt  { log_not_found off; access_log off; }
}

当我对服务器发出请求时,这为我提供了一个有效的 xdebug 连接。

然而,一旦我使用 phpunit 从 docker 容器内部运行单元和功能测试,我还想要一个 xdebug 会话:

le-docker-container:/var/www/my-app# ./vendor/bin/phpunit -c tests/phpunit-no-coverage.xml

我怎样才能做到这一点?

您可以使用此方法:

PHP_IDE_CONFIG="serverName=my_app.local" "XDEBUG_CONFIG="remote_enable=1 remote_host=YOUR_REMOTE_IP idekey=PHPSTORM" ./vendor/bin/phpunit -c tests/phpunit-no-coverage.xml

服务器名称必须添加到phpstorm Langauge和Framework>配置PHP>服务器配置。并且必须确保定义了正确的路径映射。

xdebug.remote_connect_back=1需要$_SERVER['REMOTE_ADDR']。尝试明确定义它:

le-docker-container:/var/www/my-app# REMOTE_ADDR="10.0.0.1" ./vendor/bin/phpunit -c tests/phpunit-no-coverage.xml

或者将 xDebug 配置更改为改用xdebug.remote_host