Skip to content

Commit

Permalink
Format dates if importing with WithChunkReading (#3872)
Browse files Browse the repository at this point in the history
* Add test for chunckreading of dates

* Format dates when importing with WithChunkReading

* Update test of chunck reader to test against real dates
  • Loading branch information
svenbw authored Feb 16, 2023
1 parent b79b0d4 commit 4a62a08
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Jobs/ReadChunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
79 changes: 78 additions & 1 deletion tests/Concerns/WithChunkReadingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Maatwebsite\Excel\Tests\Concerns;

use DateTime;
use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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');
}
}
3 changes: 2 additions & 1 deletion tests/Data/Disks/Local/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
!import-external-reference.xls
!csv-with-comma.csv
!import-users-with-grouped-headers.xlsx
!import-users-with-mixed-headings.xlsx
!import-users-with-mixed-headings.xlsx
!import-batches-with-date.xlsx
Binary file not shown.

0 comments on commit 4a62a08

Please sign in to comment.