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.3] Add tests for non-positive arguments in query builder pagination #16359

Merged
merged 2 commits into from
Nov 11, 2016
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
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