php 如何让 XDebug 在 CLI 上与 PHPUnit 一起运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12104303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:49:54  来源:igfitidea点击:

How can I get XDebug to run with PHPUnit on the CLI?

phpphpunitcommand-line-interfacexdebug

提问by blacktie24

I've tried running the following CLI command:

我试过运行以下 CLI 命令:

phpunit -d xdebug.profiler_enable=on XYZTestCase.php

but it just runs as normal. Can anyone point me in the right direction?? Thx!

但它只是正常运行。谁能指出我正确的方向?谢谢!

Here's the XDebug settings:

这是 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

回答by edorian

The xdebug.profiler_enablesetting can't be changed at runtimebut only at the start of script.

xdebug.profiler_enable设置不能在运行时更改,只能在脚本开始时更改

Running phpunit -d foo=barwill just lead to phpunit calling ini_set("foo", "bar");and that doesn't work since the value can't change at runtime.

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

See: xdebug.profiler_enable

看: xdebug.profiler_enable

Enables Xdebug's profiler which creates files in the profile output directory. Those files can be read by KCacheGrind to visualize your data. This setting can not be set in your script with ini_set(). If you want to selectively enable the profiler, please set xdebug.profiler_enable_trigger to 1 instead of using this setting.

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

Solution:

解决方案:

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

By applying the setting directly to the PHP runtime and not phpunit it will be set beforethe script starts and should work.

通过将设置直接应用于 PHP 运行时而不是 phpunit,它将在脚本启动之前设置并且应该可以工作。

回答by timhc22

Spent ages trying to get this to work. Think this may change my life though!

花了很长时间试图让它发挥作用。认为这可能会改变我的生活!

I originally was trying to do this (i.e. run phpunit) inside a vagrant box but realised it was easier (and faster performance wise) running it outside the vagrant box.

我最初试图在 vagrant box 内执行此操作(即运行 phpunit),但意识到在 vagrant box 外运行它更容易(并且性能更快)。

First off I used brew install php55 php55-xdebugusing homebrew on a mac (but your configuration may be different and it should still work). My site is a symfony2 project.

首先,我brew install php55 php55-xdebug在 mac 上使用自制软件(但您的配置可能不同,它应该仍然有效)。我的网站是一个 symfony2 项目。

I was trying to follow this: phpunit vagrant xdebugto get it working from inside a vagrant box (almost got there but with some issues).

我试图按照这个:phpunit vagrant xdebug让它从一个 vagrant box 内部工作(几乎到了那里,但有一些问题)。

These settings worked for me (running site from a vagrant box, but phpunit outside vagrant box):

这些设置对我有用(从流浪箱运行站点,但在流浪箱外运行 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

Then running this at the command line (here I am using a download of phpunit instead of the one linked to in /usr/local/bin (which doesn't seem to work))

然后在命令行运行它(这里我使用的是 phpunit 的下载,而不是链接到 /usr/local/bin 中的那个(这似乎不起作用))

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

Or you can create a file called phpunit-debug (to store the XDEBUG_CONFIG environment variable) as outlined here: phpunit xdebug

或者您可以创建一个名为 phpunit-debug 的文件(用于存储 XDEBUG_CONFIG 环境变量),如下所述:phpunit xdebug

回答by David Jacquel

Did you try to :

您是否尝试过:

  1. Set your xdebug.idekeyin your php.ini to wathever you want (eg: blacktie)
  2. restart your server

  3. Call you script by adding -d xdebug.idekey=blacktie

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

  1. 设置你的xdebug.idekey在你的php.ini wathever你想要的(如:blacktie)
  2. 重启你的服务器

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

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

Hope that help.

希望有所帮助。

回答by David Harkness

The correct name of the setting is xdebug.profiler_enablewith an underscore. Change your command to this:

设置的正确名称xdebug.profiler_enable带有下划线。将您的命令更改为:

phpunit -d xdebug.profiler_enable=on XYZTestCase.php

回答by Salvatore Zappalà

You can run Xdebug from the command line by setting an environment variable beforehand, e.g.:

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

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

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

This worked for me.

这对我有用。

More information on the Xdebug documentation.

有关Xdebug 文档的更多信息。

回答by Jules Colle

The only thing I need to do in the terminal, while using VS code debugger (with WSL), is execute this command:

在使用 VS 代码调试器(使用 WSL)时,我在终端中唯一需要做的就是执行以下命令:

export XDEBUG_CONFIG="idekey=VSCODE"

after that I simply run my phpunit command as usual. i.e. phpunit ./tests/and the debugger will stop on the first breakpoint I have set.

之后,我只是像往常一样运行我的 phpunit 命令。即phpunit ./tests/调试器将在我设置的第一个断点处停止。

If the debugger pauses on unreachable code, you might want to toggle of "Everything" in the debugger pane under Breakpoints.

如果调试器在无法访问的代码上暂停,您可能希望在断点下的调试器窗格中切换“一切”。

I got this info from https://tighten.co/blog/configure-vscode-to-debug-phpunit-tests-with-xdebug

我从https://tighten.co/blog/configure-vscode-to-debug-phpunit-tests-with-xdebug得到了这个信息

回答by Walf

Assuming you already have Xdebug working from your editor/standalone debugger when triggered by cookie/post/get variables, add a shell script to do the same triggering, so you have less to remember:

假设您已经在编辑器/独立调试器中使用了由 cookie/post/get 变量触发的 Xdebug,请添加一个 shell 脚本来执行相同的触发,因此您无需记住:

Create ~/bin/php-cli-debug.sh:

创建~/bin/php-cli-debug.sh

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

Then to debug things on the CLI, use something like:

然后在 CLI 上调试东西,使用类似的东西:

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

Make sure your .bashrchas added ~/binto your $PATH.

确保您.bashrc已添加~/bin到您的$PATH.

回答by Onur Demir

First, my environment:

一、我的环境:

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

php.ini:

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:

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>

I made a test run configuration on phpstorm like this: Click the button in the red circleConfiguration Panel

我在 phpstorm 上做了一个这样的测试运行配置: 点击红圈内的按钮配置面板

After making run configuration, the below command was run when I clicked debug button in 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

Please notice the --teamcity. I have no idea about it :) Also notice the back and forward slashes.

注意--teamcity. 我不知道它:) 还要注意前后斜线。

I hope this helps everyone.

我希望这对大家有帮助。

回答by ChrisDeDavidMindflowAU

On Windows with phpStorm

带有 phpStorm 的 Windows 上

Enter this into the command line:

在命令行中输入:

set XDEBUG_CONFIG="idekey=PHPSTORM"

This will add an environment variable that phpStorm will lookout for.

这将添加一个 phpStorm 将查找的环境变量。