Skip to content

Commit

Permalink
Merge branch 'fix-empty-collection-chunk' of https://github.com/jstoo…
Browse files Browse the repository at this point in the history
…ne/framework into jstoone-fix-empty-collection-chunk
  • Loading branch information
taylorotwell committed Nov 1, 2016
2 parents 5a9c646 + 5e6de19 commit 11d30ff
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,16 @@ public function split($numberOfGroups)
*/
public function chunk($size)
{
if ($size < 0) {
throw new InvalidArgumentException(
'Size parameter expected to be greater than 0'
);
}

if ($size == 0) {
return new static;
}

$chunks = [];

foreach (array_chunk($this->items, $size, true) as $chunk) {
Expand Down
21 changes: 21 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,27 @@ public function testChunk()
$this->assertEquals([9 => 10], $data[3]->toArray());
}

public function testChunkWhenGivenZeroAsSize()
{
$collection = new Collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);

$this->assertEquals(
[],
$collection->chunk(0)->toArray()
);
}

public function testChunkWhenGivenLessThanZero()
{
$collection = new Collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);

$this->expectException(InvalidArgumentException::class);
$this->assertEquals(
[],
$collection->chunk(-1)->toArray()
);
}

public function testEvery()
{
$data = new Collection([
Expand Down

0 comments on commit 11d30ff

Please sign in to comment.