123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497 |
- <?php
- namespace Maatwebsite\Excel\Tests;
- use Illuminate\Contracts\View\View;
- use Illuminate\Http\UploadedFile;
- use Illuminate\Support\Collection;
- use Maatwebsite\Excel\Concerns\Exportable;
- use Maatwebsite\Excel\Concerns\FromCollection;
- use Maatwebsite\Excel\Concerns\FromView;
- use Maatwebsite\Excel\Concerns\Importable;
- use Maatwebsite\Excel\Concerns\RegistersEventListeners;
- use Maatwebsite\Excel\Concerns\ToArray;
- use Maatwebsite\Excel\Concerns\WithCustomCsvSettings;
- use Maatwebsite\Excel\Concerns\WithEvents;
- use Maatwebsite\Excel\Excel;
- use Maatwebsite\Excel\Facades\Excel as ExcelFacade;
- use Maatwebsite\Excel\Importer;
- use Maatwebsite\Excel\Tests\Data\Stubs\EmptyExport;
- use Maatwebsite\Excel\Tests\Helpers\FileHelper;
- use PHPUnit\Framework\Assert;
- use Symfony\Component\HttpFoundation\BinaryFileResponse;
- class ExcelTest extends TestCase
- {
- /**
- * @var Excel
- */
- protected $SUT;
- protected function setUp(): void
- {
- parent::setUp();
- $this->SUT = $this->app->make(Excel::class);
- }
- /**
- * @test
- */
- public function can_download_an_export_object_with_facade()
- {
- $export = new EmptyExport();
- $response = ExcelFacade::download($export, 'filename.xlsx');
- $this->assertInstanceOf(BinaryFileResponse::class, $response);
- $this->assertEquals('attachment; filename=filename.xlsx', str_replace('"', '', $response->headers->get('Content-Disposition')));
- }
- /**
- * @test
- */
- public function can_download_an_export_object()
- {
- $export = new EmptyExport();
- $response = $this->SUT->download($export, 'filename.xlsx');
- $this->assertInstanceOf(BinaryFileResponse::class, $response);
- $this->assertEquals('attachment; filename=filename.xlsx', str_replace('"', '', $response->headers->get('Content-Disposition')));
- }
- /**
- * @test
- */
- public function can_store_an_export_object_on_default_disk()
- {
- $export = new EmptyExport;
- $name = 'filename.xlsx';
- $path = FileHelper::absolutePath($name, 'local');
- @unlink($path);
- $this->assertFileMissing($path);
- $response = $this->SUT->store($export, $name);
- $this->assertTrue($response);
- $this->assertFileExists($path);
- }
- /**
- * @test
- */
- public function can_store_an_export_object_on_another_disk()
- {
- $export = new EmptyExport;
- $name = 'filename.xlsx';
- $path = FileHelper::absolutePath($name, 'test');
- @unlink($path);
- $this->assertFileMissing($path);
- $response = $this->SUT->store($export, $name, 'test');
- $this->assertTrue($response);
- $this->assertFileExists($path);
- }
- /**
- * @test
- */
- public function can_store_csv_export_with_default_settings()
- {
- $export = new EmptyExport;
- $name = 'filename.csv';
- $path = FileHelper::absolutePath($name, 'local');
- @unlink($path);
- $this->assertFileMissing($path);
- $response = $this->SUT->store($export, $name);
- $this->assertTrue($response);
- $this->assertFileExists($path);
- }
- /**
- * @test
- */
- public function can_get_raw_export_contents()
- {
- $export = new EmptyExport;
- $response = $this->SUT->raw($export, Excel::XLSX);
- $this->assertNotEmpty($response);
- }
- /**
- * @test
- */
- public function can_store_tsv_export_with_default_settings()
- {
- $export = new EmptyExport;
- $name = 'filename.tsv';
- $path = FileHelper::absolutePath($name, 'local');
- @unlink($path);
- $this->assertFileMissing($path);
- $response = $this->SUT->store($export, $name);
- $this->assertTrue($response);
- $this->assertFileExists($path);
- }
- /**
- * @test
- */
- public function can_store_csv_export_with_custom_settings()
- {
- $export = new class implements WithEvents, FromCollection, WithCustomCsvSettings
- {
- use RegistersEventListeners;
- /**
- * @return Collection
- */
- public function collection()
- {
- return collect([
- ['A1', 'B1'],
- ['A2', 'B2'],
- ]);
- }
- /**
- * @return array
- */
- public function getCsvSettings(): array
- {
- return [
- 'line_ending' => PHP_EOL,
- 'enclosure' => '"',
- 'delimiter' => ';',
- 'include_separator_line' => true,
- 'excel_compatibility' => false,
- ];
- }
- };
- $this->SUT->store($export, 'filename.csv');
- $contents = file_get_contents(__DIR__ . '/Data/Disks/Local/filename.csv');
- $this->assertStringContains('sep=;', $contents);
- $this->assertStringContains('"A1";"B1"', $contents);
- $this->assertStringContains('"A2";"B2"', $contents);
- }
- /**
- * @test
- */
- public function cannot_use_from_collection_and_from_view_on_same_export()
- {
- $this->expectException(\Maatwebsite\Excel\Exceptions\ConcernConflictException::class);
- $this->expectExceptionMessage('Cannot use FromQuery, FromArray or FromCollection and FromView on the same sheet');
- $export = new class implements FromCollection, FromView
- {
- use Exportable;
- /**
- * @return Collection
- */
- public function collection()
- {
- return collect();
- }
- /**
- * @return View
- */
- public function view(): View
- {
- return view('users');
- }
- };
- $export->download('filename.csv');
- }
- /**
- * @test
- */
- public function can_import_a_simple_xlsx_file_to_array()
- {
- $import = new class
- {
- use Importable;
- };
- $this->assertEquals([
- [
- ['test', 'test'],
- ['test', 'test'],
- ],
- ], $import->toArray('import.xlsx'));
- }
- /**
- * @test
- */
- public function can_import_a_simple_xlsx_file_to_collection()
- {
- $import = new class
- {
- use Importable;
- };
- $this->assertEquals(new Collection([
- new Collection([
- new Collection(['test', 'test']),
- new Collection(['test', 'test']),
- ]),
- ]), $import->toCollection('import.xlsx'));
- }
- /**
- * @test
- */
- public function can_import_a_simple_xlsx_file_to_collection_without_import_object()
- {
- $this->assertEquals(new Collection([
- new Collection([
- new Collection(['test', 'test']),
- new Collection(['test', 'test']),
- ]),
- ]), ExcelFacade::toCollection(null, 'import.xlsx'));
- }
- /**
- * @test
- */
- public function can_import_a_simple_xlsx_file()
- {
- $import = new class implements ToArray
- {
- /**
- * @param array $array
- */
- public function array(array $array)
- {
- Assert::assertEquals([
- ['test', 'test'],
- ['test', 'test'],
- ], $array);
- }
- };
- $imported = $this->SUT->import($import, 'import.xlsx');
- $this->assertInstanceOf(Importer::class, $imported);
- }
- /**
- * @test
- */
- public function can_import_a_tsv_file()
- {
- $import = new class implements ToArray, WithCustomCsvSettings
- {
- /**
- * @param array $array
- */
- public function array(array $array)
- {
- Assert::assertEquals([
- 'tconst',
- 'titleType',
- 'primaryTitle',
- 'originalTitle',
- 'isAdult',
- 'startYear',
- 'endYear',
- 'runtimeMinutes',
- 'genres',
- ], $array[0]);
- }
- /**
- * @return array
- */
- public function getCsvSettings(): array
- {
- return [
- 'delimiter' => "\t",
- ];
- }
- };
- $imported = $this->SUT->import($import, 'import-titles.tsv');
- $this->assertInstanceOf(Importer::class, $imported);
- }
- /**
- * @test
- */
- public function can_chain_imports()
- {
- $import1 = new class implements ToArray
- {
- /**
- * @param array $array
- */
- public function array(array $array)
- {
- Assert::assertEquals([
- ['test', 'test'],
- ['test', 'test'],
- ], $array);
- }
- };
- $import2 = new class implements ToArray
- {
- /**
- * @param array $array
- */
- public function array(array $array)
- {
- Assert::assertEquals([
- ['test', 'test'],
- ['test', 'test'],
- ], $array);
- }
- };
- $imported = $this->SUT
- ->import($import1, 'import.xlsx')
- ->import($import2, 'import.xlsx');
- $this->assertInstanceOf(Importer::class, $imported);
- }
- /**
- * @test
- */
- public function can_import_a_simple_xlsx_file_from_uploaded_file()
- {
- $import = new class implements ToArray
- {
- /**
- * @param array $array
- */
- public function array(array $array)
- {
- Assert::assertEquals([
- ['test', 'test'],
- ['test', 'test'],
- ], $array);
- }
- };
- $this->SUT->import($import, $this->givenUploadedFile(__DIR__ . '/Data/Disks/Local/import.xlsx'));
- }
- /**
- * @test
- */
- public function can_import_a_simple_xlsx_file_from_real_path()
- {
- $import = new class implements ToArray
- {
- /**
- * @param array $array
- */
- public function array(array $array)
- {
- Assert::assertEquals([
- ['test', 'test'],
- ['test', 'test'],
- ], $array);
- }
- };
- $this->SUT->import($import, __DIR__ . '/Data/Disks/Local/import.xlsx');
- }
- /**
- * @test
- */
- public function import_will_throw_error_when_no_reader_type_could_be_detected_when_no_extension()
- {
- $this->expectException(\Maatwebsite\Excel\Exceptions\NoTypeDetectedException::class);
- $import = new class implements ToArray
- {
- /**
- * @param array $array
- */
- public function array(array $array)
- {
- Assert::assertEquals([
- ['test', 'test'],
- ['test', 'test'],
- ], $array);
- }
- };
- $this->SUT->import($import, UploadedFile::fake()->create('import'));
- }
- /**
- * @test
- */
- public function import_will_throw_error_when_no_reader_type_could_be_detected_with_unknown_extension()
- {
- $this->expectException(\Maatwebsite\Excel\Exceptions\NoTypeDetectedException::class);
- $import = new class implements ToArray
- {
- /**
- * @param array $array
- */
- public function array(array $array)
- {
- //
- }
- };
- $this->SUT->import($import, 'unknown-reader-type.zip');
- }
- /**
- * @test
- */
- public function can_import_without_extension_with_explicit_reader_type()
- {
- $import = new class implements ToArray
- {
- /**
- * @param array $array
- */
- public function array(array $array)
- {
- Assert::assertEquals([
- ['test', 'test'],
- ['test', 'test'],
- ], $array);
- }
- };
- $this->SUT->import(
- $import,
- $this->givenUploadedFile(__DIR__ . '/Data/Disks/Local/import.xlsx', 'import'),
- null,
- Excel::XLSX
- );
- }
- }
|