123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602 |
- <?php
- namespace Maatwebsite\Excel\Tests\Concerns;
- use DateTime;
- use Exception;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Facades\DB;
- use Maatwebsite\Excel\Concerns\Importable;
- use Maatwebsite\Excel\Concerns\ToArray;
- use Maatwebsite\Excel\Concerns\ToModel;
- use Maatwebsite\Excel\Concerns\WithBatchInserts;
- use Maatwebsite\Excel\Concerns\WithChunkReading;
- use Maatwebsite\Excel\Concerns\WithEvents;
- use Maatwebsite\Excel\Concerns\WithFormatData;
- use Maatwebsite\Excel\Concerns\WithHeadingRow;
- use Maatwebsite\Excel\Concerns\WithMultipleSheets;
- use Maatwebsite\Excel\Events\AfterImport;
- use Maatwebsite\Excel\Events\BeforeImport;
- use Maatwebsite\Excel\Events\ImportFailed;
- use Maatwebsite\Excel\Reader;
- use Maatwebsite\Excel\Tests\Data\Stubs\Database\Group;
- use Maatwebsite\Excel\Tests\Data\Stubs\Database\User;
- use Maatwebsite\Excel\Tests\TestCase;
- use PhpOffice\PhpSpreadsheet\Shared\Date;
- use PHPUnit\Framework\Assert;
- use Throwable;
- class WithChunkReadingTest extends TestCase
- {
- /**
- * Setup the test environment.
- */
- protected function setUp(): void
- {
- parent::setUp();
- $this->loadLaravelMigrations(['--database' => 'testing']);
- $this->loadMigrationsFrom(dirname(__DIR__) . '/Data/Stubs/Database/Migrations');
- }
- /**
- * @test
- */
- public function can_import_to_model_in_chunks_un()
- {
- DB::connection()->enableQueryLog();
- $import = new class implements ToModel, WithChunkReading, WithEvents
- {
- use Importable;
- public $before = 0;
- public $after = 0;
- /**
- * @param array $row
- * @return Model|null
- */
- public function model(array $row)
- {
- return new User([
- 'name' => $row[0],
- 'email' => $row[1],
- 'password' => 'secret',
- ]);
- }
- /**
- * @return int
- */
- public function chunkSize(): int
- {
- return 1;
- }
- /**
- * @return array
- */
- public function registerEvents(): array
- {
- return [
- BeforeImport::class => function (BeforeImport $event) {
- Assert::assertInstanceOf(Reader::class, $event->reader);
- $this->before++;
- },
- AfterImport::class => function (AfterImport $event) {
- Assert::assertInstanceOf(Reader::class, $event->reader);
- $this->after++;
- },
- ];
- }
- };
- $import->import('import-users.xlsx');
- $this->assertCount(2, DB::getQueryLog());
- DB::connection()->disableQueryLog();
- $this->assertEquals(1, $import->before, 'BeforeImport was not called or more than once.');
- $this->assertEquals(1, $import->after, 'AfterImport was not called or more than once.');
- }
- /**
- * @test
- */
- public function can_import_to_model_in_chunks_and_insert_in_batches()
- {
- DB::connection()->enableQueryLog();
- $import = new class implements ToModel, WithChunkReading, WithBatchInserts
- {
- use Importable;
- /**
- * @param array $row
- * @return Model|null
- */
- public function model(array $row)
- {
- return new Group([
- 'name' => $row[0],
- ]);
- }
- /**
- * @return int
- */
- public function chunkSize(): int
- {
- return 1000;
- }
- /**
- * @return int
- */
- public function batchSize(): int
- {
- return 1000;
- }
- };
- $import->import('import-batches.xlsx');
- $this->assertCount(5000 / $import->batchSize(), DB::getQueryLog());
- DB::connection()->disableQueryLog();
- }
- /**
- * @test
- */
- public function can_import_to_model_in_chunks_and_insert_in_batches_with_heading_row()
- {
- DB::connection()->enableQueryLog();
- $import = new class implements ToModel, WithChunkReading, WithBatchInserts, WithHeadingRow
- {
- use Importable;
- /**
- * @param array $row
- * @return Model|null
- */
- public function model(array $row)
- {
- return new Group([
- 'name' => $row['name'],
- ]);
- }
- /**
- * @return int
- */
- public function chunkSize(): int
- {
- return 1000;
- }
- /**
- * @return int
- */
- public function batchSize(): int
- {
- return 1000;
- }
- };
- $import->import('import-batches-with-heading-row.xlsx');
- $this->assertCount(5000 / $import->batchSize(), DB::getQueryLog());
- DB::connection()->disableQueryLog();
- }
- /**
- * @test
- */
- public function can_import_csv_in_chunks_and_insert_in_batches()
- {
- DB::connection()->enableQueryLog();
- $import = new class implements ToModel, WithChunkReading, WithBatchInserts
- {
- use Importable;
- /**
- * @param array $row
- * @return Model|null
- */
- public function model(array $row)
- {
- return new Group([
- 'name' => $row[0],
- ]);
- }
- /**
- * @return int
- */
- public function chunkSize(): int
- {
- return 1000;
- }
- /**
- * @return int
- */
- public function batchSize(): int
- {
- return 1000;
- }
- };
- $import->import('import-batches.csv');
- $this->assertCount(10, DB::getQueryLog());
- DB::connection()->disableQueryLog();
- }
- /**
- * @test
- */
- public function can_import_to_model_in_chunks_and_insert_in_batches_with_multiple_sheets()
- {
- DB::connection()->enableQueryLog();
- $import = new class implements ToModel, WithChunkReading, WithBatchInserts
- {
- use Importable;
- /**
- * @param array $row
- * @return Model|null
- */
- public function model(array $row)
- {
- return new Group([
- 'name' => $row[0],
- ]);
- }
- /**
- * @return int
- */
- public function chunkSize(): int
- {
- return 1000;
- }
- /**
- * @return int
- */
- public function batchSize(): int
- {
- return 1000;
- }
- };
- $import->import('import-batches-multiple-sheets.xlsx');
- $this->assertCount(10, DB::getQueryLog());
- DB::connection()->disableQueryLog();
- }
- /**
- * @test
- */
- public function can_import_to_array_in_chunks()
- {
- $import = new class implements ToArray, WithChunkReading, WithFormatData
- {
- use Importable;
- public $called = 0;
- /**
- * @param array $array
- */
- public function array(array $array)
- {
- $this->called++;
- Assert::assertCount(100, $array);
- }
- /**
- * @return int
- */
- public function chunkSize(): int
- {
- return 100;
- }
- };
- $import->import('import-batches.xlsx');
- $this->assertEquals(50, $import->called);
- }
- /**
- * @test
- */
- public function can_import_to_model_in_chunks_and_insert_in_batches_with_multiple_sheets_objects_by_index()
- {
- DB::connection()->enableQueryLog();
- $import = new class implements WithMultipleSheets, WithChunkReading
- {
- use Importable;
- /**
- * @return int
- */
- public function chunkSize(): int
- {
- return 1000;
- }
- /**
- * @return array
- */
- public function sheets(): array
- {
- return [
- new class implements ToModel, WithBatchInserts
- {
- /**
- * @param array $row
- * @return Model|null
- */
- public function model(array $row)
- {
- return new Group([
- 'name' => $row[0],
- ]);
- }
- /**
- * @return int
- */
- public function batchSize(): int
- {
- return 1000;
- }
- },
- new class implements ToModel, WithBatchInserts
- {
- /**
- * @param array $row
- * @return Model|null
- */
- public function model(array $row)
- {
- return new Group([
- 'name' => $row[0],
- ]);
- }
- /**
- * @return int
- */
- public function batchSize(): int
- {
- return 2000;
- }
- },
- ];
- }
- };
- $import->import('import-batches-multiple-sheets.xlsx');
- $this->assertCount(10, DB::getQueryLog());
- DB::connection()->disableQueryLog();
- }
- /**
- * @test
- */
- public function can_import_to_model_in_chunks_and_insert_in_batches_with_multiple_sheets_objects_by_name()
- {
- DB::connection()->enableQueryLog();
- $import = new class implements WithMultipleSheets, WithChunkReading
- {
- use Importable;
- /**
- * @return int
- */
- public function chunkSize(): int
- {
- return 1000;
- }
- /**
- * @return array
- */
- public function sheets(): array
- {
- return [
- 'Worksheet' => new class implements ToModel, WithBatchInserts
- {
- /**
- * @param array $row
- * @return Model|null
- */
- public function model(array $row)
- {
- return new Group([
- 'name' => $row[0],
- ]);
- }
- /**
- * @return int
- */
- public function batchSize(): int
- {
- return 1000;
- }
- },
- 'Worksheet2' => new class implements ToModel, WithBatchInserts
- {
- /**
- * @param array $row
- * @return Model|null
- */
- public function model(array $row)
- {
- return new Group([
- 'name' => $row[0],
- ]);
- }
- /**
- * @return int
- */
- public function batchSize(): int
- {
- return 2000;
- }
- },
- ];
- }
- };
- $import->import('import-batches-multiple-sheets.xlsx');
- $this->assertCount(10, DB::getQueryLog());
- DB::connection()->disableQueryLog();
- }
- /**
- * @test
- */
- public function can_catch_job_failed_in_chunks()
- {
- $import = new class implements ToModel, WithChunkReading, WithEvents
- {
- use Importable;
- public $failed = false;
- /**
- * @param array $row
- * @return Model|null
- */
- public function model(array $row)
- {
- throw new Exception('Something went wrong in the chunk');
- }
- /**
- * @return int
- */
- public function chunkSize(): int
- {
- return 1;
- }
- /**
- * @return array
- */
- public function registerEvents(): array
- {
- return [
- ImportFailed::class => function (ImportFailed $event) {
- Assert::assertInstanceOf(Throwable::class, $event->getException());
- Assert::assertEquals('Something went wrong in the chunk', $event->e->getMessage());
- $this->failed = true;
- },
- ];
- }
- };
- try {
- $import->import('import-users.xlsx');
- } catch (Throwable $e) {
- $this->assertInstanceOf(Exception::class, $e);
- $this->assertEquals('Something went wrong in the chunk', $e->getMessage());
- }
- $this->assertTrue($import->failed, 'ImportFailed event was not called.');
- }
- /**
- * @test
- */
- public function can_import_to_array_and_format_in_chunks()
- {
- config()->set('excel.imports.read_only', false);
- $import = new class implements ToArray, WithChunkReading, WithFormatData
- {
- use Importable;
- /**
- * @param array $array
- */
- public function array(array $array)
- {
- Assert::assertCount(2, $array);
- Assert::assertCount(1, $array[0]);
- Assert::assertCount(1, $array[1]);
- Assert::assertIsString($array[0][0]);
- Assert::assertIsString($array[1][0]);
- Assert::assertEquals('01/12/22', $array[0][0]);
- Assert::assertEquals('2023-02-20', $array[1][0]);
- }
- /**
- * @return int
- */
- public function chunkSize(): int
- {
- return 2;
- }
- };
- $import->import('import-batches-with-date.xlsx');
- }
- /**
- * @test
- */
- public function can_import_to_array_in_chunks_without_formatting()
- {
- config()->set('excel.imports.read_only', true);
- $import = new class implements ToArray, WithChunkReading
- {
- use Importable;
- /**
- * @param array $array
- */
- public function array(array $array)
- {
- Assert::assertCount(2, $array);
- Assert::assertCount(1, $array[0]);
- Assert::assertCount(1, $array[1]);
- Assert::assertIsInt($array[0][0]);
- Assert::assertIsInt($array[1][0]);
- Assert::assertEquals((int) Date::dateTimeToExcel(DateTime::createFromFormat('Y-m-d', '2022-12-01')->setTime(0, 0, 0, 0)), $array[0][0]);
- Assert::assertEquals((int) Date::dateTimeToExcel(DateTime::createFromFormat('Y-m-d', '2023-02-20')->setTime(0, 0, 0, 0)), $array[1][0]);
- }
- /**
- * @return int
- */
- public function chunkSize(): int
- {
- return 2;
- }
- };
- $import->import('import-batches-with-date.xlsx');
- }
- }
|