Skip to content

Commit

Permalink
Allow chunkById on Arrays, as well as Models (#43666)
Browse files Browse the repository at this point in the history
  • Loading branch information
BennyC authored Aug 12, 2022
1 parent fdf3ae9 commit 8cde478
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Concerns/BuildsQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,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
Original file line number Diff line number Diff line change
Expand Up @@ -3841,6 +3841,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

0 comments on commit 8cde478

Please sign in to comment.