symfony: Terminate kernel before sending BinaryFileResponse content during test

Hi,

I’m having an issue during testing an action which return a BinaryFileResponse.

My use case is:

  • We copy a file from a remote server in a temporary directory on our app ;
  • We do stuff on it ;
  • We return a BinaryFileResponse ;
  • And on the kernel.terminate event we remove the file.

It’s working fine but when I try to test it, I have this error:

fopen(... /tmp/7fe716dd333c2dc8eec1b0d0f212037608f4de5c.pdf): failed to open stream: No such file or directory

... /vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/BinaryFileResponse.php:266
... /vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Client.php:221
... /vendor/symfony/symfony/src/Symfony/Component/BrowserKit/Client.php:300
... /src/AppBundle/Tests/Tests/Controller/DocumentControllerTest.php:15
... /vendor/phpunit/phpunit/phpunit:47

By searching, it seems that the BinaryFileResponse::sendContent is called after the terminate kernel event, which is not the case in not testing environment.

Thanks

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 18 (10 by maintainers)

Most upvoted comments

Thanks a lot for helping out here!

Not using X-Send-File but my tests fail when using this code in controller:

$response = $app->sendFile($someFile->getRealPath());
$response->deleteFileAfterSend(true);
return $response;

and

ob_start();
$client->request('POST', $url);
$client->getResponse()->sendContent();
$content = ob_get_clean();

in the tests.

Doing something similar but using curl works just as expected, even within the same web test class.

Couldn’t you use $response->deleteFileAfterSend(true); instead of deleting the file manually in kernel.terminate?