From e1edc39fd0901a1460117c2cb83e83fd2323e700 Mon Sep 17 00:00:00 2001 From: vlakoff Date: Fri, 11 Nov 2016 14:24:31 +0100 Subject: [PATCH] [5.3] Add tests for non-positive arguments in query builder pagination (#16359) * Add tests for skip, take, forPage with arguments <= 0 * Add tests for chunk with count zero --- .../Database/DatabaseEloquentBuilderTest.php | 30 ++++++++++ tests/Database/DatabaseQueryBuilderTest.php | 57 ++++++++++++++++++- 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/tests/Database/DatabaseEloquentBuilderTest.php b/tests/Database/DatabaseEloquentBuilderTest.php index bd6722fb9c6e..51ed426ea13a 100755 --- a/tests/Database/DatabaseEloquentBuilderTest.php +++ b/tests/Database/DatabaseEloquentBuilderTest.php @@ -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()]); @@ -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(); diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index 46c207fc1d4e..b2d857f4bf0d 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -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() @@ -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(); @@ -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();