-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from atrapalo/StrictCollection
EntityCollection now can be empty and hte new EntityStrictCollection …
- Loading branch information
Showing
21 changed files
with
538 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.