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

前端 未结 3 2035
一生所求
一生所求 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:30

    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');
    

提交回复
热议问题