如何让XDebug在CLI上使用PHPUnit运行?

如何让XDebug在CLI上使用PHPUnit运行?,php,phpunit,command-line-interface,xdebug,Php,Phpunit,Command Line Interface,Xdebug,我已尝试运行以下CLI命令: 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

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

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.profiler\u enable
。将命令更改为:

phpunit -d xdebug.profiler_enable=on XYZTestCase.php
您是否尝试:

  • 将php.ini中的xdebug.idekey设置为所需的值(例如: (黑铁)
  • 重新启动服务器

  • 通过添加-d xdebug.idekey=blacktie调用脚本

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


  • 希望有帮助。

    在运行时不能更改
    xdebug.profiler\u enable
    设置,只能在脚本开始时更改

    运行
    phpunit-dfoo=bar
    只会导致phpunit调用
    ini_集(“foo”,“bar”)这不起作用,因为该值在运行时无法更改

    见:

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

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

    花了很多时间试图让它工作。我想这可能会改变我的生活

    我最初试图在一个“漫游”框内运行phpunit,但意识到在“漫游”框外运行phpunit更容易(而且性能更快)

    首先,我使用了在mac上使用自制的
    brew安装php55 php55 xdebug
    (但您的配置可能不同,它仍然可以工作)。我的网站是一个symfony2项目

    我试图做到这一点:让它在一个流浪者的盒子里工作(几乎到了那里,但有一些问题)

    这些设置对我有效(从一个流浪者盒子运行站点,但在流浪者盒子外phpunit):

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


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

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

    export XDEBUG\u CONFIG=“idekey=YOUR\u IDE\u KEY remote\u host=localhost remote\u enable=1”

    这对我有用


    有关的详细信息。

    假设在由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

    首先,我的环境:

    • WampServer版本3.1.3 64位
    • Apache2.4.33-PHP7.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中的debug按钮时,下面的命令被运行

    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
    
    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\u enable=1-dxdebug.remote\u mode=req-dxdebug.remote\u port=9000-dxdebug.remote\u host=127.0.0.1 C:/wamp64/www//vendor/phpunit/phpunit/phpunit--bootstrap C:\wamp64\www\\vendor\autoload.php--configuration C:\wamp64\www\\phpunit.xml--teamcity
    
    请注意
    --teamcity
    。我对此一无所知:)同时请注意前后斜杠。


    我希望这对每个人都有帮助。

    在命令行中输入以下内容:

    set XDEBUG_CONFIG="idekey=PHPSTORM"
    

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

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

    export XDEBUG_CONFIG="idekey=VSCODE"
    
    之后,我只需像往常一样运行phpunit命令。i、 e.
    phpunit./tests/
    ,调试器将在我设置的第一个断点处停止

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

    我从你那里得到这个信息

    ::编辑::

    而上面的工作在我的本地Web服务器上。今天,当我试图将我的项目移植到docker时,我被这个问题咬了一口

    结果表明,此设置不起作用:

    xdebug.remote_connect_back=1
    
    但您需要设置以下选项:

    xdebug.remote_host=172.17.0.1
    xdebug.remote_connect_back=0
    
    注意:我使用172.17.0.1从docker容器中引用我的主机,因为我在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,我似乎不再需要导出XDEBUG\u CONFIG=“idekey=VSCODE”

    您确定安装了XDEBUG吗?php--info | grep xdebug的输出是什么?@DavidHarkness我已经安装了xdebug。当我在web浏览器中通过XDEBUG_SESSION_START时,我可以让它工作,但在CLI上执行脚本时,我就是无法让它工作。你要选择最佳/最正确的答案吗?David,我已修复了设置的名称,但它仍然不工作:(还有其他想法吗?@blacktie24-Add
    var_dump(ini_get('XDEBUG.profiler_enable'))
    到您的
    bootstrap.php
    文件,以验证设置是否由
    xdebug.remote_host=172.17.0.1
    xdebug.remote_connect_back=0
    
    xdebug.remote_host=docker.for.mac.host.internal
    xdebug.remote_connect_back=0
    
    xdebug.remote_host=host.docker.internal
    xdebug.remote_connect_back=0