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

[Backport] chunkById Database query builder method #7

Merged
merged 14 commits into from
Sep 22, 2023
Merged
Prev Previous commit
Next Next commit
Add and pass test testChunkPaginatesUsingIdWithAlias
abdulhp committed Sep 20, 2023
commit 698970c4fb1fea0b63eebd817b9a41c33540af7a
20 changes: 20 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
@@ -1448,6 +1448,26 @@ public function testChunkPaginatesUsingIdWithCountZero(): void
}, 'someIdField');
}

public function testChunkPaginatesUsingIdWithAlias(): void
{
$builder = $this->getMockQueryBuilder();
$builder->orders[] = ['column' => 'foobar', 'direction' => 'asc'];

$chunk1 = Collection::make([(object) ['table_id' => 1], (object) ['table_id' => 10]]);
$chunk2 = Collection::make([]);
$builder->shouldReceive('forPageAfterId')->once()->with(2, 0, 'table.id')->andReturnSelf();
$builder->shouldReceive('forPageAfterId')->once()->with(2, 10, 'table.id')->andReturnSelf();
$builder->shouldReceive('get')->times(2)->andReturn($chunk1, $chunk2);

$callbackAssertor = m::mock(stdClass::class);
$callbackAssertor->shouldReceive('doSomething')->once()->with($chunk1);
$callbackAssertor->shouldReceive('doSomething')->never()->with($chunk2);

$builder->chunkById(2, function ($results) use ($callbackAssertor) {
$callbackAssertor->doSomething($results);
}, 'table.id', 'table_id');
}

protected function getBuilder(): Builder
{
$grammar = new Illuminate\Database\Query\Grammars\Grammar;