-
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 #3 from atrapalo/UniqueEntityCollection
Unique entity collection
- Loading branch information
Showing
15 changed files
with
450 additions
and
88 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
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,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'); | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
<?php | ||
|
||
namespace Atrapalo\PHPTools\Enum; | ||
namespace Atrapalo\PHPTools\Tester\Enum; | ||
|
||
use Atrapalo\PHPTools\Enum\Enum; | ||
use Atrapalo\PHPTools\Parser\PHPDocClass; | ||
|
||
/** | ||
|
@@ -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 | ||
*/ | ||
|
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
Oops, something went wrong.