Skip to content

Commit

Permalink
feat(ChunkWriteService): support iterable directly
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna authored and pionl committed Oct 17, 2024
1 parent 3f4f982 commit 9e21547
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/Database/Contracts/ChunkWriteServiceContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
namespace LaraStrict\Database\Contracts;

use Closure;
use Generator;
use Illuminate\Database\Eloquent\Model;
use LaraStrict\Database\Entities\ChunkWriteStateEntity;

/**
* @phpstan-type TData iterable<int|string, Model>
*/
interface ChunkWriteServiceContract
{
/**
* @param Closure(): Generator<int, Model> $closure
* @param Closure(): TData|TData $closure
*/
public function write(Closure $closure): ChunkWriteStateEntity;
public function write(Closure|iterable $closure): ChunkWriteStateEntity;
}
5 changes: 3 additions & 2 deletions src/Database/Services/ChunkWriteService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@

final class ChunkWriteService implements ChunkWriteServiceContract
{
public function write(Closure $closure, int $batchSize = 0): ChunkWriteStateEntity
public function write(Closure|iterable $closure, int $batchSize = 0): ChunkWriteStateEntity
{
$writeState = new ChunkWriteStateEntity(batchSize: $batchSize);

foreach ($closure() as $model) {
$source = $closure instanceof Closure ? $closure() : $closure;
foreach ($source as $model) {
$this->add($model, $writeState);
}

Expand Down
17 changes: 16 additions & 1 deletion tests/Unit/Database/Services/ChunkWriteServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
namespace Tests\LaraStrict\Unit\Database\Services;

use Closure;
use LaraStrict\Database\Contracts\ChunkWriteServiceContract;
use LaraStrict\Database\Entities\ChunkWriteStateEntity;
use LaraStrict\Database\Services\ChunkWriteService;
use LaraStrict\Tests\Traits\SqlTestEnable;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;

/**
* @phpstan-import-type TData from ChunkWriteServiceContract
*/
final class ChunkWriteServiceTest extends TestCase
{
use SqlTestEnable;
Expand Down Expand Up @@ -38,6 +42,14 @@ static function () {
);
},
],
[
static function (self $self) {
$self->assert(
new ChunkWriteStateEntity(32768, TestModel::class, 3, 2),
[new TestModel(), new TestModel(), new TestModel()],
);
},
],
];
}

Expand All @@ -50,7 +62,10 @@ public function test(Closure $assert): void
$assert($this);
}

public function assert(ChunkWriteStateEntity $expected, Closure $data): void
/**
* @param Closure(): TData|TData $data
*/
public function assert(ChunkWriteStateEntity $expected, Closure|iterable $data): void
{
$state = (new ChunkWriteService())->write($data);
Assert::assertEquals($expected, $state);
Expand Down

0 comments on commit 9e21547

Please sign in to comment.