Skip to content

Commit e1edc39

Browse files
vlakoffLe Minh Tri
authored andcommitted
[5.3] Add tests for non-positive arguments in query builder pagination (laravel#16359)
* Add tests for skip, take, forPage with arguments <= 0 * Add tests for chunk with count zero
1 parent efa36d8 commit e1edc39

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

tests/Database/DatabaseEloquentBuilderTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,21 @@ public function testChunkCanBeStoppedByReturningFalse()
213213
});
214214
}
215215

216+
public function testChunkWithCountZero()
217+
{
218+
$builder = m::mock('Illuminate\Database\Eloquent\Builder[forPage,get]', [$this->getMockQueryBuilder()]);
219+
$chunk = new Collection([]);
220+
$builder->shouldReceive('forPage')->once()->with(1, 0)->andReturnSelf();
221+
$builder->shouldReceive('get')->times(1)->andReturn($chunk);
222+
223+
$callbackAssertor = m::mock('StdClass');
224+
$callbackAssertor->shouldReceive('doSomething')->never();
225+
226+
$builder->chunk(0, function ($results) use ($callbackAssertor) {
227+
$callbackAssertor->doSomething($results);
228+
});
229+
}
230+
216231
public function testChunkPaginatesUsingIdWithLastChunkComplete()
217232
{
218233
$builder = m::mock('Illuminate\Database\Eloquent\Builder[forPageAfterId,get]', [$this->getMockQueryBuilder()]);
@@ -252,6 +267,21 @@ public function testChunkPaginatesUsingIdWithLastChunkPartial()
252267
}, 'someIdField');
253268
}
254269

270+
public function testChunkPaginatesUsingIdWithCountZero()
271+
{
272+
$builder = m::mock('Illuminate\Database\Eloquent\Builder[forPageAfterId,get]', [$this->getMockQueryBuilder()]);
273+
$chunk = new Collection([]);
274+
$builder->shouldReceive('forPageAfterId')->once()->with(0, 0, 'someIdField')->andReturnSelf();
275+
$builder->shouldReceive('get')->times(1)->andReturn($chunk);
276+
277+
$callbackAssertor = m::mock('StdClass');
278+
$callbackAssertor->shouldReceive('doSomething')->never();
279+
280+
$builder->chunkById(0, function ($results) use ($callbackAssertor) {
281+
$callbackAssertor->doSomething($results);
282+
}, 'someIdField');
283+
}
284+
255285
public function testPluckReturnsTheMutatedAttributesOfAModel()
256286
{
257287
$builder = $this->getBuilder();

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,16 +685,39 @@ public function testLimitsAndOffsets()
685685
$this->assertEquals('select * from "users" limit 10 offset 5', $builder->toSql());
686686

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

691+
$builder = $this->getBuilder();
692+
$builder->select('*')->from('users')->skip(-5)->take(-10);
693+
$this->assertEquals('select * from "users" offset 0', $builder->toSql());
694+
}
695+
696+
public function testForPage()
697+
{
691698
$builder = $this->getBuilder();
692699
$builder->select('*')->from('users')->forPage(2, 15);
693700
$this->assertEquals('select * from "users" limit 15 offset 15', $builder->toSql());
694701

702+
$builder = $this->getBuilder();
703+
$builder->select('*')->from('users')->forPage(0, 15);
704+
$this->assertEquals('select * from "users" limit 15 offset 0', $builder->toSql());
705+
695706
$builder = $this->getBuilder();
696707
$builder->select('*')->from('users')->forPage(-2, 15);
697708
$this->assertEquals('select * from "users" limit 15 offset 0', $builder->toSql());
709+
710+
$builder = $this->getBuilder();
711+
$builder->select('*')->from('users')->forPage(2, 0);
712+
$this->assertEquals('select * from "users" limit 0 offset 0', $builder->toSql());
713+
714+
$builder = $this->getBuilder();
715+
$builder->select('*')->from('users')->forPage(0, 0);
716+
$this->assertEquals('select * from "users" limit 0 offset 0', $builder->toSql());
717+
718+
$builder = $this->getBuilder();
719+
$builder->select('*')->from('users')->forPage(-2, 0);
720+
$this->assertEquals('select * from "users" limit 0 offset 0', $builder->toSql());
698721
}
699722

700723
public function testGetCountForPaginationWithBindings()
@@ -1799,6 +1822,21 @@ public function testChunkCanBeStoppedByReturningFalse()
17991822
});
18001823
}
18011824

1825+
public function testChunkWithCountZero()
1826+
{
1827+
$builder = $this->getMockQueryBuilder();
1828+
$chunk = collect([]);
1829+
$builder->shouldReceive('forPage')->once()->with(1, 0)->andReturnSelf();
1830+
$builder->shouldReceive('get')->times(1)->andReturn($chunk);
1831+
1832+
$callbackAssertor = m::mock('StdClass');
1833+
$callbackAssertor->shouldReceive('doSomething')->never();
1834+
1835+
$builder->chunk(0, function ($results) use ($callbackAssertor) {
1836+
$callbackAssertor->doSomething($results);
1837+
});
1838+
}
1839+
18021840
public function testChunkPaginatesUsingIdWithLastChunkComplete()
18031841
{
18041842
$builder = $this->getMockQueryBuilder();
@@ -1838,6 +1876,21 @@ public function testChunkPaginatesUsingIdWithLastChunkPartial()
18381876
}, 'someIdField');
18391877
}
18401878

1879+
public function testChunkPaginatesUsingIdWithCountZero()
1880+
{
1881+
$builder = $this->getMockQueryBuilder();
1882+
$chunk = collect([]);
1883+
$builder->shouldReceive('forPageAfterId')->once()->with(0, 0, 'someIdField')->andReturnSelf();
1884+
$builder->shouldReceive('get')->times(1)->andReturn($chunk);
1885+
1886+
$callbackAssertor = m::mock('StdClass');
1887+
$callbackAssertor->shouldReceive('doSomething')->never();
1888+
1889+
$builder->chunkById(0, function ($results) use ($callbackAssertor) {
1890+
$callbackAssertor->doSomething($results);
1891+
}, 'someIdField');
1892+
}
1893+
18411894
public function testChunkPaginatesUsingIdWithAlias()
18421895
{
18431896
$builder = $this->getMockQueryBuilder();

0 commit comments

Comments
 (0)