From c7f4744a0101d20d3806f583e20bc4ff2280ac84 Mon Sep 17 00:00:00 2001 From: Saulius Date: Sat, 17 Feb 2018 13:16:11 +0200 Subject: [PATCH] Added: ImmutableArrayCollection --- phpunit.xml.dist | 6 +- .../UnsupportedOperationException.php | 18 +++ src/ImmutableArrayCollection.php | 76 ++++++++++++ tests/ArrayCollectionTest.php | 109 +++++++++++++++++- 4 files changed, 207 insertions(+), 2 deletions(-) create mode 100644 src/Exception/UnsupportedOperationException.php create mode 100644 src/ImmutableArrayCollection.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 52d9a16..30d807a 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -21,6 +21,10 @@ src/ + + src/ImmutableArrayCollection.php + + @@ -28,4 +32,4 @@ - \ No newline at end of file + diff --git a/src/Exception/UnsupportedOperationException.php b/src/Exception/UnsupportedOperationException.php new file mode 100644 index 0000000..650de73 --- /dev/null +++ b/src/Exception/UnsupportedOperationException.php @@ -0,0 +1,18 @@ + + * @link http://saulius.vaiceliunas.lt + * @copyright 2018 + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sauls\Component\Collection\Exception; + +class UnsupportedOperationException extends \RuntimeException +{ + protected $message = 'Unsupported operation.'; +} diff --git a/src/ImmutableArrayCollection.php b/src/ImmutableArrayCollection.php new file mode 100644 index 0000000..9cbad2f --- /dev/null +++ b/src/ImmutableArrayCollection.php @@ -0,0 +1,76 @@ + + * @link http://saulius.vaiceliunas.lt + * @copyright 2018 + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sauls\Component\Collection; + +use Sauls\Component\Collection\Exception\UnsupportedOperationException; + +class ImmutableArrayCollection extends ArrayCollection +{ + /** + * @throws \Sauls\Component\Collection\Exception\UnsupportedOperationException + */ + public function set($key, $value): void + { + throw new UnsupportedOperationException(); + } + + /** + * @throws \Sauls\Component\Collection\Exception\UnsupportedOperationException + */ + public function add(array $elements): void + { + throw new UnsupportedOperationException(); + } + + /** + * @throws \Sauls\Component\Collection\Exception\UnsupportedOperationException + */ + public function merge(array $elements): void + { + throw new UnsupportedOperationException(); + } + + /** + * @throws \Sauls\Component\Collection\Exception\UnsupportedOperationException + */ + public function replace(array $elements): void + { + throw new UnsupportedOperationException(); + } + + /** + * @throws \Sauls\Component\Collection\Exception\UnsupportedOperationException + */ + public function clear(): void + { + throw new UnsupportedOperationException(); + } + + /** + * @throws \Sauls\Component\Collection\Exception\UnsupportedOperationException + */ + public function removeKey($key) + { + throw new UnsupportedOperationException(); + } + + /** + * @throws \Sauls\Component\Collection\Exception\UnsupportedOperationException + */ + public function removeElement($element) + { + throw new UnsupportedOperationException(); + } + + +} diff --git a/tests/ArrayCollectionTest.php b/tests/ArrayCollectionTest.php index 92d0cdf..4423edc 100644 --- a/tests/ArrayCollectionTest.php +++ b/tests/ArrayCollectionTest.php @@ -12,7 +12,7 @@ namespace Sauls\Component\Collection; -use PHPUnit\Framework\TestCase; +use Sauls\Component\Collection\Exception\UnsupportedOperationException; use function Sauls\Component\Helper\array_multiple_keys_exists; use Sauls\Component\Helper\Exception\PropertyNotAccessibleException; @@ -348,4 +348,111 @@ public function should_return_existence_of_key_or_value() $this->assertTrue($arrayCollection->has('key2.z')); $this->assertFalse($arrayCollection->has('key2.b')); } + + /** + * @test + */ + public function should_throw_unsupported_method_operation_when_trying_to_set_immutable_array_collection(): void + { + $this->expectException(UnsupportedOperationException::class); + $immutableCollection = new ImmutableArrayCollection([ + 'test' => 11, + ]); + + $immutableCollection->set('test', 12); + } + + /** + * @test + */ + public function should_throw_unsupported_method_operation_when_trying_to_add_immutable_array_collection(): void + { + $this->expectException(UnsupportedOperationException::class); + $immutableCollection = new ImmutableArrayCollection([ + 'test' => 11, + ]); + + $immutableCollection->add(['g' => 'b']); + } + + /** + * @test + */ + public function should_throw_unsupported_method_operation_when_trying_to_merge_immutable_array_collection(): void + { + $this->expectException(UnsupportedOperationException::class); + $immutableCollection = new ImmutableArrayCollection([ + 'test' => 11, + ]); + + $immutableCollection->merge(['test' => 'b']); + } + + /** + * @test + */ + public function should_throw_unsupported_method_operation_when_trying_to_replace_immutable_array_collection(): void + { + $this->expectException(UnsupportedOperationException::class); + $immutableCollection = new ImmutableArrayCollection([ + 'test' => 11, + ]); + + $immutableCollection->replace(['test' => 'ccc']); + } + + /** + * @test + */ + public function should_throw_unsupported_method_operation_when_trying_to_clear_immutable_array_collection(): void + { + $this->expectException(UnsupportedOperationException::class); + $immutableCollection = new ImmutableArrayCollection([ + 'test' => 11, + ]); + + $immutableCollection->clear(); + } + + /** + * @test + */ + public function should_throw_unsupported_method_operation_when_trying_to_remove_key_from_immutable_array_collection(): void + { + $this->expectException(UnsupportedOperationException::class); + $immutableCollection = new ImmutableArrayCollection([ + 'test' => 11, + ]); + + $immutableCollection->removeKey('test'); + } + + /** + * @test + */ + public function should_throw_unsupported_method_operation_when_trying_to_remove_element_from_immutable_array_collection(): void + { + $this->expectException(UnsupportedOperationException::class); + $immutableCollection = new ImmutableArrayCollection([ + 'test' => 11, + ]); + + $immutableCollection->removeElement(11); + } + + /** + * @test + */ + public function should_create_immutable_array(): void + { + $this->expectException(UnsupportedOperationException::class); + $array = ['test' => 'test']; + $arrayCollection = new ArrayCollection($array); + $immutableArrayCollection = (new ImmutableArrayCollection())->create($arrayCollection->all()); + + $this->assertSame($array, $immutableArrayCollection->all()); + $immutableArrayCollection->set('a', 'b'); + + + } }