使用PHPUnit时,未检测到PhpStorm调试器扩展。

34
我正在使用PhpStorm通过远程docker解释器来使用PHPUnit。容器是通过docker-compose运行的。
PHPUnit可以正常工作。Xdebug可以通过浏览器工作。在docker-php-ext-xdebug.ini中,我有所有必需的选项,并且我可以在CLI Interpreter Config中看到PhpStorm确实加载了这个配置。
但是当我尝试使用调试器运行PHPUnit时,我得到了以下错误信息:

未检测到PhpStorm调试器扩展

PhpStorm Debugger extension is not detected

CLI解释器配置:

CLI Interpreter Config

DockerFile

FROM php:7.4-fpm-alpine

# OS DEPENDENCIES
RUN apk update
RUN apk add --no-cache bash git curl libmcrypt libmcrypt-dev openssh-client icu-dev
RUN apk add --no-cache libxml2-dev freetype-dev libpng-dev libjpeg-turbo-dev zip libzip-dev g++ make autoconf
RUN apk add --no-cache postgresql-dev
RUN docker-php-source extract
RUN pecl install xdebug redis
RUN docker-php-ext-enable xdebug redis
RUN docker-php-source delete
RUN docker-php-ext-install -j$(nproc) pgsql
RUN docker-php-ext-install -j$(nproc) pdo_pgsql
RUN docker-php-ext-install soap intl zip
RUN docker-php-ext-install opcache

# XDEBUG CONFIGURATION
ARG XDEBUG_REMOTE_HOST
ARG XDEBUG_REMOTE_PORT
ARG XDEBUG_REMOTE_CONNECT_BACK
ARG XDEBUG_INI=/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

RUN echo "error_reporting = E_ALL" >> ${XDEBUG_INI}
RUN echo "display_startup_errors=1" >> ${XDEBUG_INI}
RUN echo "display_errors=1" >> ${XDEBUG_INI}
RUN echo "xdebug.profiler_enable=1" >> ${XDEBUG_INI}
RUN echo "xdebug.remote_enable=1" >> ${XDEBUG_INI}
RUN echo "xdebug.remote_connect_back=$XDEBUG_REMOTE_CONNECT_BACK" >> ${XDEBUG_INI}
RUN echo "xdebug.idekey=\"PHPSTORM\"" >> ${XDEBUG_INI}
RUN echo "xdebug.remote_handler=dbgp" >> ${XDEBUG_INI}
RUN echo "xdebug.remote_port=$XDEBUG_REMOTE_PORT" >> ${XDEBUG_INI}
RUN echo "xdebug.remote_host=$XDEBUG_REMOTE_HOST" >> ${XDEBUG_INI}
RUN echo "xdebug.remote_autostart=1" >> ${XDEBUG_INI}

# COMPOSER INSTALLATION
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# EXORT COMPOSER GLOBAL PATH
ENV PATH="$PATH:$HOME/.composer/vendor/bin" #edited

docker-compose.yaml

  dataapi:
    container_name: dataapi
    build:
      context: ${DOCKER_FILES_PATH}/privateapi/
      args:
        - XDEBUG_REMOTE_PORT=10000
        - XDEBUG_REMOTE_HOST=172.17.0.1 #DOCKER network IP
        - XDEBUG_REMOTE_CONNECT_BACK=0  #edited
    command: sh -c "composer install && bin/console doctrine:migrations:migrate --allow-no-migration -n && php-fpm"

更新:

这是phpunit中phpinfo()的结果:

sodium

sodium support => enabled
libsodium headers version => 1.0.18
libsodium library version => 1.0.18

SPL

SPL support => enabled
Interfaces => OuterIterator, RecursiveIterator, SeekableIterator, SplObserver, SplSubject
Classes => AppendIterator, ArrayIterator, ArrayObject, BadFunctionCallException, BadMethodCallException, CachingIterator, CallbackFilterIterator, DirectoryIterator, DomainException, EmptyIterator, FilesystemIterator, FilterIterator, GlobIterator, InfiniteIterator, InvalidArgumentException, IteratorIterator, LengthException, LimitIterator, LogicException, MultipleIterator, NoRewindIterator, OutOfBoundsException, OutOfRangeException, OverflowException, ParentIterator, RangeException, RecursiveArrayIterator, RecursiveCachingIterator, RecursiveCallbackFilterIterator, RecursiveDirectoryIterator, RecursiveFilterIterator, RecursiveIteratorIterator, RecursiveRegexIterator, RecursiveTreeIterator, RegexIterator, RuntimeException, SplDoublyLinkedList, SplFileInfo, SplFileObject, SplFixedArray, SplHeap, SplMinHeap, SplMaxHeap, SplObjectStorage, SplPriorityQueue, SplQueue, SplStack, SplTempFileObject, UnderflowException, UnexpectedValueException

sqlite3

SQLite3 support => enabled
SQLite Library => 3.32.1

Directive => Local Value => Master Value
sqlite3.defensive => 1 => 1
sqlite3.extension_dir => no value => no value

standard

Dynamic Library Support => enabled
Path to sendmail => /usr/sbin/sendmail -t -i

Directive => Local Value => Master Value
assert.active => 1 => 1
assert.bail => 0 => 0
assert.callback => no value => no value
assert.exception => 0 => 0
assert.quiet_eval => 0 => 0
assert.warning => 1 => 1
auto_detect_line_endings => 0 => 0
default_socket_timeout => 60 => 60
from => no value => no value
session.trans_sid_hosts => no value => no value
session.trans_sid_tags => a=href,area=href,frame=src,form= => a=href,area=href,frame=src,form=
unserialize_max_depth => 4096 => 4096
url_rewriter.hosts => no value => no value
url_rewriter.tags => form= => form=
user_agent => no value => no value

tokenizer

Tokenizer Support => enabled

xdebug

xdebug support => enabled
Version => 2.9.6
Support Xdebug on Patreon, GitHub, or as a business: https://xdebug.org/support

Debugger => enabled
IDE Key => PHPSTORM

Directive => Local Value => Master Value
xdebug.auto_trace => Off => Off
xdebug.cli_color => 0 => 0
xdebug.collect_assignments => Off => Off
xdebug.collect_includes => On => On
xdebug.collect_params => 0 => 0
xdebug.collect_return => Off => Off
xdebug.collect_vars => Off => Off
xdebug.coverage_enable => On => On
xdebug.default_enable => On => On
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.file_link_format => no value => no value
xdebug.filename_format => no value => no value
xdebug.force_display_errors => Off => Off
xdebug.force_error_reporting => 0 => 0
xdebug.gc_stats_enable => Off => Off
xdebug.gc_stats_output_dir => /tmp => /tmp
xdebug.gc_stats_output_name => gcstats.%p => gcstats.%p
xdebug.halt_level => 0 => 0
xdebug.idekey => PHPSTORM => PHPSTORM
xdebug.max_nesting_level => 256 => 256
xdebug.max_stack_frames => -1 => -1
xdebug.overload_var_dump => 2 => 2
xdebug.profiler_append => Off => Off
xdebug.profiler_enable => On => On
xdebug.profiler_enable_trigger => Off => Off
xdebug.profiler_enable_trigger_value => no value => no value
xdebug.profiler_output_dir => /tmp => /tmp
xdebug.profiler_output_name => cachegrind.out.%p => cachegrind.out.%p
xdebug.remote_addr_header => no value => no value
xdebug.remote_autostart => On => On
xdebug.remote_connect_back => Off => Off
xdebug.remote_cookie_expire_time => 3600 => 3600
xdebug.remote_enable => On => On
xdebug.remote_host => 172.17.0.1 => 172.17.0.1
xdebug.remote_log => no value => no value
xdebug.remote_log_level => 7 => 7
xdebug.remote_mode => req => req
xdebug.remote_port => 10000 => 10000
xdebug.remote_timeout => 200 => 200
xdebug.scream => Off => Off
xdebug.show_error_trace => Off => Off
xdebug.show_exception_trace => Off => Off
xdebug.show_local_vars => Off => Off
xdebug.show_mem_delta => Off => Off
xdebug.trace_enable_trigger => Off => Off
xdebug.trace_enable_trigger_value => no value => no value
xdebug.trace_format => 0 => 0
xdebug.trace_options => 0 => 0
xdebug.trace_output_dir => /tmp => /tmp
xdebug.trace_output_name => trace.%c => trace.%c
xdebug.var_display_max_children => 128 => 128
xdebug.var_display_max_data => 512 => 512
xdebug.var_display_max_depth => 3 => 3

xml

XML Support => active
XML Namespace Support => active
libxml2 Version => 2.9.10

xmlreader

XMLReader => enabled

xmlwriter

XMLWriter => enabled

Zend OPcache

Opcode Caching => Disabled
Optimization => Disabled
SHM Cache => Enabled
File Cache => Disabled
Startup Failed => Opcode Caching is disabled for CLI

Directive => Local Value => Master Value
opcache.blacklist_filename => no value => no value
opcache.consistency_checks => 0 => 0
opcache.dups_fix => Off => Off
opcache.enable => On => On
opcache.enable_cli => Off => Off
opcache.enable_file_override => Off => Off
opcache.error_log => no value => no value
opcache.file_cache => no value => no value
opcache.file_cache_consistency_checks => On => On
opcache.file_cache_only => Off => Off
opcache.file_update_protection => 2 => 2
opcache.force_restart_timeout => 180 => 180
opcache.huge_code_pages => Off => Off
opcache.interned_strings_buffer => 8 => 8
opcache.lockfile_path => /tmp => /tmp
opcache.log_verbosity_level => 1 => 1
opcache.max_accelerated_files => 10000 => 10000
opcache.max_file_size => 0 => 0
opcache.max_wasted_percentage => 5 => 5
opcache.memory_consumption => 128 => 128
opcache.opt_debug_level => 0 => 0
opcache.optimization_level => 0 => 0x7FFEBFFF
opcache.preferred_memory_model => no value => no value
opcache.preload => no value => no value
opcache.preload_user => no value => no value
opcache.protect_memory => Off => Off
opcache.restrict_api => no value => no value
opcache.revalidate_freq => 2 => 2
opcache.revalidate_path => Off => Off
opcache.save_comments => On => On
opcache.use_cwd => On => On
opcache.validate_permission => Off => Off
opcache.validate_root => Off => Off
opcache.validate_timestamps => On => On

zip

Zip => enabled
Zip version => 1.15.6
Libzip headers version => 1.6.1
Libzip library version => 1.6.1

zlib

ZLib Support => enabled
Stream Wrapper => compress.zlib://
Stream Filter => zlib.inflate, zlib.deflate
Compiled Version => 1.2.11
Linked Version => 1.2.11

Directive => Local Value => Master Value
zlib.output_compression => Off => Off
zlib.output_compression_level => -1 => -1
zlib.output_handler => no value => no value

Additional Modules

Module Name

Environment

Variable => Value
PATH => /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/.composer/vendor/bin
HOSTNAME => fb6d372627df
JETBRAINS_REMOTE_RUN => 1
IDE_PHPUNIT_PHPUNIT_PHAR => /var/www/privateapi/vendor/bin/.phpunit/phpunit-7.5-0/phpunit
IDE_PHPUNIT_VERSION => 7.5.20
PHPIZE_DEPS => autoconf         dpkg-dev dpkg       file        g++         gcc         libc-dev        make        pkgconf         re2c
PHP_INI_DIR => /usr/local/etc/php
PHP_EXTRA_CONFIGURE_ARGS => --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --disable-cgi
PHP_CFLAGS => -fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
PHP_CPPFLAGS => -fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
PHP_LDFLAGS => -Wl,-O1 -pie
GPG_KEYS => 42670A7FE4D0441C8E4632349E4FDC074A4EF02D 5A52880781F755608BF815FC910DEB46F53EA312
PHP_VERSION => 7.4.7
PHP_URL => https://www.php.net/distributions/php-7.4.7.tar.xz
PHP_ASC_URL => https://www.php.net/distributions/php-7.4.7.tar.xz.asc
PHP_SHA256 => 53558f8f24cd8ab6fa0ea252ca8198e2650160649681ce5230c1df1dc2b52faf
PHP_MD5 =>  
HOME => /root
APP_ENV => test
KERNEL_CLASS => App\Kernel
APP_AUTH_API_HOST => http://authapi
DATABASE_URL_TEST => postgresql://postgres:postgres@172.17.0.1:5433/privateapi_test
CORS_ALLOW_ORIGIN => ^https?://(localhost|127\.0\.0\.1)(:[0-9]+)?$

PHP Variables

Variable => Value
$_ENV['PATH'] => /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/.composer/vendor/bin
$_ENV['HOSTNAME'] => fb6d372627df
$_ENV['JETBRAINS_REMOTE_RUN'] => 1
$_ENV['IDE_PHPUNIT_PHPUNIT_PHAR'] => /var/www/privateapi/vendor/bin/.phpunit/phpunit-7.5-0/phpunit
$_ENV['IDE_PHPUNIT_VERSION'] => 7.5.20
$_ENV['PHPIZE_DEPS'] => autoconf        dpkg-dev dpkg       file        g++         gcc         libc-dev        make        pkgconf         re2c
$_ENV['PHP_INI_DIR'] => /usr/local/etc/php
$_ENV['PHP_EXTRA_CONFIGURE_ARGS'] => --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --disable-cgi
$_ENV['PHP_CFLAGS'] => -fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
$_ENV['PHP_CPPFLAGS'] => -fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
$_ENV['PHP_LDFLAGS'] => -Wl,-O1 -pie
$_ENV['GPG_KEYS'] => 42670A7FE4D0441C8E4632349E4FDC074A4EF02D 5A52880781F755608BF815FC910DEB46F53EA312
$_ENV['PHP_VERSION'] => 7.4.7
$_ENV['PHP_URL'] => https://www.php.net/distributions/php-7.4.7.tar.xz
$_ENV['PHP_ASC_URL'] => https://www.php.net/distributions/php-7.4.7.tar.xz.asc
$_ENV['PHP_SHA256'] => 53558f8f24cd8ab6fa0ea252ca8198e2650160649681ce5230c1df1dc2b52faf
$_ENV['PHP_MD5'] => 
$_ENV['HOME'] => /root
$_ENV['APP_ENV'] => test
$_ENV['KERNEL_CLASS'] => App\Kernel
$_ENV['APP_AUTH_API_HOST'] => http://authapi
$_ENV['DATABASE_URL_TEST'] => postgresql://postgres:postgres@172.17.0.1:5433/privateapi_test
$_ENV['CORS_ALLOW_ORIGIN'] => ^https?://(localhost|127\.0\.0\.1)(:[0-9]+)?$
$_ENV['APP_SECRET'] => $ecretf0rt3st
$_ENV['DATABASE_URL'] => 
$_ENV['SYMFONY_DOTENV_VARS'] => APP_SECRET,DATABASE_URL,SYMFONY_DEPRECATIONS_HELPER,PANTHER_APP_ENV
$_ENV['SYMFONY_DEPRECATIONS_HELPER'] => 999999
$_ENV['PANTHER_APP_ENV'] => panther
$_ENV['APP_DEBUG'] => 1

设置:

  • Ubuntu 20.04
  • PhpStorm 2020.1.4
  • Docker 19.03.12
  • docker-compose 1.26.0

"XDEBUG_REMOTE_CONNECT_BACK=1" 和 xdebug.remote_connect_back -- 尝试去掉这个(确保它等于 0)。如果启用了 xdebug.connect_back,则 xdebug.remote_host 将被忽略。对于网页调试,您可以使用 IP,但是对于 CLI 调试(PHPUnit 所需),请求数据中没有 IP...(注:这是关于 Xdebug 连接的问题,而不是实际的 Xdebug 扩展检测)。 - LazyOne
我建议创建一些虚假的PHPUnit测试并尝试进行调试:在那里获取 phpinfo() 输出(特别是 Xdebug 部分;保存到文件中),看看那里会有什么值。也许它在那个时候以某种未知的原因具有不同的值。 - LazyOne
我已经对connect_back进行了更改,但它仍然不起作用。 我使用 phpinfo() 的输出编辑了问题。 - BastienSander
Xdebug 部分看起来对我来说没问题 - 就我所看到的,它已经存在并启用了。你能否启用 Xdebug 日志并尝试调试(你的 PHPUnit 测试;也可以尝试一个网页,但请确保它们是完全独立的日志)- 它会显示什么? - LazyOne
1
  1. 请展示您从IDE中的PHPUnit设置 - 这可能是您端上IDE配置错误(未为项目选择PHP解释器)。这个想法基于此评论:https://youtrack.jetbrains.com/issue/WI-51570#focus=Comments-27-4231759.0-0
- LazyOne
4个回答

111

你的 Xdebug 设置看起来很正常。它显示了 PHPUnit 的预期值,并且可以用于网页调试。

问题应该出在 IDE 的设置上,或者是一些 IDE 配置有误。尤其需要确保已选择默认的 PHP 解释器。即使在“运行/调试配置”中指定了一个解释器,PhpStorm 仍然需要选择一个项目默认的解释器--这是一个已知的限制: WI-51570

设置/首选项|语言和框架|PHP|CLI 解释器

enter image description here

注意:2021.1版本开始,PHP 设置节点已经移到顶层,现在是设置/首选项|PHP|CLI 解释器

enter image description here

P.S.你也可以通过文件|新建项目设置|新项目设置...进行设置。这样在计算机上创建所有未来的新项目时都会设置它(然后您可以按项目基础进行需要更改)。


13
对我来说,设置默认的CLI解释器也解决了问题。前往“设置”>“语言和框架”>“PHP”>“CLI解释器”,然后设置您需要的任何一个。 - Oliver Baumann
2
设置项目范围的默认解释器对我也有效。 - Gruffy
到这里后发现我大约一年前已经赞过了这个回答。问题在2022.1版本中仍然存在。@JetBrains请问更新错误信息或在点击时显示有帮助的弹出窗口需要付出多少努力呢? - obe

8
我遇到了这个问题。 我通过在“首选项> PHP”中指定CLI解释器来解决它。

输入映像描述


2
这在您的Docker中将不起作用,因为在运行容器时这些步骤将被删除:
RUN echo 'export PATH="$PATH:$HOME/.composer/vendor/bin"' >> ~/.bashrc
RUN source ~/.bashrc

您可以使用:

ENV PATH="$PATH:$HOME/.composer/vendor/bin"

更新

您也可以不在配置文件中使用IP,而是在启动容器时将端口映射到您的主机上。

首先将XDEBUG_REMOTE_HOST=172.17.0.1更改为XDEBUG_REMOTE_HOST=0.0.0.0


我已经进行了更改并重新构建了图像,但是行为仍然相同。 - BastienSander
1
请问您是如何启动容器的?您是否正确设置了端口和网络,以便您的IDE可以找到它们? - LinPy
我会添加这些信息。 XDebug 可以与浏览器一起使用,但无法与 PHPUnit 一起使用。 - BastienSander
我看到了你的更新。我不太明白为什么我要这样做,因为我已经让XDebug工作了,只是没有与PHPUnit一起使用。 我测试了你的更新,但它并没有变得更好。现在,如果我想要在浏览器中使用xdebug,它会告诉我端口10000已被占用(这是由于端口映射引起的)。 - BastienSander
1
端口:-“10000:10000” 您不需要它:是 Xdebug 连接到 IDE,而不是反过来。有了这个设置,您告诉 Docker 使用该端口,现在 IDE 无法侦听该端口(也无法接受传入连接)。 - LazyOne
@LazyOne 我也是这样理解的。但这并没有解决我的问题 :( - BastienSander

1
我曾遇到过同样的问题。然而,事实证明在XDebug 3中,端口已更改为9003。
Ctrl + Alt + S > 语言和框架 > PHP > 调试 > 调试端口设置为9003

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接