簡體   English   中英

如何讓 XDebug 在 CLI 上與 PHPUnit 一起運行?

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

我試過運行以下 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_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,它將在腳本啟動之前設置並且應該可以工作。

花了很長時間試圖讓它發揮作用。 認為這可能會改變我的生活!

我最初試圖在 vagrant box 內執行此操作(即運行 phpunit),但意識到在 vagrant box 外運行它更容易(並且性能更快)。

首先,我在 Mac 上使用 homebrew 使用brew install php55 php55-xdebug (但您的配置可能不同,它應該仍然有效)。 我的網站是一個 symfony2 項目。

我試圖按照這個: phpunit vagrant xdebug讓它從一個 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

然后在命令行運行它(這里我使用的是 phpunit 的下載,而不是鏈接到 /usr/local/bin 中的那個(這似乎不起作用))

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

或者您可以創建一個名為 phpunit-debug 的文件(用於存儲 XDEBUG_CONFIG 環境變量),如下所述: phpunit xdebug

您是否嘗試過:

  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

希望有所幫助。

設置的正確名稱是帶有下划線的xdebug.profiler_enable 將您的命令更改為:

phpunit -d xdebug.profiler_enable=on XYZTestCase.php

您可以通過預先設置環境變量從命令行運行 Xdebug,例如:

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

這對我有用。

有關Xdebug 文檔的更多信息。

在使用 VS 代碼調試器(使用 WSL)時,我唯一需要在終端中執行以下命令:

export XDEBUG_CONFIG="idekey=VSCODE"

之后,我只是像往常一樣運行我的 phpunit 命令。 phpunit ./tests/調試器將在我設置的第一個斷點處停止。

如果調試器在無法訪問的代碼上暫停,您可能希望在斷點下的調試器窗格中切換“一切”。

我從https://tighten.co/blog/configure-vscode-to-debug-phpunit-tests-with-xdebug得到了這個信息

:: 編輯 ::

雖然上述工作在我的本地網絡服務器上。 今天,當我試圖將我的項目移植到 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

或在窗戶上:

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

使用export XDEBUG_CONFIG="idekey=VSCODE"似乎我也不需要再執行export XDEBUG_CONFIG="idekey=VSCODE"了。

假設您已經在編輯器/獨立調試器中使用了由 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 位
  • 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 我不知道它:) 還要注意前后斜線。

我希望這對大家有幫助。

帶有 phpStorm 的 Windows 上

在命令行中輸入:

set XDEBUG_CONFIG="idekey=PHPSTORM"

這將添加一個 phpStorm 將查找的環境變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM