Skip to content

Commit

Permalink
Added: ImmutableArrayCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
sauls committed Feb 17, 2018
1 parent 421fe0a commit c7f4744
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 2 deletions.
6 changes: 5 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
<exclude>
<file>src/ImmutableArrayCollection.php</file>
</exclude>

</whitelist>
</filter>

<logging>
<!--<log type="coverage-clover" target="build/logs/clover.xml"/>-->
<log type="coverage-text" target="php://stdout" showOnlySummary="true"/>
</logging>
</phpunit>
</phpunit>
18 changes: 18 additions & 0 deletions src/Exception/UnsupportedOperationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* This file is part of the sauls/collections package.
*
* @author Saulius Vaičeliūnas <vaiceliunas@inbox.lt>
* @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.';
}
76 changes: 76 additions & 0 deletions src/ImmutableArrayCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* This file is part of the sauls/collections package.
*
* @author Saulius Vaičeliūnas <vaiceliunas@inbox.lt>
* @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();
}


}
109 changes: 108 additions & 1 deletion tests/ArrayCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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');


}
}

0 comments on commit c7f4744

Please sign in to comment.