diff --git a/CHANGELOG.md b/CHANGELOG.md index 589e02f..14063e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,11 @@ # Meritoo Common Library Common and useful classes, methods, exceptions etc. -## 0.0.19 +# 0.0.20 + +1. Collection > add() method > treat empty string as not provided index (same as null) + +# 0.0.19 1. Add this changelog 2. Reorganize documentation & update [Readme](README.md) diff --git a/composer.json b/composer.json index d1fc9df..8303f1b 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "meritoo/common-library", "description": "Useful classes, methods, extensions etc.", "license": "MIT", - "version": "0.0.19", + "version": "0.0.20", "authors": [ { "name": "Meritoo.pl", diff --git a/src/Collection/Collection.php b/src/Collection/Collection.php index 75770c2..5ecc7d8 100644 --- a/src/Collection/Collection.php +++ b/src/Collection/Collection.php @@ -109,7 +109,7 @@ class Collection implements Countable, ArrayAccess, IteratorAggregate */ public function add($element, $index = null) { - if (null === $index) { + if (null === $index || '' === $index) { $this->elements[] = $element; } else { $this->elements[$index] = $element; diff --git a/tests/Collection/CollectionTest.php b/tests/Collection/CollectionTest.php index 60a4479..97f2b38 100644 --- a/tests/Collection/CollectionTest.php +++ b/tests/Collection/CollectionTest.php @@ -9,6 +9,7 @@ namespace Meritoo\Common\Test\Collection; use ArrayIterator; +use Generator; use Meritoo\Common\Collection\Collection; use Meritoo\Common\Test\Base\BaseTestCase; use Meritoo\Common\Type\OopVisibilityType; @@ -129,22 +130,39 @@ class CollectionTest extends BaseTestCase static::assertInstanceOf(ArrayIterator::class, $this->simpleCollection->getIterator()); } - public function testAdd() + /** + * @param mixed $element The element to add + * @param int $expectedCount Expected count of elements in collection + * @param int $expectedIndex Expected index of added element in collection + * @param Collection $collection The collection + * + * @dataProvider provideElementToAdd + */ + public function testAddWithoutIndex($element, $expectedCount, $expectedIndex, Collection $collection) { - $this->emptyCollection->add('test1'); + $collection->add($element); - static::assertTrue($this->emptyCollection->has('test1')); - static::assertEquals(1, $this->emptyCollection->count()); - static::assertEquals('test1', $this->emptyCollection[0]); + static::assertTrue($collection->has($element)); + static::assertEquals($expectedCount, $collection->count()); + static::assertEquals($element, $collection[$expectedIndex]); } - public function testAddWithIndex() + /** + * @param mixed $element The element to add + * @param mixed $index Index of element to add + * @param int $expectedCount Expected count of elements in collection + * @param int $expectedIndex Expected index of added element in collection + * @param Collection $collection The collection + * + * @dataProvider provideElementToAddWithIndex + */ + public function testAddWithIndex($element, $index, $expectedCount, $expectedIndex, Collection $collection) { - $this->emptyCollection->add('test2', 1234); + $collection->add($element, $index); - static::assertTrue($this->emptyCollection->has('test2')); - static::assertEquals(1, $this->emptyCollection->count()); - static::assertEquals('test2', $this->emptyCollection[1234]); + static::assertTrue($collection->has($element)); + static::assertEquals($expectedCount, $collection->count()); + static::assertEquals($element, $collection[$expectedIndex]); } public function testAddMultipleUsingEmptyArray() @@ -309,6 +327,87 @@ class CollectionTest extends BaseTestCase static::assertMethodVisibilityAndArguments(Collection::class, 'exists', OopVisibilityType::IS_PRIVATE, 1, 1); } + /** + * Provides element to add to collection + * + * @return Generator + */ + public function provideElementToAdd() + { + $collection = new Collection(); + + yield[ + 'test1', + 1, + 0, + $collection, + ]; + + yield[ + 'test2', + 2, + 1, + $collection, + ]; + + yield[ + 'test3', + 3, + 2, + $collection, + ]; + } + + /** + * Provides element with index to add to collection + * + * @return Generator + */ + public function provideElementToAddWithIndex() + { + $collection = new Collection(); + + yield[ + 'test1', + 'aa', + 1, + 'aa', + $collection, + ]; + + yield[ + 'test2', + 'oo', + 2, + 'oo', + $collection, + ]; + + yield[ + 'test3', + null, + 3, + 0, + $collection, + ]; + + yield[ + 'test4', + '', + 4, + 1, + $collection, + ]; + + yield[ + 'test5', + 'vv', + 5, + 'vv', + $collection, + ]; + } + /** * {@inheritdoc} */