Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ChunkWriteService): support iterable directly #99

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading