From 682fdad835a67e610db16096b16f1dfdbb2d1195 Mon Sep 17 00:00:00 2001 From: Eugene Ritter Date: Mon, 26 Mar 2018 09:42:32 -0500 Subject: [PATCH] Lorenzo's suggested changes to appendItem --- src/Collection/CollectionTrait.php | 36 ++++++++++++ tests/TestCase/Collection/CollectionTest.php | 59 ++++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/src/Collection/CollectionTrait.php b/src/Collection/CollectionTrait.php index 82454cda17f..6b39b572913 100644 --- a/src/Collection/CollectionTrait.php +++ b/src/Collection/CollectionTrait.php @@ -411,6 +411,42 @@ public function append($items) return new Collection($list); } + /** + * {@inheritDoc} + */ + public function appendItem($item, $key = null) + { + if ($key !== null) { + $data = [$key => $item]; + } else { + $data = [$item]; + } + + return $this->append($data); + } + + /** + * {@inheritDoc} + */ + public function prepend($items) + { + return (new Collection($items))->append($this); + } + + /** + * {@inheritDoc} + */ + public function prependItem($item, $key = null) + { + if ($key !== null) { + $data = [$key => $item]; + } else { + $data = [$item]; + } + + return $this->prepend($data); + } + /** * {@inheritDoc} */ diff --git a/tests/TestCase/Collection/CollectionTest.php b/tests/TestCase/Collection/CollectionTest.php index 47dcf610b7c..050b55b6fdb 100644 --- a/tests/TestCase/Collection/CollectionTest.php +++ b/tests/TestCase/Collection/CollectionTest.php @@ -1149,6 +1149,65 @@ public function testAppend() $this->assertEquals(['a' => 4, 'b' => 2, 'c' => 3], $combined->toArray()); } + /** + * Tests the appendItem method + */ + public function testAppendItem() + { + $collection = new Collection([1, 2, 3]); + $combined = $collection->appendItem(4); + $this->assertEquals([1, 2, 3, 4], $combined->toArray(false)); + + $collection = new Collection(['a' => 1, 'b' => 2]); + $combined = $collection->appendItem(3, 'c'); + $combined = $combined->appendItem(4, 'a'); + $this->assertEquals(['a' => 4, 'b' => 2, 'c' => 3], $combined->toArray()); + } + + /** + * Tests the prepend method + */ + public function testPrepend() + { + $collection = new Collection([1, 2, 3]); + $combined = $collection->prepend(['a']); + $this->assertEquals(['a', 1, 2, 3], $combined->toArray()); + + $collection = new Collection(['c' => 3, 'd' => 4]); + $combined = $collection->prepend(['a' => 1, 'b' => 2]); + $this->assertEquals(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4], $combined->toArray()); + } + + /** + * Tests prependItem method + */ + public function testPrependItem() + { + $collection = new Collection([1, 2, 3]); + $combined = $collection->prependItem('a'); + $this->assertEquals(['a', 1, 2, 3], $combined->toList()); + + $collection = new Collection(['c' => 3, 'd' => 4]); + $combined = $collection->prependItem(2, 'b'); + $combined = $combined->prependItem(1, 'a'); + $this->assertEquals(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4], $combined->toArray()); + } + + /** + * Tests prependItem method + */ + public function testPrependItemPreserveKeys() + { + $collection = new Collection([1, 2, 3]); + $combined = $collection->prependItem('a'); + $this->assertEquals(['a', 1, 2, 3], $combined->toList()); + + $collection = new Collection(['c' => 3, 'd' => 4]); + $combined = $collection->prependItem(2, 'b'); + $combined = $combined->prependItem(1, 'a'); + $this->assertEquals(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4], $combined->toArray()); + } + /** * Tests the append method with iterator */