Skip to content

Commit

Permalink
[5.3] Add tests for non-positive arguments in query builder pagination (
Browse files Browse the repository at this point in the history
laravel#16359)

* Add tests for skip, take, forPage with arguments <= 0

* Add tests for chunk with count zero
  • Loading branch information
vlakoff authored and Le Minh Tri committed Nov 16, 2016
1 parent efa36d8 commit e1edc39
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
30 changes: 30 additions & 0 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,21 @@ public function testChunkCanBeStoppedByReturningFalse()
});
}

public function testChunkWithCountZero()
{
$builder = m::mock('Illuminate\Database\Eloquent\Builder[forPage,get]', [$this->getMockQueryBuilder()]);
$chunk = new Collection([]);
$builder->shouldReceive('forPage')->once()->with(1, 0)->andReturnSelf();
$builder->shouldReceive('get')->times(1)->andReturn($chunk);

$callbackAssertor = m::mock('StdClass');
$callbackAssertor->shouldReceive('doSomething')->never();

$builder->chunk(0, function ($results) use ($callbackAssertor) {
$callbackAssertor->doSomething($results);
});
}

public function testChunkPaginatesUsingIdWithLastChunkComplete()
{
$builder = m::mock('Illuminate\Database\Eloquent\Builder[forPageAfterId,get]', [$this->getMockQueryBuilder()]);
Expand Down Expand Up @@ -252,6 +267,21 @@ public function testChunkPaginatesUsingIdWithLastChunkPartial()
}, 'someIdField');
}

public function testChunkPaginatesUsingIdWithCountZero()
{
$builder = m::mock('Illuminate\Database\Eloquent\Builder[forPageAfterId,get]', [$this->getMockQueryBuilder()]);
$chunk = new Collection([]);
$builder->shouldReceive('forPageAfterId')->once()->with(0, 0, 'someIdField')->andReturnSelf();
$builder->shouldReceive('get')->times(1)->andReturn($chunk);

$callbackAssertor = m::mock('StdClass');
$callbackAssertor->shouldReceive('doSomething')->never();

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

public function testPluckReturnsTheMutatedAttributesOfAModel()
{
$builder = $this->getBuilder();
Expand Down
57 changes: 55 additions & 2 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -685,16 +685,39 @@ public function testLimitsAndOffsets()
$this->assertEquals('select * from "users" limit 10 offset 5', $builder->toSql());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->skip(-5)->take(10);
$this->assertEquals('select * from "users" limit 10 offset 0', $builder->toSql());
$builder->select('*')->from('users')->skip(0)->take(0);
$this->assertEquals('select * from "users" limit 0 offset 0', $builder->toSql());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->skip(-5)->take(-10);
$this->assertEquals('select * from "users" offset 0', $builder->toSql());
}

public function testForPage()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->forPage(2, 15);
$this->assertEquals('select * from "users" limit 15 offset 15', $builder->toSql());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->forPage(0, 15);
$this->assertEquals('select * from "users" limit 15 offset 0', $builder->toSql());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->forPage(-2, 15);
$this->assertEquals('select * from "users" limit 15 offset 0', $builder->toSql());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->forPage(2, 0);
$this->assertEquals('select * from "users" limit 0 offset 0', $builder->toSql());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->forPage(0, 0);
$this->assertEquals('select * from "users" limit 0 offset 0', $builder->toSql());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->forPage(-2, 0);
$this->assertEquals('select * from "users" limit 0 offset 0', $builder->toSql());
}

public function testGetCountForPaginationWithBindings()
Expand Down Expand Up @@ -1799,6 +1822,21 @@ public function testChunkCanBeStoppedByReturningFalse()
});
}

public function testChunkWithCountZero()
{
$builder = $this->getMockQueryBuilder();
$chunk = collect([]);
$builder->shouldReceive('forPage')->once()->with(1, 0)->andReturnSelf();
$builder->shouldReceive('get')->times(1)->andReturn($chunk);

$callbackAssertor = m::mock('StdClass');
$callbackAssertor->shouldReceive('doSomething')->never();

$builder->chunk(0, function ($results) use ($callbackAssertor) {
$callbackAssertor->doSomething($results);
});
}

public function testChunkPaginatesUsingIdWithLastChunkComplete()
{
$builder = $this->getMockQueryBuilder();
Expand Down Expand Up @@ -1838,6 +1876,21 @@ public function testChunkPaginatesUsingIdWithLastChunkPartial()
}, 'someIdField');
}

public function testChunkPaginatesUsingIdWithCountZero()
{
$builder = $this->getMockQueryBuilder();
$chunk = collect([]);
$builder->shouldReceive('forPageAfterId')->once()->with(0, 0, 'someIdField')->andReturnSelf();
$builder->shouldReceive('get')->times(1)->andReturn($chunk);

$callbackAssertor = m::mock('StdClass');
$callbackAssertor->shouldReceive('doSomething')->never();

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

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

0 comments on commit e1edc39

Please sign in to comment.