94 words, 1 min read

Imagine you are using streamed downloads in your Laravel controllers like this:

use App\Services\GitHub;
return response()->streamDownload(function () {
echo GitHub::api('repo')
->contents()
->readme('laravel', 'laravel')['contents'];
}, 'laravel-readme.md');

I was doing this in a project and I wanted to test it. I wanted to make sure that the response was streamed and that the content was correct.

The way I tested it was like this:

/** @test */
public function it_streams_the_download()
{
$user = User::factory()->create();
$response = $this->actingAs($user)
->get('/my-endpoint')
->assertOk();
$content = $response->streamedContent();
$this->assertEquals('The content of the file', $content);
}

Thanks for this article for the inspiration.