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

[9.x] Allow chunkById on Arrays, as well as Models #43666

Merged
merged 1 commit into from Aug 12, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Concerns/BuildsQueries.php
Expand Up @@ -140,7 +140,7 @@ public function chunkById($count, callable $callback, $column = null, $alias = n
return false;
}

$lastId = $results->last()->{$alias};
$lastId = data_get($results->last(), $alias);

if ($lastId === null) {
throw new RuntimeException("The chunkById operation was aborted because the [{$alias}] column is not present in the query result.");
Expand Down
23 changes: 23 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Expand Up @@ -3694,6 +3694,29 @@ public function testChunkWithCountZero()
});
}

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

$chunk1 = collect([['someIdField' => 1], ['someIdField' => 2]]);
$chunk2 = collect([['someIdField' => 10], ['someIdField' => 11]]);
$chunk3 = collect([]);
$builder->shouldReceive('forPageAfterId')->once()->with(2, 0, 'someIdField')->andReturnSelf();
$builder->shouldReceive('forPageAfterId')->once()->with(2, 2, 'someIdField')->andReturnSelf();
$builder->shouldReceive('forPageAfterId')->once()->with(2, 11, 'someIdField')->andReturnSelf();
$builder->shouldReceive('get')->times(3)->andReturn($chunk1, $chunk2, $chunk3);

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

$builder->chunkById(2, function ($results) use ($callbackAssertor) {
$callbackAssertor->doSomething($results);
}, 'someIdField');
}

public function testChunkPaginatesUsingIdWithLastChunkComplete()
{
$builder = $this->getMockQueryBuilder();
Expand Down