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

[5.1] Fix for chunk() #9681

Closed
wants to merge 2 commits into from
Closed
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
23 changes: 20 additions & 3 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,14 @@ public function pluck($column)
*/
public function chunk($count, callable $callback)
{
$results = $this->forPage($page = 1, $count)->get();
$originalOffset = $this->query->getOffset();
$originalLimit = $this->query->getLimit();

$offset = is_null($originalOffset) ? 0 : $originalOffset;
$limit = is_null($originalLimit) ? $count : min($count, $originalLimit);

$totalResultsFetched = 0;
$results = $this->skip($offset)->take($limit)->get();

while (count($results) > 0) {
// On each chunk result set, we will pass them to the callback and then let the
Expand All @@ -226,9 +233,19 @@ public function chunk($count, callable $callback)
break;
}

$page++;
$totalResultsFetched += count($results);
$offset += $limit;

if (!is_null($originalLimit)) {
$resultsLeft = $originalLimit - $totalResultsFetched;
if ($resultsLeft === 0) {
break;
}

$limit = min($limit, $resultsLeft);
}

$results = $this->forPage($page, $count)->get();
$results = $this->skip($offset)->take($limit)->get();
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,18 @@ public function offset($value)
return $this;
}

/**
* Get the current "offset" value of the query.
*
* @return int|null
*/
public function getOffset()
{
$property = $this->unions ? 'unionOffset' : 'offset';

return $this->$property;
}

/**
* Alias to set the "offset" value of the query.
*
Expand Down Expand Up @@ -1195,6 +1207,18 @@ public function limit($value)
return $this;
}

/**
* Get the current "limit" value of the query.
*
* @return int|null
*/
public function getLimit()
{
$property = $this->unions ? 'unionLimit' : 'limit';

return $this->$property;
}

/**
* Alias to set the "limit" value of the query.
*
Expand Down
63 changes: 56 additions & 7 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,13 @@ public function testValueMethodWithModelNotFound()

public function testChunkExecuteCallbackOverPaginatedRequest()
{
$builder = m::mock('Illuminate\Database\Eloquent\Builder[forPage,get]', [$this->getMockQueryBuilder()]);
$builder->shouldReceive('forPage')->once()->with(1, 2)->andReturn($builder);
$builder->shouldReceive('forPage')->once()->with(2, 2)->andReturn($builder);
$builder->shouldReceive('forPage')->once()->with(3, 2)->andReturn($builder);
$builder = m::mock('Illuminate\Database\Eloquent\Builder[skip,take,get]', [$this->getMockQueryBuilder()]);
$builder->getQuery()->shouldReceive('getOffset')->andReturn(null);
$builder->getQuery()->shouldReceive('getLimit')->andReturn(null);
$builder->shouldReceive('skip')->once()->with(0)->andReturn($builder);
$builder->shouldReceive('skip')->once()->with(2)->andReturn($builder);
$builder->shouldReceive('skip')->once()->with(4)->andReturn($builder);
$builder->shouldReceive('take')->times(3)->with(2)->andReturn($builder);
$builder->shouldReceive('get')->times(3)->andReturn(['foo1', 'foo2'], ['foo3'], []);

$callbackExecutionAssertor = m::mock('StdClass');
Expand All @@ -173,9 +176,12 @@ public function testChunkExecuteCallbackOverPaginatedRequest()

public function testChunkCanBeStoppedByReturningFalse()
{
$builder = m::mock('Illuminate\Database\Eloquent\Builder[forPage,get]', [$this->getMockQueryBuilder()]);
$builder->shouldReceive('forPage')->once()->with(1, 2)->andReturn($builder);
$builder->shouldReceive('forPage')->never()->with(2, 2);
$builder = m::mock('Illuminate\Database\Eloquent\Builder[skip,take,get]', [$this->getMockQueryBuilder()]);
$builder->getQuery()->shouldReceive('getOffset')->andReturn(null);
$builder->getQuery()->shouldReceive('getLimit')->andReturn(null);
$builder->shouldReceive('skip')->once()->with(0)->andReturn($builder);
$builder->shouldReceive('take')->once()->with(2)->andReturn($builder);
$builder->shouldReceive('skip')->never()->with(2);
$builder->shouldReceive('get')->times(1)->andReturn(['foo1', 'foo2']);

$callbackExecutionAssertor = m::mock('StdClass');
Expand All @@ -192,6 +198,49 @@ public function testChunkCanBeStoppedByReturningFalse()
});
}

public function testChunkWithSmallLimit()
{
$builder = m::mock('Illuminate\Database\Eloquent\Builder[skip,take,get]', [$this->getMockQueryBuilder()]);
$builder->getQuery()->shouldReceive('getOffset')->andReturn(1);
$builder->getQuery()->shouldReceive('getLimit')->andReturn(1);
$builder->shouldReceive('skip')->once()->with(1)->andReturn($builder);
$builder->shouldReceive('take')->times(1)->with(1)->andReturn($builder);
$builder->shouldReceive('get')->times(1)->andReturn(['foo2']);

$callbackExecutionAssertor = m::mock('StdClass');
$callbackExecutionAssertor->shouldReceive('doSomething')->with('foo1')->never();
$callbackExecutionAssertor->shouldReceive('doSomething')->with('foo2')->once();
$callbackExecutionAssertor->shouldReceive('doSomething')->with('foo3')->never();

$builder->chunk(2, function ($results) use ($callbackExecutionAssertor) {
foreach ($results as $result) {
$callbackExecutionAssertor->doSomething($result);
}
});
}

public function testChunkWithBigLimit()
{
$builder = m::mock('Illuminate\Database\Eloquent\Builder[skip,take,get]', [$this->getMockQueryBuilder()]);
$builder->getQuery()->shouldReceive('getOffset')->andReturn(1);
$builder->getQuery()->shouldReceive('getLimit')->andReturn(100);
$builder->shouldReceive('skip')->once()->with(1)->andReturn($builder);
$builder->shouldReceive('skip')->once()->with(3)->andReturn($builder);
$builder->shouldReceive('take')->times(2)->with(2)->andReturn($builder);
$builder->shouldReceive('get')->times(2)->andReturn(['foo2', 'foo3'], []);

$callbackExecutionAssertor = m::mock('StdClass');
$callbackExecutionAssertor->shouldReceive('doSomething')->with('foo1')->never();
$callbackExecutionAssertor->shouldReceive('doSomething')->with('foo2')->once();
$callbackExecutionAssertor->shouldReceive('doSomething')->with('foo3')->once();

$builder->chunk(2, function ($results) use ($callbackExecutionAssertor) {
foreach ($results as $result) {
$callbackExecutionAssertor->doSomething($result);
}
});
}

public function testListsReturnsTheMutatedAttributesOfAModel()
{
$builder = $this->getBuilder();
Expand Down