Laravel-excel: Can't test CSV download

Created on 7 Aug 2015  路  7Comments  路  Source: Maatwebsite/Laravel-Excel

We have got controller method with following code:

....
return Excel::create('List', function($excel) use ($list)
{
  $excel->sheet('List', function($sheet) use ($list)
  {
    $sheet->fromModel($list);
  });
})
->download('csv');  

and simple test like this:

$this->call('GET', 'route/to/csv', [
    'param' => 'value',
]);

$this->dump();

Above test outputs [ERROR]: Headers already sent from this line.

Controller method works fine, but we are not able to test it.

We also tried to run phpunit with --stderr param. In that case, no error is thrown, but it just dumps output of CSV file and exits.
Also tried to run test with @runInSeparateProcess annotation and got errors like:

PHPUnit_Framework_Exception: PHP Notice:  Constant LARAVEL_START already defined in bootstrap/autoload.php on line 3
....
PHP Fatal error:  Uncaught exception 'ReflectionException' with message 'Class env does not exist' in vendor/laravel/framework/src/Illuminate/Container/Container.php:736

Is it bug or we are testing it wrong?
Laravel version 5.1.8, Laravel Excel version 2.0.6

Most helpful comment

I'm having the same issue, this is how I implemented @leondeng's fix for CSV files.

$excel = Excel::create(...);

$identifier = app('excel.identifier');
$format = $identifier->getFormatByExtension('csv');
$contentType = $identifier->getContentTypeByFormat($format);

return response($excel->string('csv'), 200, [
    'Content-Type'        => $contentType,
    'Content-Disposition' => 'attachment; filename="' . $excel->getFileName() . '.csv"',
    'Expires'             => 'Mon, 26 Jul 1997 05:00:00 GMT', // Date in the past
    'Last-Modified'       => Carbon::now()->format('D, d M Y H:i:s'),
    'Cache-Control'       => 'cache, must-revalidate',
    'Pragma'              => 'public',
]);

All 7 comments

try catch it?

try {
  Excel::create..
}
catch(Exception $e) {
  if (getenv('APP_ENV') !== 'testing') {
    throw $e;
  }
}

Though of course that's just a trick to prevent the error, not a real solution..

Never tried to test the excel download with phpunit. Am afraid I can't really help you with that.

Seems that Phpunit already sends headers, which makes the excel download not able to run.
Storing a file would be easier to test: you could open the file afterwards and check if the results are what you would expect. (This is how I test the package)

Thanks!

To those still getting this issue:
There's a walk around: call string() instead of export() then wrap up result in your response.

Also used string and then checked the content of the string in my tests. Not beautiful but worked.

I'm having the same issue, this is how I implemented @leondeng's fix for CSV files.

$excel = Excel::create(...);

$identifier = app('excel.identifier');
$format = $identifier->getFormatByExtension('csv');
$contentType = $identifier->getContentTypeByFormat($format);

return response($excel->string('csv'), 200, [
    'Content-Type'        => $contentType,
    'Content-Disposition' => 'attachment; filename="' . $excel->getFileName() . '.csv"',
    'Expires'             => 'Mon, 26 Jul 1997 05:00:00 GMT', // Date in the past
    'Last-Modified'       => Carbon::now()->format('D, d M Y H:i:s'),
    'Cache-Control'       => 'cache, must-revalidate',
    'Pragma'              => 'public',
]);
Was this page helpful?
0 / 5 - 0 ratings