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

前端 未结 3 2069
一生所求
一生所求 2021-01-17 10:02

I am writing functional tests with Symfony2.

I have a controller that calls a getImage() function which streams an image file as follows:



        
3条回答
  •  遇见更好的自我
    2021-01-17 10:23

    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

提交回复
热议问题