#development #laravel #php #testing
Imagine you are using streamed downloads in your Laravel controllers like this:
1use App\Services\GitHub;
2
3return response()->streamDownload(function () {
4 echo GitHub::api('repo')
5 ->contents()
6 ->readme('laravel', 'laravel')['contents'];
7}, '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:
1/** @test */
2public function it_streams_the_download()
3{
4 $user = User::factory()->create();
5
6 $response = $this->actingAs($user)
7 ->get('/my-endpoint')
8 ->assertOk();
9
10 $content = $response->streamedContent();
11 $this->assertEquals('The content of the file', $content);
12}
Thanks for this article for the inspiration.
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.