Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
23 / 23 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| EntityWithPropertyFactory | |
100.00% |
23 / 23 |
|
100.00% |
1 / 1 |
4 | |
100.00% |
1 / 1 |
| __construct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| createEntityWithProperty | |
100.00% |
23 / 23 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | namespace Apie\Fixtures\TestHelpers; |
| 3 | |
| 4 | use Apie\Core\Attributes\OverwriteAfterPersist; |
| 5 | use Apie\Core\Entities\EntityInterface; |
| 6 | use Apie\Core\Identifiers\IdentifierInterface; |
| 7 | use Apie\Core\Identifiers\Ulid; |
| 8 | use ReflectionClass; |
| 9 | |
| 10 | class EntityWithPropertyFactory |
| 11 | { |
| 12 | private const NAMESPACE = 'FixtureTest'; |
| 13 | |
| 14 | /** |
| 15 | * @codeCoverageIgnore |
| 16 | */ |
| 17 | private function __construct() |
| 18 | { |
| 19 | } |
| 20 | /** |
| 21 | * @return ReflectionClass<EntityInterface> |
| 22 | */ |
| 23 | public static function createEntityWithProperty(string $property): ReflectionClass |
| 24 | { |
| 25 | $className = 'Entity' . md5($property); |
| 26 | if (!class_exists($className)) { |
| 27 | $code = ( |
| 28 | "<?php |
| 29 | namespace " . self::NAMESPACE . "; |
| 30 | |
| 31 | use " . EntityInterface::class . "; |
| 32 | use " . IdentifierInterface::class . "; |
| 33 | use " . OverwriteAfterPersist::class ."; |
| 34 | use " . Ulid::class . "; |
| 35 | use ReflectionClass; |
| 36 | |
| 37 | class " . $className . "Identifier extends Ulid implements IdentifierInterface |
| 38 | { |
| 39 | public static function getReferenceFor(): ReflectionClass |
| 40 | { |
| 41 | return new ReflectionClass(" . $className . "::class); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | #[OverwriteAfterPersist] |
| 46 | class " . $className . " implements EntityInterface |
| 47 | { |
| 48 | private ". $className . "Identifier \$id; |
| 49 | public function __construct( |
| 50 | public \\" . $property . " \$property |
| 51 | ) { |
| 52 | \$this->id = " . $className . "Identifier::createRandom(); |
| 53 | } |
| 54 | |
| 55 | public function getId(): " . $className . "Identifier |
| 56 | { |
| 57 | return \$this->id; |
| 58 | } |
| 59 | } |
| 60 | " |
| 61 | ); |
| 62 | $tmpPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'code' . md5($code) . '.php'; |
| 63 | if (!file_exists($tmpPath)) { |
| 64 | file_put_contents($tmpPath, $code); |
| 65 | } |
| 66 | require_once($tmpPath); |
| 67 | } |
| 68 | |
| 69 | return new \ReflectionClass(self::NAMESPACE . '\\' . $className); |
| 70 | } |
| 71 | } |