How to retrieve a streamed response (e.g. download a file) with Symfony test client

时光怂恿深爱的人放手 提交于 2019-12-01 15:06:06

The return value of sendContent (rather than getContent) is the callback that you've set. getContent actually just returns false in Symfony2

Using sendContent you can enable the output buffer and assign the content to that for your tests, like so:

$client = static::createClient();
$client->request('GET', $url);

// Enable the output buffer
ob_start();
// Send the response to the output buffer
$client->getResponse()->sendContent();
// Get the contents of the output buffer
$content = ob_get_contents();
// Clean the output buffer and end it
ob_end_clean();

You can read more on the output buffer here

The API for StreamResponse is here

For me didn't work like that. Instead, I used ob_start() before making the request, and after the request i used $content = ob_get_clean() and made asserts on that content.

In test:

    // Enable the output buffer
    ob_start();
    $this->client->request(
        'GET',
        '$url',
        array(),
        array(),
        array('CONTENT_TYPE' => 'application/json')
    );
    // Get the output buffer and clean it
    $content = ob_get_clean();
    $this->assertEquals('my response content', $content);

Maybe this was because my response is a csv file.

In controller:

    $response->headers->set('Content-Type', 'text/csv; charset=utf-8');
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!