如何使用 Behat 和 BrowserKitDriver 处理 StreamedResponse?

How to handle StreamedResponse with Behat and the BrowserKitDriver?

我有一些控制器代码,其中 returns 一个 StreamedResponse 包含一个 Excel 文件。

我的行为测试 运行 使用 Symfony2Extension 及其 BrowserKitDriver 而不是基于浏览器的驱动程序。

我有一个看起来像这样的场景:

Scenario: Download link visible and returns success code
    When I am on the report page
    And I follow "Download report"
    Then the response status code should be 200

"Download report"link去returns一个StreamedResponse的控制器。在这个简单的测试中,我对响应的内容不感兴趣,只是生成它时没有错误。

以上场景运行并通过!然而,就在 And I follow "Download report" 步骤之前(无论是使用标准格式化程序还是 progress ),behat 输出 30 行左右的二进制 "junk" 到屏幕,这大概是一部分(或all) 流响应。

鉴于这种情况已经过去,我只想去掉 "junk" 输出。这可能吗?

有一个类似的 SO 问题 here,但它使用 PHP 的 get_headers 方法,假设我有一个 URL 可以浏览 - 事实并非如此据我所知,使用 Symfony 驱动程序,因为它实际上并没有发出 HTTP 请求。

在另一个项目中,我通过添加 BinaryFileResponse::trustXSendfileTypeHeader(); 返回了 BinaryFileResponse"outsourced" the streaming to nginx 但我想知道是否有办法使用正常的 StreamedResponse 代替。

我使用略微修改的 'when i press button' 步骤来清理控制台输出来处理 streamedresponse

/**
 * @When /^(?:|I )press "(?P<button>(?:[^"]|\")*)" handling a streamedresponse$/
 *
 * @throws
 */
public function pressButtonWithStreamedResponse($button)
{
    ob_start();

    $button = $this->fixStepArgument($button);
    $this->getSession()->getPage()->pressButton($button);

    return ob_get_clean();
}