如何在PhpStorm中用Xdebug调试Drupal项目的Behat测试?
在Drupal环境下用Xdebug调试Behat测试的问题
核心问题
在Drupal环境中使用Xdebug调试Behat测试时,已设置断点但无法进入Drupal上下文函数或自定义上下文。
环境信息
- 运行环境:ddev容器中运行Drupal项目的Behat测试
- 调试工具:Xdebug(PHP调试)
问题现象
即使在上下文文件中设置断点,调试器也无法进入Drupal上下文或自定义上下文;Xdebug仅从index.php启动调试,跳过Behat测试内的Drupal环境调试,只启动网站调试而非场景上下文的调试。
Xdebug配置诊断输出
│ │ __ __ _ _ │ \ \ / / | | | | │ \ V / __| | ___| |__ _ _ __ _ │ > < / _` |/ _ \ '_ \| | | |/ _` | │ / . \ (_| | __/ |_) | |_| | (_| | │ /_/ \_\__,_|\___|_.__/ \__,_|\__, | │ __/ | │ |___/ │ │ Version => 3.2.1 │ Support Xdebug on Patreon, GitHub, or as a business: https://xdebug.org/support │ │ Enabled Features (through 'xdebug.mode' setting) │ Feature => Enabled/Disabled │ Development Helpers => ✘ disabled │ Coverage => ✘ disabled │ GC Stats => ✘ disabled │ Profiler => ✘ disabled │ Step Debugger => ✔ enabled │ Tracing => ✘ disabled │ │ Optional Features │ Compressed File Support => no │ Clock Source => clock_gettime │ 'xdebug://gateway' pseudo-host support => yes │ 'xdebug://nameserver' pseudo-host support => yes │ Systemd Private Temp Directory => not enabled │ │ Diagnostic Log │ [Step Debug] WARN: Creating socket for '172.17.0.1:9000', poll success, but error: Operation now in progress (29). │ [Step Debug] ERR: Could not connect to debugging client. Tried: 172.17.0.1:9000 (through xdebug.client_host/xdebug.client_port). │ │ Step Debugging │ Debugger is not active │ │ PHP │ Build Configuration │ Version (Run Time) => 8.2.11 │ Version (Compile Time) => 8.2.9 │ Debug Build => no │ Thread Safety => disabled │ Settings │ Configuration File (php.ini) Path => /etc/php/8.2/cli │ Loaded Configuration File => /etc/php/8.2/cli/php.ini │ Scan this dir for additional .ini files => /etc/php/8.2/cli/conf.d │ Additional .ini files parsed => /etc/php/8.2/cli/conf.d/10-mysqlnd.ini, │ /etc/php/8.2/cli/conf.d/10-opcache.ini, │ /etc/php/8.2/cli/conf.d/10-pdo.ini, │ /etc/php/8.2/cli/conf.d/15-xml.ini, │ /etc/php/8.2/cli/conf.d/20-apcu.ini, │ /etc/php/8.2/cli/conf.d/20-assert.ini, │ /etc/php/8.2/cli/conf.d/20-bcmath.ini, │ /etc/php/8.2/cli/conf.d/20-bz2.ini, │ /etc/php/8.2/cli/conf.d/20-calendar.ini, │ /etc/php/8.2/cli/conf.d/20-ctype.ini, │ /etc/php/8.2/cli/conf.d/20-curl.ini, │ /etc/php/8.2/cli/conf.d/20-dom.ini, │ /etc/php/8.2/cli/conf.d/20-exif.ini, │ /etc/php/8.2/cli/conf.d/20-ffi.ini, │ /etc/php/8.2/cli/conf.d/20-fileinfo.ini, │ /etc/php/8.2/cli/conf.d/20-ftp.ini, │ /etc/php/8.2/cli/conf.d/20-gd.ini, │ /etc/php/8.2/cli/conf.d/20-gettext.ini, │ /etc/php/8.2/cli/conf.d/20-iconv.ini, │ /etc/php/8.2/cli/conf.d/20-igbinary.ini, │ /etc/php/8.2/cli/conf.d/20-imagick.ini, │ /etc/php/8.2/cli/conf.d/20-intl.ini, │ /etc/php/8.2/cli/conf.d/20-ldap.ini, │ /etc/php/8.2/cli/conf.d/20-mbstring.ini, │ /etc/php/8.2/cli/conf.d/20-msgpack.ini, │ /etc/php/8.2/cli/conf.d/20-mysqli.ini, │ /etc/php/8.2/cli/conf.d/20-pdo_mysql.ini, │ /etc/php/8.2/cli/conf.d/20-pdo_pgsql.ini, │ /etc/php/8.2/cli/conf.d/20-pdo_sqlite.ini, │ /etc/php/8.2/cli/conf.d/20-pgsql.ini, │ /etc/php/8.2/cli/conf.d/20-phar.ini, │ /etc/php/8.2/cli/conf.d/20-posix.ini, │ /etc/php/8.2/cli/conf.d/20-readline.ini, │ /etc/php/8.2/cli/conf.d/20-redis.ini, │ /etc/php/8.2/cli/conf.d/20-shmop.ini, │ /etc/php/8.2/cli/conf.d/20-simplexml.ini, │ /etc/php/8.2/cli/conf.d/20-soap.ini, │ /etc/php/8.2/cli/conf.d/20-sockets.ini, │ /etc/php/8.2/cli/conf.d/20-sqlite3.ini, │ /etc/php/8.2/cli/conf.d/20-sysvmsg.ini, │ /etc/php/8.2/cli/conf.d/20-sysvsem.ini, │ /etc/php/8.2/cli/conf.d/20-sysvshm.ini, │ /etc/php/8.2/cli/conf.d/20-tokenizer.ini, │ /etc/php/8.2/cli/conf.d/20-uploadprogress.ini, │ /etc/php/8.2/cli/conf.d/20-xmlreader.ini, │ /etc/php/8.2/cli/conf.d/20-xmlrpc.ini, │ /etc/php/8.2/cli/conf.d/20-xmlwriter.ini, │ /etc/php/8.2/cli/conf.d/20-xsl.ini, │ /etc/php/8.2/cli/conf.d/20-zip.ini, │ /etc/php/8.2/cli/conf.d/25-memcached.ini │ │ │ Directive => Local Value => Master Value │ xdebug.mode => debug => debug │ xdebug.start_with_request => default => default │ xdebug.start_upon_error => default => default │ xdebug.output_dir => /tmp => /tmp │ xdebug.use_compression => 0 => 0 │ xdebug.trigger_value => no value => no value │ xdebug.file_link_format => no value => no value │ xdebug.filename_format => no value => no value │ xdebug.log => no value => no value │ xdebug.log_level => 7 => 7 │ xdebug.var_display_max_children => 128 => 128 │ xdebug.var_display_max_data => 512 => 512 │ xdebug.var_display_max_depth => 3 => 3 │ xdebug.max_nesting_level => 256 => 256 │ xdebug.cli_color => 0 => 0 │ xdebug.force_display_errors => Off => Off │ xdebug.force_error_reporting => 0 => 0 │ xdebug.halt_level => 0 => 0 │ xdebug.max_stack_frames => -1 => -1 │ xdebug.show_error_trace => Off => Off │ xdebug.show_exception_trace => Off => Off │ xdebug.show_local_vars => Off => Off │ xdebug.dump.COOKIE => no value => no value │ xdebug.dump.ENV => no value => no value │ xdebug.dump.FILES => no value => no value │ xdebug.dump.GET => no value => no value │ xdebug.dump.POST => no value => no value │ xdebug.dump.REQUEST => no value => no value │ xdebug.dump.SERVER => no value => no value │ xdebug.dump.SESSION => no value => no value │ xdebug.dump_globals => On => On │ xdebug.dump_once => On => On │ xdebug.dump_undefined => Off => Off │ xdebug.profiler_output_name => cachegrind.out.%p => cachegrind.out.%p │ xdebug.profiler_append => Off => Off │ xdebug.cloud_id => no value => no value │ xdebug.client_host => 172.17.0.1 => 172.17.0.1 │ xdebug.client_port => 9000 => 9000 │ xdebug.discover_client_host => Off => Off │ xdebug.client_discovery_header => HTTP_X_FORWARDED_FOR,REMOTE_ADDR => HTTP_X_FORWARDED_FOR,REMOTE_ADDR │ xdebug.idekey => no value => no value │ xdebug.connect_timeout_ms => 200 => 200 │ xdebug.scream => Off => Off │ xdebug.gc_stats_output_name => gcstats.%p => gcstats.%p │ xdebug.trace_output_name => trace.%c => trace.%c │ xdebug.trace_format => 0 => 0 │ xdebug.trace_options => 0 => 0 │ xdebug.collect_assignments => Off => Off │ xdebug.collect_return => Off => Off
相关截图


解决方案
1. 修复Xdebug客户端连接问题
从诊断日志看,Xdebug无法连接到172.17.0.1:9000,需要:
- 确认本地IDE的调试监听端口是9000,且未被占用
- 在ddev容器中开启
xdebug.discover_client_host=on,让Xdebug自动检测客户端主机,替代固定的172.17.0.1 - 或者手动设置正确的客户端主机IP:在ddev中执行
hostname -I获取宿主机可访问的IP,替换xdebug.client_host的值
2. 强制Xdebug在CLI模式下启动调试
Behat是CLI运行的,默认xdebug.start_with_request=default不会自动启动调试,需要修改配置:
- 设置
xdebug.start_with_request=yes,让Xdebug在所有请求(包括CLI)启动时自动触发调试 - 或者运行Behat时添加环境变量:
XDEBUG_SESSION=1 ddev behat,手动触发调试
3. 确保Behat使用正确的PHP配置
- 确认Behat在ddev容器中运行时,加载的是包含Xdebug配置的php.ini(即
/etc/php/8.2/cli/php.ini) - 执行
ddev exec php -v和ddev exec php -m,验证Xdebug已在CLI模式下加载
4. 检查断点有效性
- 确认上下文文件的路径在IDE中已正确映射到ddev容器内的路径(比如本地
./features/bootstrap/对应容器内/var/www/html/features/bootstrap/) - 断点设置在实际会被执行的代码行上,避免设置在注释、空行或未被调用的方法中
内容的提问来源于stack exchange,提问作者Mykyta Nalchadzhy
相关产品推荐
相关产品推荐

