From 5e6de194c6e56d381da3a13c55ae0311cd2280c2 Mon Sep 17 00:00:00 2001 From: jstoone Date: Tue, 1 Nov 2016 16:34:02 +0100 Subject: [PATCH] Throw invalid argument exception if value below zero is given --- src/Illuminate/Support/Collection.php | 6 ++++++ tests/Support/SupportCollectionTest.php | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index a129c9c618b2..dde214e8f1fa 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -955,6 +955,12 @@ 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; } diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index cbc809abd910..d6af9d730dd4 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -692,6 +692,17 @@ public function testChunkWhenGivenZeroAsSize() ); } + 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([