Skip to content

Commit

Permalink
Merge pull request #3 from atrapalo/UniqueEntityCollection
Browse files Browse the repository at this point in the history
Unique entity collection
  • Loading branch information
GuilleGF authored Apr 3, 2017
2 parents bcc930b + 8cd2618 commit 0598587
Show file tree
Hide file tree
Showing 15 changed files with 450 additions and 88 deletions.
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<whitelist>
<directory>./src</directory>
<exclude>
<directory>./src/Tester</directory>
<directory>./tests</directory>
<directory>./vendor</directory>
</exclude>
Expand Down
22 changes: 19 additions & 3 deletions src/Collection/EntityCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public function __construct(array $entities, bool $allowEntitiesChildren = false

$this->allowEntitiesChildren = $allowEntitiesChildren;

foreach ($entities as $key => $entity) {
$this->set($key, $entity);
foreach ($entities as $entity) {
$this->add($entity);
}
}

Expand All @@ -57,7 +57,7 @@ public function add($entity)
* @param $entity
* @throws \Exception
*/
public function set($key, $entity)
protected function set($key, $entity)
{
if (!$this->isValid($entity)) {
throw static::customInvalidEntityException();
Expand Down Expand Up @@ -122,6 +122,22 @@ public function toArray(): array
return $this->entities;
}

/**
* @param int $offset
* @param null $length
* @return static
* @throws \Exception
*/
public function slice(int $offset, $length = null)
{
$entities = array_slice($this->entities, $offset, $length, true);
if (empty($entities)) {
throw static::customEmptyException();
}

return new static($entities);
}

/**
* @return mixed
*/
Expand Down
76 changes: 76 additions & 0 deletions src/Collection/UniqueEntityCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Atrapalo\PHPTools\Collection;

/**
* Class UniqueEntityCollection
* @package Collection
*/
abstract class UniqueEntityCollection extends EntityCollection
{
/**
* @param $entity
* @return string
*/
abstract public function entityUniqueId($entity): string;

/**
* @param $entity
* @throws \Exception
*/
public function add($entity)
{
if (!$this->isValid($entity)) {
throw self::customInvalidEntityException();
} elseif ($this->exist($entity)) {
throw self::customDuplicateEntityException();
}

$this->set($this->uniqueId($entity), $entity);
}

/**
* @param $entity
* @return bool
*/
public function exist($entity): bool
{
if (in_array($this->uniqueId($entity), $this->keys())) {
return true;
}

return false;
}

/**
* @param $entity
* @return string
* @throws \Exception
*/
private function uniqueId($entity): string
{
$uniqueId = $this->entityUniqueId($entity);
if (empty($uniqueId)) {
throw static::customInvalidEntityIdException();
}

return $uniqueId;
}

/**
* @return \Exception
*/
public static function customDuplicateEntityException(): \Exception
{
return new \InvalidArgumentException("Entity already exists in collection");
}


/**
* @return \Exception
*/
public static function customInvalidEntityIdException(): \Exception
{
return new \InvalidArgumentException('Entity unique id can not be empty');
}
}
4 changes: 2 additions & 2 deletions src/Enum/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public static function customInvalidValueException(string $value): \Exception
*/
public static function customUnknownStaticMethodException(string $method): \Exception
{
throw new \BadMethodCallException("No static method or enum constant '$method' in class " . get_called_class());
return new \BadMethodCallException("No static method or enum constant '$method' in class " . get_called_class());
}

/**
Expand All @@ -211,6 +211,6 @@ public static function customUnknownStaticMethodException(string $method): \Exce
*/
public static function customUnknownMethodException(string $method): \Exception
{
throw new \BadMethodCallException(sprintf('The method "%s" is not defined.', $method));
return new \BadMethodCallException(sprintf('The method "%s" is not defined.', $method));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

namespace Atrapalo\PHPTools\Collection;
namespace Atrapalo\PHPTools\Tester\Collection;

use Atrapalo\PHPTools\Collection\EntityCollection;

/**
* Class EntityCollectionTester
Expand Down Expand Up @@ -79,23 +81,6 @@ public function invalidAddElement()
$collection->add([]);
}

/**
* @test
*/
public function invalidSetElement()
{
/** @var EntityCollection $entityCollectionClass */
$entityCollectionClass = $this->entityCollectionClass();
$exception = $entityCollectionClass::customInvalidEntityException();

$this->expectException(get_class($exception));
$this->expectExceptionMessage($exception->getMessage());

/** @var EntityCollection $collection */
$collection = new $entityCollectionClass([$this->prophesize($entityCollectionClass::entityClass())->reveal()], true);
$collection->set('key', []);
}

/**
* @test
*/
Expand Down Expand Up @@ -141,7 +126,7 @@ public function addTwoValidElement()
/** @var \Prophecy\Prophecy\ObjectProphecy $entity */
$entity = $this->prophesize($entityCollectionClass::entityClass());
$collection->add($entity->reveal());
$collection->set('key', $entity->reveal());
$collection->add($entity->reveal());

$this->assertSame(3, $collection->count());
foreach ($collection as $element) {
Expand Down
58 changes: 56 additions & 2 deletions src/Enum/EnumPHPDocTester.php → src/Tester/Enum/EnumTester.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

namespace Atrapalo\PHPTools\Enum;
namespace Atrapalo\PHPTools\Tester\Enum;

use Atrapalo\PHPTools\Enum\Enum;
use Atrapalo\PHPTools\Parser\PHPDocClass;

/**
Expand All @@ -10,13 +11,66 @@
*
* @author Guillermo González <[email protected]>
*/
trait EnumPHPDocTester
trait EnumTester
{
/**
* @return string
*/
abstract protected function enumClass(): string;

/**
* @test
*/
public function invalidConstructor()
{
/** @var Enum $enumClass */
$enumClass = $this->enumClass();
$value = md5(openssl_random_pseudo_bytes(32));

$exception = $enumClass::customInvalidValueException($value);

$this->expectException(get_class($exception));
$this->expectExceptionMessage($exception->getMessage());

new $enumClass($value);
}

/**
* @test
*/
public function callInvalidStaticMethod()
{
/** @var Enum $enumClass */
$enumClass = $this->enumClass();
$method = md5(openssl_random_pseudo_bytes(32));

$exception = $enumClass::customUnknownStaticMethodException($method);

$this->expectException(get_class($exception));
$this->expectExceptionMessage($exception->getMessage());

call_user_func([$enumClass, $method]);
}

/**
* @test
*/
public function callInvalidMethod()
{
/** @var Enum $enumClass */
$enumClass = $this->enumClass();

$enum = $this->createValidEnum($enumClass);
$method = md5(openssl_random_pseudo_bytes(32));

$exception = $enumClass::customUnknownMethodException($method);

$this->expectException(get_class($exception));
$this->expectExceptionMessage($exception->getMessage());

$enum->$method();
}

/**
* @test
*/
Expand Down
64 changes: 62 additions & 2 deletions tests/Collection/BaseEntityCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function toArray($elements)
{
$collection = $this->buildCollection($elements);

$this->assertSame($elements, $collection->toArray());
$this->assertSame(array_values($elements), $collection->toArray());
}

/**
Expand Down Expand Up @@ -81,6 +81,7 @@ public function last($elements)
public function key($elements)
{
$collection = $this->buildCollection($elements);
$elements = array_values($elements);

$this->assertSame(key($elements), $collection->key());

Expand All @@ -97,6 +98,7 @@ public function key($elements)
public function next($elements)
{
$collection = $this->buildCollection($elements);
$elements = array_values($elements);

while (true) {
$collectionNext = $collection->next();
Expand Down Expand Up @@ -136,7 +138,7 @@ public function getKeys($elements)
{
$collection = $this->buildCollection($elements);

$this->assertSame(array_keys($elements), $collection->keys());
$this->assertSame(array_keys(array_values($elements)), $collection->keys());
}

/**
Expand Down Expand Up @@ -168,6 +170,7 @@ public function countElements($elements)
public function iterator($elements)
{
$collection = $this->buildCollection($elements);
$elements = array_values($elements);

$iterations = 0;
foreach ($collection as $key => $item) {
Expand All @@ -177,4 +180,61 @@ public function iterator($elements)

$this->assertEquals(count($elements), $iterations, 'Number of iterations not match');
}

/**
* @test
* @dataProvider provideDifferentElements
*/
public function slice($elements)
{
$collection = $this->buildCollection($elements);
$elements = array_values($elements);

$length = intval(ceil(count($elements)/2));
$sliceCollection = $collection->slice(0, $length);

$this->assertCount($length, $sliceCollection);
}

/**
* @test
* @dataProvider provideDifferentElements
*/
public function sliceWithOffset($elements)
{
$collection = $this->buildCollection($elements);
$elements = array_values($elements);

$length = intval(ceil(count($elements)/2));
$offset = intval(ceil(count($length)/2));
$sliceCollection = $collection->slice($offset, $length);

$this->assertCount($length, $sliceCollection);
}

/**
* @test
* @dataProvider provideDifferentElements
* @expectedException \LengthException
* @expectedExceptionMessage Collection can not be empty
*/
public function sliceEmpty($elements)
{
$collection = $this->buildCollection($elements);

$collection->slice(0, 0);
}

/**
* @test
* @dataProvider provideDifferentElements
* @expectedException \LengthException
* @expectedExceptionMessage Collection can not be empty
*/
public function sliceEmptyByOffset($elements)
{
$collection = $this->buildCollection($elements);

$collection->slice(count($elements), 0);
}
}
9 changes: 9 additions & 0 deletions tests/Collection/EwokCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@ public static function entityClass(): string
{
return Ewok::class;
}

/**
* @param $key
* @param $entity
*/
public function setEntity($key, $entity)
{
parent::set($key, $entity);
}
}
Loading

0 comments on commit 0598587

Please sign in to comment.