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 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
14 changes: 6 additions & 8 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ public function value($column)
*
* @param int $count
* @param callable $callback
* @return bool
* @return bool
Copy link
Contributor

Choose a reason for hiding this comment

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

I wrote @return_type, one space!

(also you may want to stash the commits to not clutter git history)

Copy link
Member

Choose a reason for hiding this comment

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

don't worry about phpdoc cs - as long a it's valid phpdoc, taylor or I can clean it up after merge

Copy link
Member

Choose a reason for hiding this comment

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

Please do squash this to one commit though.

*/
public function chunk($count, callable $callback)
{
Expand All @@ -357,10 +357,10 @@ 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
* @return bool
* @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 @@ -1623,7 +1623,7 @@ protected function restoreFieldsForCount()
*
* @param int $count
* @param callable $callback
* @return bool
* @return bool
*/
public function chunk($count, callable $callback)
{
Expand All @@ -1648,10 +1648,10 @@ 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
* @return bool
* @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
31 changes: 31 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,18 @@ 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;
}
}