Skip to content
Advertisement

How to test a Symfony BinaryFileResponse with php-unit

I have written an action which create a temporary file and returned it with a BinaryFileResponse and delete it after. Something like this :

JavaScript

Where $filename refers to a temporary file.

It’s working great, my file is sent and deleted after. But I cannot test it.

Here the summary of my test :

JavaScript

But during the time of the test the file has already been deleted…

JavaScript

I have found bug request on the symfony github but no solution yet. Any idea of how I can achieve this ?

My ideas so far :

1 Remove deleteFileAfterSenddepending on the environment, but I found this solution quit ugly. 2 Stop using WebTestCase and start using cURL but I don’t want to lose code coverage and it seems to be a lot of work.

Advertisement

Answer

I finally found a solution, I just send the file without using BinaryFileResponse.

JavaScript

To delete the file after sending, just save the result of file_get_contents in a variable and delete the file, then send the response.

For testing, it’s really easy :

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement