How to write Laravel feature test for streamed content to browser?

I could use some help writing a feature test for a route that returns streamed content. Here’s the details…

I have a Laravel route:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Route::get('/export/user/role/{role}', [AdminExportUserController::class, 'export'])->name('export.user');
</code>
<code>Route::get('/export/user/role/{role}', [AdminExportUserController::class, 'export'])->name('export.user'); </code>
Route::get('/export/user/role/{role}', [AdminExportUserController::class, 'export'])->name('export.user');

that will call the export controller method below. The export() method gets a collection of users from the database and streams rows of users in ‘.csv’ format to the browser.

Here’s the export method:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><?php
namespace AppHttpControllersAdmin;
use AppModelsUser;
use AppEnumsRoleEnum;
use AppHttpControllersController;
use SpatieSimpleExcelSimpleExcelWriter;
class ExportUserController extends Controller
{
public function export(RoleEnum $role)
{
$filename = strtolower($role->value) . '.csv';
$filetype = 'csv';
$writer = SimpleExcelWriter::streamDownload($filename, $filetype);
// First row is column labels.
$writer->addHeader([
'Company Name',
'Account Code',
'First Name',
'Last Name',
'Email',
'Registration',
'Email Verified',
'Last Login',
'Status',
'Role'
]);
User::where('role', '=', $role->value)
->orderby('first_name', 'asc')
->orderby('last_name', 'asc')
->chunk(
200, // Chunk size is arbitrary, but reasonable.
function($users) use ($writer) {
foreach($users as $user) {
$writer->addRow(
[
$user->company_name,
$user->account_code,
$user->first_name,
$user->last_name,
$user->email,
$user->local_created_at_date,
$user->local_email_verified_at_date,
$user->local_last_login_at_date,
$user->status->value,
$user->role->value,
]
);
}
flush(); // Flush the buffer every chunk.
}
);
$writer->toBrowser();
$writer->close();
}
}
</code>
<code><?php namespace AppHttpControllersAdmin; use AppModelsUser; use AppEnumsRoleEnum; use AppHttpControllersController; use SpatieSimpleExcelSimpleExcelWriter; class ExportUserController extends Controller { public function export(RoleEnum $role) { $filename = strtolower($role->value) . '.csv'; $filetype = 'csv'; $writer = SimpleExcelWriter::streamDownload($filename, $filetype); // First row is column labels. $writer->addHeader([ 'Company Name', 'Account Code', 'First Name', 'Last Name', 'Email', 'Registration', 'Email Verified', 'Last Login', 'Status', 'Role' ]); User::where('role', '=', $role->value) ->orderby('first_name', 'asc') ->orderby('last_name', 'asc') ->chunk( 200, // Chunk size is arbitrary, but reasonable. function($users) use ($writer) { foreach($users as $user) { $writer->addRow( [ $user->company_name, $user->account_code, $user->first_name, $user->last_name, $user->email, $user->local_created_at_date, $user->local_email_verified_at_date, $user->local_last_login_at_date, $user->status->value, $user->role->value, ] ); } flush(); // Flush the buffer every chunk. } ); $writer->toBrowser(); $writer->close(); } } </code>
<?php

namespace AppHttpControllersAdmin;

use AppModelsUser;
use AppEnumsRoleEnum;
use AppHttpControllersController;
use SpatieSimpleExcelSimpleExcelWriter;

class ExportUserController extends Controller
{
    public function export(RoleEnum $role)
    {
        $filename = strtolower($role->value) . '.csv';
        $filetype = 'csv';

        $writer = SimpleExcelWriter::streamDownload($filename, $filetype);

        // First row is column labels.
        $writer->addHeader([
            'Company Name',
            'Account Code',
            'First Name',
            'Last Name',
            'Email',
            'Registration',
            'Email Verified',
            'Last Login',
            'Status',
            'Role'
        ]);

        User::where('role', '=', $role->value)
            ->orderby('first_name', 'asc')
            ->orderby('last_name', 'asc')
            ->chunk(
                200, // Chunk size is arbitrary, but reasonable.
                function($users) use ($writer) {
                    foreach($users as $user) {
                        $writer->addRow(
                            [
                                $user->company_name,
                                $user->account_code,
                                $user->first_name,
                                $user->last_name,
                                $user->email,
                                $user->local_created_at_date,
                                $user->local_email_verified_at_date,
                                $user->local_last_login_at_date,
                                $user->status->value,
                                $user->role->value,
                            ]
                        );
                    }

                    flush(); // Flush the buffer every chunk.
                }
            );

        $writer->toBrowser();
        $writer->close();
    }
}

I could use some help with writing a feature test for this route. I’m not sure what to test or how exactly to write it up. When I run the below test method, the streamed content to the browser is output to the terminal, mixed in with the artisan test output. I’m not sure if I should be capturing the streamed output and inspecting the content, etc..

Any tips on writing this test method and preventing the output to the terminal?
Thanks!

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><?php
namespace TestsFeatureAdmin;
use AppModelsUser;
use AppEnumsRoleEnum;
use IlluminateFoundationTestingRefreshDatabase;
use TestsTestCase;
/**
* Test the 'Export Users' Feature.
*/
class UserExportTest extends TestCase
{
use RefreshDatabase;
public function test_export_users(): void
{
// Create the admin performing the activity.
$admin = User::factory()->admin()->create();
// Populate the database with users.
User::factory(4)->user()->create();
// What is the correct way to write a test for streamed content?
$response = $this
->actingAs($admin)
->get(route('admin.export.user', ['role' => RoleEnum::User->value]));
// ...?
}
}
</code>
<code><?php namespace TestsFeatureAdmin; use AppModelsUser; use AppEnumsRoleEnum; use IlluminateFoundationTestingRefreshDatabase; use TestsTestCase; /** * Test the 'Export Users' Feature. */ class UserExportTest extends TestCase { use RefreshDatabase; public function test_export_users(): void { // Create the admin performing the activity. $admin = User::factory()->admin()->create(); // Populate the database with users. User::factory(4)->user()->create(); // What is the correct way to write a test for streamed content? $response = $this ->actingAs($admin) ->get(route('admin.export.user', ['role' => RoleEnum::User->value])); // ...? } } </code>
<?php

namespace TestsFeatureAdmin;

use AppModelsUser;
use AppEnumsRoleEnum;
use IlluminateFoundationTestingRefreshDatabase;
use TestsTestCase;

/**
 * Test the 'Export Users' Feature.
 */
class UserExportTest extends TestCase
{
    use RefreshDatabase;

    public function test_export_users(): void
    {
        // Create the admin performing the activity.
        $admin = User::factory()->admin()->create();

        // Populate the database with users.
        User::factory(4)->user()->create();

        // What is the correct way to write a test for streamed content?
        $response = $this
            ->actingAs($admin)
            ->get(route('admin.export.user', ['role' => RoleEnum::User->value]));

        // ...?
    }
}

I’ve read through the Laravel.com documentation and Googled the heck out of this, to no avail. I’m not sure where to turn. I appreciate any help.

New contributor

dfaltermier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật