【问题标题】:Is it possible to debug API requests on localhost while testing using PHPUnit and PhpStorm?在使用 PHPUnit 和 PhpStorm 进行测试时,是否可以在 localhost 上调试 API 请求?
【发布时间】:2019-11-09 05:33:03
【问题描述】:

我的配置可以使用 PHPUnit 在 PhpStorm 中成功测试我的 localhost API。我也可以在断点处停止,但只能在我的 TestCase 类中。

我需要在被测试的逻辑内部设置的断点处停止,但它不会停在那里。

测试命令:

/usr/bin/php -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9000 -dxdebug.remote_host=127.0.0.1 /foo/vendors/composer/phpunit/phpunit/phpunit --configuration /foo/_stuff/phpunit/config-api2.xml --teamcity

我的配置 xml:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit
    bootstrap="../../foo/tests/app_test_case.php"
    colors="true"
    stopOnFailure="false"
>
    <testsuites>
        <testsuite name="foo">
            <file>../../foo.php</file>
        </testsuite>

添加到 php.ini:

xdebug.remote_autostart = 1

添加到请求的路线:

?XDEBUG_SESSION_START=PHPSTORM

并在index.php中添加cookie:

header('Cookie: XDEBUG_SESSION=PHPSTORM');

【问题讨论】:

    标签: phpunit phpstorm xdebug


    【解决方案1】:

    是的,你可以。

    这里的问题是您将如何为这些 API 调用启动调试会话。

    最常用的方法是将 xdebug.remote_autostart=1 添加到运行调用的解释器的 php.ini 中,以便为每个 PHP 调用启动调试会话,无论 API 与否。

    如果您对此不满意,可以在调用的 HTTP 请求中添加 XDEBUG_SESSION_START GET 参数,但这显然需要修改代码。

    【讨论】:

    • 更新了主帖。它仍然没有在我的逻辑代码断点处停止
    • 那么Xdebug remote log值得一看。
    • 在 xdebug 日志中它说断点没有命中:&lt;- breakpoint_set -i 31 -t line -f file:///home/pawel/PhpstormProjects/foo/bar/controllers/api_baz_controller.php -n 44 -&gt; &lt;response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" command="breakpoint_set" transaction_id="31" id="308570025"&gt;&lt;/response&gt;
    • 这一行表示 PhpStorm 正在通知 Xdebug 它有某些断点,但这并不意味着它是正确的调试会话或已到达断点。我想我欠你一些解释。您可能已将 remote_autostart 添加到 CLI 解释器的 php.ini 中。这是错误的,因为您需要调试的是 Web 请求,因此应该为 Web 服务器解释器配置 Xdebug。您向本地发出请求的 Web 服务器是?如果是这样,请加载 phpinfo() 并确认 Xdebug 配置正确。
    • fck,路径映射是问题,其他一切正常
    猜你喜欢
    • 2017-12-20
    • 2012-04-06
    • 2017-05-07
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    • 1970-01-01
    • 2020-03-26
    • 2016-05-19
    相关资源
    最近更新 更多