Skip to content

Commit

Permalink
Merge pull request #4 from atrapalo/StrictCollection
Browse files Browse the repository at this point in the history
EntityCollection now can be empty and hte new EntityStrictCollection …
  • Loading branch information
GuilleGF authored Apr 24, 2017
2 parents fdb8ab5 + 827b42b commit a482766
Show file tree
Hide file tree
Showing 21 changed files with 538 additions and 164 deletions.
29 changes: 10 additions & 19 deletions src/Collection/EntityCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,9 @@ abstract public static function entityClass(): string;
* Collection constructor.
* @param array $entities
* @param bool $allowEntitiesChildren
* @throws \Exception
*/
public function __construct(array $entities, bool $allowEntitiesChildren = false)
public function __construct(array $entities = array(), bool $allowEntitiesChildren = false)
{
if (empty($entities)) {
throw static::customEmptyException();
}

$this->allowEntitiesChildren = $allowEntitiesChildren;

foreach ($entities as $entity) {
Expand Down Expand Up @@ -82,6 +77,14 @@ public function isValid($entity): bool
return false;
}

/**
* @return bool
*/
public function isEmpty(): bool
{
return $this->count() === 0;
}

/**
* @return array
*/
Expand Down Expand Up @@ -126,16 +129,12 @@ public function toArray(): array
* @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 new static($entities, $this->allowEntitiesChildren);
}

/**
Expand Down Expand Up @@ -183,14 +182,6 @@ public function count(): int
return count($this->entities);
}

/**
* @return \Exception
*/
public static function customEmptyException(): \Exception
{
return new \LengthException('Collection can not be empty');
}

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

namespace Atrapalo\PHPTools\Collection;

/**
* Class EntityStrictCollection
* @package Atrapalo\PHPTools\Collection
*
* @author Guillermo González <[email protected]>
*/
abstract class EntityStrictCollection extends EntityCollection
{
/**
* Collection constructor.
* @param array $entities
* @param bool $allowEntitiesChildren
* @throws \Exception
*/
public function __construct(array $entities, bool $allowEntitiesChildren = false)
{
if (empty($entities)) {
throw static::customEmptyException();
}

$this->allowEntitiesChildren = $allowEntitiesChildren;

foreach ($entities as $entity) {
$this->add($entity);
}
}

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

return new static($entities);
}

/**
* @return \Exception
*/
public static function customEmptyException(): \Exception
{
return new \LengthException('Collection can not be empty');
}
}
32 changes: 16 additions & 16 deletions src/Tester/Collection/EntityCollectionTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,6 @@ trait EntityCollectionTester
*/
abstract protected function entityCollectionClass(): string;

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

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

new $entityCollectionClass([]);
}

/**
* @test
* @dataProvider invalidEntities
Expand Down Expand Up @@ -64,6 +49,21 @@ public function invalidEntities()
];
}

/**
* @test
*/
public function emptyConstructor()
{
/** @var EntityCollection $entityCollectionClass */
$entityCollectionClass = $this->entityCollectionClass();

/** @var EntityCollection $collection */
$collection = new $entityCollectionClass();

$this->assertSame(0, $collection->count());
$this->assertTrue($collection->isEmpty());
}

/**
* @test
*/
Expand All @@ -77,7 +77,7 @@ public function invalidAddElement()
$this->expectExceptionMessage($exception->getMessage());

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

Expand Down
136 changes: 136 additions & 0 deletions src/Tester/Collection/EntityStrictCollectionTester.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

namespace Atrapalo\PHPTools\Tester\Collection;

use Atrapalo\PHPTools\Collection\EntityStrictCollection;

/**
* Class EntityCollectionTester
* @package Atrapalo\PHPTools\Collection
*
* @author Guillermo González <[email protected]>
*/
trait EntityStrictCollectionTester
{
/**
* @return string
*/
abstract protected function entityCollectionClass(): string;

/**
* @test
*/
public function emptyConstructor()
{
/** @var EntityStrictCollection $entityCollectionClass */
$entityCollectionClass = $this->entityCollectionClass();
$exception = $entityCollectionClass::customEmptyException();

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

new $entityCollectionClass([]);
}

/**
* @test
* @dataProvider invalidEntities
* @param array $elements
*/
public function invalidElementByConstructor(array $elements)
{
/** @var EntityStrictCollection $entityCollectionClass */
$entityCollectionClass = $this->entityCollectionClass();
$exception = $entityCollectionClass::customInvalidEntityException();

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

new $entityCollectionClass($elements);
}

/**
* @return array
*/
public function invalidEntities()
{
return [
[[1]],
[['element']],
[[new \stdClass()]],
[[new \stdClass(), new \stdClass()]],
[[null]],
[[false]]
];
}

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

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

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

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

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

new $entityCollectionClass([$this->prophesize($entityCollectionClass::entityClass())->reveal()]);
}

/**
* @test
*/
public function validElementByConstructor()
{
/** @var EntityStrictCollection $entityCollectionClass */
$entityCollectionClass = $this->entityCollectionClass();

/** @var EntityStrictCollection $collection */
$collection = new $entityCollectionClass([$this->prophesize($entityCollectionClass::entityClass())->reveal()], true);

$this->assertSame(1, $collection->count());
$this->assertInstanceOf($entityCollectionClass::entityClass(), $collection->current());
}

/**
* @test
*/
public function addTwoValidElement()
{
/** @var EntityStrictCollection $entityCollectionClass */
$entityCollectionClass = $this->entityCollectionClass();

/** @var EntityStrictCollection $collection */
$collection = new $entityCollectionClass([$this->prophesize($entityCollectionClass::entityClass())->reveal()], true);


/** @var \Prophecy\Prophecy\ObjectProphecy $entity */
$entity = $this->prophesize($entityCollectionClass::entityClass());
$collection->add($entity->reveal());
$collection->add($entity->reveal());

$this->assertSame(3, $collection->count());
foreach ($collection as $element) {
$this->assertInstanceOf($entityCollectionClass::entityClass(), $element);
}
}
}
Loading

0 comments on commit a482766

Please sign in to comment.