如何在CLI中使用PHPUnit运行XDebug?

69

我尝试运行以下命令行命令:

phpunit -d xdebug.profiler_enable=on XYZTestCase.php

但是它只是正常运行。有人可以指点我吗?谢谢!

以下是XDebug设置:

xdebug

xdebug support => enabled
Version => 2.1.2

Supported protocols => Revision
DBGp - Common DeBuGger Protocol => $Revision: 1.145 $

Directive => Local Value => Master Value
xdebug.auto_trace => Off => Off
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.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.extended_info => On => On
xdebug.file_link_format => no value => no value
xdebug.idekey => Nam => no value
xdebug.manual_url => http://www.php.net => http://www.php.net
xdebug.max_nesting_level => 100 => 100
xdebug.overload_var_dump => On => On
xdebug.profiler_aggregate => Off => Off
xdebug.profiler_append => Off => Off
xdebug.profiler_enable => Off => Off
xdebug.profiler_enable_trigger => Off => Off
xdebug.profiler_output_dir => c:/wamp/tmp => c:/wamp/tmp
xdebug.profiler_output_name => cachegrind.out.%t.%p => cachegrind.out.%t.%p
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_handler => dbgp => dbgp
xdebug.remote_host => localhost => localhost
xdebug.remote_log => no value => no value
xdebug.remote_mode => req => req
xdebug.remote_port => 9000 => 9000
xdebug.scream => Off => Off
xdebug.show_exception_trace => Off => Off
xdebug.show_local_vars => Off => Off
xdebug.show_mem_delta => Off => Off
xdebug.trace_format => 0 => 0
xdebug.trace_options => 0 => 0
xdebug.trace_output_dir => \ => \
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

你确定已经安装了Xdebug吗?从 php --info | grep xdebug 的输出是什么? - David Harkness
@DavidHarkness 我已经安装了XDebug。当我在Web浏览器中传递XDEBUG_SESSION_START时,我可以让它工作,但是当我在CLI上执行脚本时,我就无法让它工作。 - blacktie24
你打算选择最佳/正确的答案吗? - tread
9个回答

56

xdebug.profiler_enable设置不能在运行时更改,只能在脚本启动时进行更改。

运行phpunit -d foo=bar只会导致phpunit调用ini_set("foo", "bar");这并不起作用,因为该值无法在运行时更改。

请参阅:xdebug.profiler_enable

启用Xdebug的分析器,它会在分析输出目录中创建文件。可以使用KCacheGrind读取这些文件以可视化数据。无法在脚本中使用ini_set()来设置此设置。如果要选择性地启用分析器,请将xdebug.profiler_enable_trigger设置为1,而不是使用此设置。

解决方案:

php -d xdebug.profiler_enable=on /usr/bin/phpunit XYZTestCase.php

将设置直接应用于PHP运行时,而非PHPUnit,它将在脚本启动之前设置并应该可以工作。


4
请尝试使用以下命令:-dxdebug.profiler_enable=On -dxdebug.remote_host=127.0.0.1 -dxdebug.remote_port=9000 /path/to/phpunit XYZTestCase.php。该命令用于启用性能分析器并将调试信息发送到本地主机的9000端口,以便调试PHPUnit测试用例XYZTestCase.php。 - JhovaniC
1
指定输出路径:php -d xdebug.profiler_enable=on -d xdebug.profiler_output_dir=/my_path /usr/bin/phpunit XYZTestCase.php - Nicolas BADIA

14

我花了很长时间才让这个工作起来。不过我认为这可能会改变我的生活!

最初我试图在vagrant虚拟机中运行phpunit,但后来意识到在vagrant之外运行它更容易(而且性能更快)。首先,我在Mac上使用Homebrew安装了brew install php55 php55-xdebug(但你的配置可能不同,仍然可以工作)。我的网站是一个Symfony2项目。

我试图遵循这篇文章:phpunit vagrant xdebug,在vagrant虚拟机中让其工作(基本接近成功,但还有一些问题)。

这些设置对我有用(从vagrant虚拟机中运行网站,但在vagrant之外运行phpunit):

#xdebug.ini (parent machine, not inside vagrant box).
[xdebug]
zend_extension="/usr/local/Cellar/php55-xdebug/2.2.6/xdebug.so" #this will be different on your machine and will probably already be set

xdebug.max_nesting_level = 250 
xdebug.default_enable = 1
xdebug.idekey = "PHPSTORM" #seems to work without this too
xdebug.remote_enable = 1

然后在命令行运行以下命令(这里我使用的是 phpunit 的下载版本,而不是 /usr/local/bin 中链接的版本(似乎无法正常工作))

XDEBUG_CONFIG="idekey=PHPSTORM" bin/phpunit -c app

或者您可以按照这里的步骤创建一个名为phpunit-debug的文件(用于存储XDEBUG_CONFIG环境变量):phpunit xdebug


设置-d xdebug.idekey=PHPSTORM对我来说似乎没有起作用,因此使用XDEBUG_CONFIG环境变量。 - timhc22

7

您是否尝试过以下操作:

  1. 将您的xdebug.idekey在php.ini文件中设置为您想要的任何值(例如:blacktie)
  2. 重新启动您的服务器

  3. 通过添加-d xdebug.idekey=blacktie来调用您的脚本

    phpunit -d xdebug.profiler_enable=on -d xdebug.idekey=blacktie XYZTestCase.php

希望这些操作对您有所帮助。


5

当我使用VS code调试器(带有WSL)时,在终端中唯一需要做的事情就是执行这个命令:

export XDEBUG_CONFIG="idekey=VSCODE"

之后,我按照惯例运行我的phpunit命令,即phpunit ./tests/,调试器将停在我设置的第一个断点处。

如果调试器在无法访问的代码上暂停,您可能需要在断点下方的调试器窗格中切换“全部”选项。

我从https://tighten.co/blog/configure-vscode-to-debug-phpunit-tests-with-xdebug获取了这些信息。

::编辑::

尽管在我的本地web服务器上可以使用上述方法。但今天我尝试将我的项目移到docker时遇到了问题。

事实证明,使用以下设置将无法正常工作:

xdebug.remote_connect_back=1

但是你需要设置这个代替:
xdebug.remote_host=172.17.0.1
xdebug.remote_connect_back=0

注意:我在docker容器内部使用172.17.0.1来指代我的主机,因为我正在使用Linux,但如果你在Mac上,最好使用以下内容:
xdebug.remote_host=docker.for.mac.host.internal
xdebug.remote_connect_back=0

或在Windows上:

xdebug.remote_host=host.docker.internal
xdebug.remote_connect_back=0

使用Docker似乎不再需要执行export XDEBUG_CONFIG="idekey=VSCODE"的步骤了。

这对我在Ubuntu上起作用,尽管我因Ubuntu为apache/CLI/fpm设置单独的php.ini文件而卡了一段时间。如果有人通过浏览器但无法通过命令行使用XDebug,请确保已将XDebug启用选项添加到每个适当的php.ini文件中-从命令行的php -v输出将包含一些XDebug信息,如果您的CLI PHP配置正确设置了XDebug。 - Brian Dunne

4
正确的设置名为 xdebug.profiler_enable,其中有一个下划线。请将您的命令更改为以下内容:
phpunit -d xdebug.profiler_enable=on XYZTestCase.php

David,我修复了设置的名称,但它仍然无法工作:(还有其他想法吗? - blacktie24
1
@blacktie24 - 将 var_dump(ini_get('xdebug.profiler_enable')) 添加到您的 bootstrap.php 文件中,以验证 PHPUnit 是否应用了该设置。 - David Harkness

4

您可以通过事先设置环境变量来从命令行运行Xdebug,例如:

export XDEBUG_CONFIG="idekey=YOUR_IDE_KEY remote_host=localhost remote_enable=1"

这对我有效。

有关Xdebug文档的更多信息。


请确保 idekey 与您的 IDE/调试器中设置的相同。 - Salvatore Zappalà

0

首先,我的环境:

  • WampServer 版本3.1.3 64位
  • Apache 2.4.33 - PHP 7.1.16
  • MySQL 5.7.21
  • MariaDB 10.2.14

php.ini:

[xdebug]
zend_extension ="c:/wamp64/bin/php/php7.1.16/zend_ext/php_xdebug-2.6.0-7.1-vc14-x86_64.dll"

xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.profiler_enable = 1
xdebug.profiler_enable_trigger = 1
xdebug.profiler_output_name = cachegrind.out.%t.%p
xdebug.profiler_output_dir ="c:/wamp64/tmp"
xdebug.show_local_vars=0
xdebug.idekey = "PHPSTORM" #seems to work without this too

phpunit.xml:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="bootstrap/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory suffix="Test.php">./tests</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
    </php>
</phpunit>

我在phpstorm上进行了一个测试运行配置,如下所示: 点击红圈中的按钮 配置面板 在进行运行配置后,当我点击PHPSTORM的调试按钮时,会运行以下命令。
C:\wamp64\bin\php\php7.1.16\php.exe
-dzend_extension=C:\wamp64\bin\php\php7.1.16\zend_ext\php_xdebug-2.6.0-7.1-vc14-x86_64.dll
-dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9000 -dxdebug.remote_host=127.0.0.1 C:/wamp64/www/<PROJECT_FOLDER>/vendor/phpunit/phpunit/phpunit --bootstrap C:\wamp64\www\<PROJECT_FOLDER>\vendor\autoload.php --configuration C:\wamp64\www\<PROJECT_FOLDER>\phpunit.xml --teamcity

请注意--teamcity。我对此一无所知 :) 还要注意反斜杠和正斜杠。 希望这能帮到大家。

0

假设您已经在编辑器/独立调试器中使用cookie/post/get变量触发Xdebug,现在添加一个shell脚本来执行相同的触发操作,这样您就不必记住太多内容:

创建~/bin/php-cli-debug.sh文件:

#!/bin/bash
phpfile="$1"
idekey=YOUR_IDE_KEY
shift 1
php -d'xdebug.remote_enable=1' -d'xdebug.remote_autostart=1' -d'xdebug.idekey='"$idekey" -f "$phpfile" -- "$@"

然后要在CLI上调试东西,可以使用类似以下的命令:

$ php-cli-debug.sh "$(which phpunit)" --bootstrap tests/bootstrap.php tests/FooBarTest | less -S

请确保您的.bashrc已将~/bin添加到您的$PATH中。

0
在 Windows 上使用 phpStorm:
在命令行中输入以下内容:
set XDEBUG_CONFIG="idekey=PHPSTORM"

这将添加一个环境变量,phpStorm将会寻找它。


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