123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- <?php
- namespace Maatwebsite\Excel\Tests\Concerns;
- use Illuminate\Contracts\Support\Responsable;
- use Illuminate\Http\Request;
- use Maatwebsite\Excel\Concerns\Exportable;
- use Maatwebsite\Excel\Excel;
- use Maatwebsite\Excel\Exporter;
- use Maatwebsite\Excel\Tests\Data\Stubs\EmptyExport;
- use Maatwebsite\Excel\Tests\TestCase;
- use Symfony\Component\HttpFoundation\BinaryFileResponse;
- class ExportableTest extends TestCase
- {
- /**
- * @test
- */
- public function needs_to_have_a_file_name_when_downloading()
- {
- $this->expectException(\Maatwebsite\Excel\Exceptions\NoFilenameGivenException::class);
- $this->expectExceptionMessage('A filename needs to be passed in order to download the export');
- $export = new class
- {
- use Exportable;
- };
- $export->download();
- }
- /**
- * @test
- */
- public function needs_to_have_a_file_name_when_storing()
- {
- $this->expectException(\Maatwebsite\Excel\Exceptions\NoFilePathGivenException::class);
- $this->expectExceptionMessage('A filepath needs to be passed in order to store the export');
- $export = new class
- {
- use Exportable;
- };
- $export->store();
- }
- /**
- * @test
- */
- public function needs_to_have_a_file_name_when_queuing()
- {
- $this->expectException(\Maatwebsite\Excel\Exceptions\NoFilePathGivenException::class);
- $this->expectExceptionMessage('A filepath needs to be passed in order to store the export');
- $export = new class
- {
- use Exportable;
- };
- $export->queue();
- }
- /**
- * @test
- */
- public function responsable_needs_to_have_file_name_configured_inside_the_export()
- {
- $this->expectException(\Maatwebsite\Excel\Exceptions\NoFilenameGivenException::class);
- $this->expectExceptionMessage('A filename needs to be passed in order to download the export');
- $export = new class implements Responsable
- {
- use Exportable;
- };
- $export->toResponse(new Request());
- }
- /**
- * @test
- */
- public function is_responsable()
- {
- $export = new class implements Responsable
- {
- use Exportable;
- protected $fileName = 'export.xlsx';
- };
- $this->assertInstanceOf(Responsable::class, $export);
- $response = $export->toResponse(new Request());
- $this->assertInstanceOf(BinaryFileResponse::class, $response);
- }
- /**
- * @test
- */
- public function can_have_customized_header()
- {
- $export = new class
- {
- use Exportable;
- };
- $response = $export->download(
- 'name.csv',
- Excel::CSV,
- [
- 'Content-Type' => 'text/csv',
- ]
- );
- $this->assertEquals('text/csv', $response->headers->get('Content-Type'));
- }
- /**
- * @test
- */
- public function can_set_custom_headers_in_export_class()
- {
- $export = new class
- {
- use Exportable;
- protected $fileName = 'name.csv';
- protected $writerType = Excel::CSV;
- protected $headers = [
- 'Content-Type' => 'text/csv',
- ];
- };
- $response = $export->toResponse(request());
- $this->assertEquals('text/csv', $response->headers->get('Content-Type'));
- }
- /**
- * @test
- */
- public function can_get_raw_export_contents()
- {
- $export = new EmptyExport;
- $response = $export->raw(Excel::XLSX);
- $this->assertNotEmpty($response);
- }
- /**
- * @test
- */
- public function can_have_customized_disk_options_when_storing()
- {
- $export = new EmptyExport;
- $this->mock(Exporter::class)
- ->shouldReceive('store')->once()
- ->with($export, 'name.csv', 's3', Excel::CSV, ['visibility' => 'private']);
- $export->store('name.csv', 's3', Excel::CSV, ['visibility' => 'private']);
- }
- /**
- * @test
- */
- public function can_have_customized_disk_options_when_queueing()
- {
- $export = new EmptyExport;
- $this->mock(Exporter::class)
- ->shouldReceive('queue')->once()
- ->with($export, 'name.csv', 's3', Excel::CSV, ['visibility' => 'private']);
- $export->queue('name.csv', 's3', Excel::CSV, ['visibility' => 'private']);
- }
- /**
- * @test
- */
- public function can_set_disk_options_in_export_class_when_storing()
- {
- $export = new class
- {
- use Exportable;
- public $disk = 's3';
- public $writerType = Excel::CSV;
- public $diskOptions = ['visibility' => 'private'];
- };
- $this->mock(Exporter::class)
- ->shouldReceive('store')->once()
- ->with($export, 'name.csv', 's3', Excel::CSV, ['visibility' => 'private']);
- $export->store('name.csv');
- }
- /**
- * @test
- */
- public function can_set_disk_options_in_export_class_when_queuing()
- {
- $export = new class
- {
- use Exportable;
- public $disk = 's3';
- public $writerType = Excel::CSV;
- public $diskOptions = ['visibility' => 'private'];
- };
- $this->mock(Exporter::class)
- ->shouldReceive('queue')->once()
- ->with($export, 'name.csv', 's3', Excel::CSV, ['visibility' => 'private']);
- $export->queue('name.csv');
- }
- /**
- * @test
- */
- public function can_override_export_class_disk_options_when_calling_store()
- {
- $export = new class
- {
- use Exportable;
- public $diskOptions = ['visibility' => 'public'];
- };
- $this->mock(Exporter::class)
- ->shouldReceive('store')->once()
- ->with($export, 'name.csv', 's3', Excel::CSV, ['visibility' => 'private']);
- $export->store('name.csv', 's3', Excel::CSV, ['visibility' => 'private']);
- }
- /**
- * @test
- */
- public function can_override_export_class_disk_options_when_calling_queue()
- {
- $export = new class
- {
- use Exportable;
- public $diskOptions = ['visibility' => 'public'];
- };
- $this->mock(Exporter::class)
- ->shouldReceive('queue')->once()
- ->with($export, 'name.csv', 's3', Excel::CSV, ['visibility' => 'private']);
- $export->queue('name.csv', 's3', Excel::CSV, ['visibility' => 'private']);
- }
- /**
- * @test
- */
- public function can_have_empty_disk_options_when_storing()
- {
- $export = new EmptyExport;
- $this->mock(Exporter::class)
- ->shouldReceive('store')->once()
- ->with($export, 'name.csv', null, null, []);
- $export->store('name.csv');
- }
- /**
- * @test
- */
- public function can_have_empty_disk_options_when_queueing()
- {
- $export = new EmptyExport;
- $this->mock(Exporter::class)
- ->shouldReceive('queue')->once()
- ->with($export, 'name.csv', null, null, []);
- $export->queue('name.csv');
- }
- }
|