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.2] Add chunkById query builder tests #12906

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
14 changes: 6 additions & 8 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,8 @@ public function value($column)
/**
* Chunk the results of the query.
*
* @param int $count
* @param callable $callback
* @param int $count
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll get hit on the fingers for these! 😱

The formats are @param__type__$variable (2/2 spaces) and @return_type (1 space).
Always the same number of spaces, no vertical alignment.

* @param callable $callback
* @return bool
*/
public function chunk($count, callable $callback)
Expand All @@ -357,9 +357,9 @@ public function chunk($count, callable $callback)
/**
* Chunk the results of a query by comparing numeric IDs.
*
* @param int $count
* @param callable $callback
* @param string $column
* @param int $count
* @param callable $callback
* @param string $column
* @return bool
*/
public function chunkById($count, callable $callback, $column = 'id')
Expand All @@ -373,9 +373,7 @@ public function chunkById($count, callable $callback, $column = 'id')
return false;
}

if ($column) {
$lastId = $results->last()->{$column};
}
$lastId = $results->last()->{$column};

$results = $this->pageAfterId($count, $lastId, $column)->get();
}
Expand Down
22 changes: 10 additions & 12 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1335,10 +1335,10 @@ public function forPage($page, $perPage = 15)
/**
* Constrain the query to the next "page" of results after a given ID.
*
* @param int $perPage
* @param int $column
* @param string $column
* @return Builder|static
* @param int $perPage
* @param int $lastId
* @param string $column
* @return \Illuminate\Database\Query\Builder|static
*/
public function pageAfterId($perPage = 15, $lastId = 0, $column = 'id')
{
Expand Down Expand Up @@ -1621,8 +1621,8 @@ protected function restoreFieldsForCount()
/**
* Chunk the results of the query.
*
* @param int $count
* @param callable $callback
* @param int $count
* @param callable $callback
* @return bool
*/
public function chunk($count, callable $callback)
Expand All @@ -1648,9 +1648,9 @@ public function chunk($count, callable $callback)
/**
* Chunk the results of a query by comparing numeric IDs.
*
* @param int $count
* @param callable $callback
* @param string $column
* @param int $count
* @param callable $callback
* @param string $column
* @return bool
*/
public function chunkById($count, callable $callback, $column = 'id')
Expand All @@ -1664,9 +1664,7 @@ public function chunkById($count, callable $callback, $column = 'id')
return false;
}

if ($column) {
$lastId = last($results)->{$column};
}
$lastId = last($results)->{$column};

$results = $this->pageAfterId($count, $lastId, $column)->get();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public function testChunkCanBeStoppedByReturningFalse()
});
}

public function testChunkPaginatesUsingWhereBetweenIds()
public function testChunkPaginatesUsingId()
{
$builder = m::mock('Illuminate\Database\Eloquent\Builder[pageAfterId,get]', [$this->getMockQueryBuilder()]);
$builder->shouldReceive('pageAfterId')->once()->with(2, 0, 'someIdField')->andReturn($builder);
Expand Down
32 changes: 32 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1470,6 +1470,23 @@ public function testTableValuedFunctionAsTableInSqlServer()
$this->assertEquals('select * from [users](1,2)', $builder->toSql());
}

public function testChunkPaginatesUsingId()
{
$builder = $this->getMockQueryBuilder();

$builder->shouldReceive('pageAfterId')->once()->with(2, 0, 'someIdField')->andReturn($builder);
$builder->shouldReceive('pageAfterId')->once()->with(2, 2, 'someIdField')->andReturn($builder);
$builder->shouldReceive('pageAfterId')->once()->with(2, 10, 'someIdField')->andReturn($builder);

$builder->shouldReceive('get')->times(3)->andReturn(
[(object) ['someIdField' => 1], (object) ['someIdField' => 2]],
[(object) ['someIdField' => 10]],
[]
);

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

protected function getBuilder()
{
$grammar = new Illuminate\Database\Query\Grammars\Grammar;
Expand Down Expand Up @@ -1517,4 +1534,19 @@ protected function getMySqlBuilderWithProcessor()

return new Builder(m::mock('Illuminate\Database\ConnectionInterface'), $grammar, $processor);
}

/**
* @return m\MockInterface
*/
protected function getMockQueryBuilder()
{

$builder = m::mock('Illuminate\Database\Query\Builder[pageAfterId,get]', [
m::mock('Illuminate\Database\ConnectionInterface'),
new Illuminate\Database\Query\Grammars\Grammar,
m::mock('Illuminate\Database\Query\Processors\Processor'),
]);

return $builder;
}
}