From 6d009a3e0f4aaddfd175a1ffd8ad2a9eab114656 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 29 Sep 2023 03:07:38 -0400 Subject: [PATCH] Add chunk method to BaseCollection - Add test for BaseCollection->chunk --- src/BaseCollection.php | 18 +++++++++ tests/src/BaseCollectionTest.php | 66 ++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 tests/src/BaseCollectionTest.php diff --git a/src/BaseCollection.php b/src/BaseCollection.php index 97e772f168..f2a64151d8 100644 --- a/src/BaseCollection.php +++ b/src/BaseCollection.php @@ -129,6 +129,24 @@ class BaseCollection extends \ArrayIterator return new static(array_reverse($this->getArrayCopy()), $this->getTotalCount()); } + /** + * Split the collection in smaller collections no bigger than the provided length + * + * @param int $length + * @return static[] + */ + public function chunk(int $length): array + { + if ($length < 1) { + throw new \RangeException('BaseCollection->chunk(): Size parameter expected to be greater than 0'); + } + + return array_map(function ($array) { + return new static($array); + }, array_chunk($this->getArrayCopy(), $length)); + } + + /** * @inheritDoc * diff --git a/tests/src/BaseCollectionTest.php b/tests/src/BaseCollectionTest.php new file mode 100644 index 0000000000..ce7fb4670e --- /dev/null +++ b/tests/src/BaseCollectionTest.php @@ -0,0 +1,66 @@ +. + * + */ + +namespace Friendica\Test\src; + +use Friendica\BaseCollection; +use Friendica\BaseEntity; +use Mockery\Mock; +use PHPUnit\Framework\TestCase; + +class BaseCollectionTest extends TestCase +{ + public function testChunk() + { + $entity1 = \Mockery::mock(BaseEntity::class); + $entity2 = \Mockery::mock(BaseEntity::class); + $entity3 = \Mockery::mock(BaseEntity::class); + $entity4 = \Mockery::mock(BaseEntity::class); + + $collection = new BaseCollection([$entity1, $entity2]); + + $this->assertEquals([new BaseCollection([$entity1]), new BaseCollection([$entity2])], $collection->chunk(1)); + $this->assertEquals([new BaseCollection([$entity1, $entity2])], $collection->chunk(2)); + + $collection = new BaseCollection([$entity1, $entity2, $entity3]); + + $this->assertEquals([new BaseCollection([$entity1]), new BaseCollection([$entity2]), new BaseCollection([$entity3])], $collection->chunk(1)); + $this->assertEquals([new BaseCollection([$entity1, $entity2]), new BaseCollection([$entity3])], $collection->chunk(2)); + $this->assertEquals([new BaseCollection([$entity1, $entity2, $entity3])], $collection->chunk(3)); + + $collection = new BaseCollection([$entity1, $entity2, $entity3, $entity4]); + + $this->assertEquals([new BaseCollection([$entity1, $entity2]), new BaseCollection([$entity3, $entity4])], $collection->chunk(2)); + $this->assertEquals([new BaseCollection([$entity1, $entity2, $entity3]), new BaseCollection([$entity4])], $collection->chunk(3)); + $this->assertEquals([new BaseCollection([$entity1, $entity2, $entity3, $entity4])], $collection->chunk(4)); + } + + public function testChunkLengthException() + { + $this->expectException(\RangeException::class); + + $entity1 = \Mockery::mock(BaseEntity::class); + + $collection = new BaseCollection([$entity1]); + + $collection->chunk(0); + } +}