From 4a62a0855690253633745c9d6a10670c2e8cb6b0 Mon Sep 17 00:00:00 2001 From: Sven Rymenants Date: Thu, 16 Feb 2023 16:07:12 +0100 Subject: [PATCH] Format dates if importing with WithChunkReading (#3872) * Add test for chunckreading of dates * Format dates when importing with WithChunkReading * Update test of chunck reader to test against real dates --- src/Jobs/ReadChunk.php | 4 +- tests/Concerns/WithChunkReadingTest.php | 79 +++++++++++++++++- tests/Data/Disks/Local/.gitignore | 3 +- .../Disks/Local/import-batches-with-date.xlsx | Bin 0 -> 4880 bytes 4 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 tests/Data/Disks/Local/import-batches-with-date.xlsx diff --git a/src/Jobs/ReadChunk.php b/src/Jobs/ReadChunk.php index 960a748d7..9bfeee5fb 100644 --- a/src/Jobs/ReadChunk.php +++ b/src/Jobs/ReadChunk.php @@ -143,8 +143,8 @@ public function handle(TransactionHandler $transaction) ); $this->reader->setReadFilter($filter); - $this->reader->setReadDataOnly(true); - $this->reader->setReadEmptyCells(false); + $this->reader->setReadDataOnly(config('excel.imports.read_only', true)); + $this->reader->setReadEmptyCells(!config('excel.imports.ignore_empty', false)); $spreadsheet = $this->reader->load( $this->temporaryFile->sync()->getLocalPath() diff --git a/tests/Concerns/WithChunkReadingTest.php b/tests/Concerns/WithChunkReadingTest.php index 0c9109314..7b60e34a1 100644 --- a/tests/Concerns/WithChunkReadingTest.php +++ b/tests/Concerns/WithChunkReadingTest.php @@ -2,6 +2,7 @@ namespace Maatwebsite\Excel\Tests\Concerns; +use DateTime; use Exception; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\DB; @@ -11,6 +12,7 @@ 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; @@ -20,6 +22,7 @@ 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; @@ -283,7 +286,7 @@ public function batchSize(): int */ public function can_import_to_array_in_chunks() { - $import = new class implements ToArray, WithChunkReading + $import = new class implements ToArray, WithChunkReading, WithFormatData { use Importable; @@ -522,4 +525,78 @@ public function registerEvents(): array $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_and_keep_format_in_chunks() + { + config()->set('excel.imports.read_only', true); + + $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::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'); + } } diff --git a/tests/Data/Disks/Local/.gitignore b/tests/Data/Disks/Local/.gitignore index 051902f60..76375e51f 100644 --- a/tests/Data/Disks/Local/.gitignore +++ b/tests/Data/Disks/Local/.gitignore @@ -24,4 +24,5 @@ !import-external-reference.xls !csv-with-comma.csv !import-users-with-grouped-headers.xlsx -!import-users-with-mixed-headings.xlsx \ No newline at end of file +!import-users-with-mixed-headings.xlsx +!import-batches-with-date.xlsx \ No newline at end of file diff --git a/tests/Data/Disks/Local/import-batches-with-date.xlsx b/tests/Data/Disks/Local/import-batches-with-date.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..f6ed2ee3c4567e5516201eef92bf3f0812eda5c4 GIT binary patch literal 4880 zcmaJ_bzIZ!7H7Z+X&4|ak_#5W&_%C=JLdswDpLhj|NDrbzyJ=*!jNt10-YF2>y@3VQHzh%c zRNV`oclGt&0X{ik9q|-ZV!dzO@E1REj9ZB|$0pHwIhmp-!w6fW_1)d1JapJu?kY(v z-sH^S@axqD7YCQiw9-<*VlD(?U?ENYbDNozOyr{4`ZPHlD;vEGqm3wz&0)BGXr|O+s$4|r#vOtABm-R zmDdye;7t4WF8o@4hO1X9h&fRX?7BqsB?>{&XjI@B6`ad0u+hep3b|KRLHHc48%`WuAv+fO#`^OzB(esLf`h;bLPc4f{cxQR-|o24%A z%T2$|;FbXD-Ofh;#L&P>`KHyWq-DwGT5|&}F!^p@SUb&%ZVYtUbtpiagrlclV`m)X z?uiX^-UQ}Ys169*P7SWk?vxBFjo#gS7X))K<&Y+uYYYTRHLA~sqqQID52k^i?YcXJ zGdAFNSbQ_21|R2FZqZ1*W}ak0;o<>bn2K`=m|uNp ze4BgTF|5aOlmp|!WnHHx+}QNasGEFn!6G>h-TRY%sv+v`a_)M;C#RwdAqh;?pLbS5 zy(gRuZ8gdAQMZ>c{>1h-?FMcyDA=>@V2fBy26zFA0?69W2{Isq0+xu?dIqWG_AkBb zRmzGViB_#+gl@=fl2u|6ks!GX0^&MJgK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