symfonybehat

How to handle StreamedResponse with Behat and the BrowserKitDriver?


I've got some controller code which returns a StreamedResponse containing an Excel file.

My behat tests are running using the Symfony2Extension and its BrowserKitDriver rather than a browser-based driver.

I have a scenario which looks something like this:

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

The "Download report" link goes the controller which returns a StreamedResponse. In this simple test, I'm not interested in the contents of the response, just that there were no errors when generating it.

The above scenario runs and passes! However, immediately before the And I follow "Download report" step (whether using the standard formatter or the progress one), behat outputs 30 or so lines of binary "junk" to the screen, which presumably is part (or all) of the streamed response.

Given that this scenario is passing, I'd just like to get rid of the "junk" output. Is this possible?

There's a similar SO question here, but that uses PHP's get_headers method which assumes I have a URL to browse to - that's not the case with the Symfony driver as far as I can see since that doesn't actually make HTTP requests.

On another project, I've instead returned a BinaryFileResponse and "outsourced" the streaming to nginx by adding BinaryFileResponse::trustXSendfileTypeHeader(); But I'd like to know if there's a way to use a normal StreamedResponse instead.


Solution

  • I clean up the console output using a slightly modified 'when i press button' step to handle 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();
    }