Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.32% |
56 / 62 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| TestWithDoctrineEntityDatalayer | |
90.32% |
56 / 62 |
|
0.00% |
0 / 2 |
10.09 | |
0.00% |
0 / 1 |
| className | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| createExampleObject | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
3.07 | |||
| it_can_be_stored_in_a_property_with_doctrine_entity_datalayer | |
91.23% |
52 / 57 |
|
0.00% |
0 / 1 |
7.03 | |||
| 1 | <?php |
| 2 | namespace Apie\Fixtures\TestHelpers; |
| 3 | |
| 4 | use Apie\Core\BoundedContext\BoundedContext; |
| 5 | use Apie\Core\BoundedContext\BoundedContextHashmap; |
| 6 | use Apie\Core\FileStorage\FileStorageFactory; |
| 7 | use Apie\Core\Indexing\Indexer; |
| 8 | use Apie\Core\Lists\ReflectionClassList; |
| 9 | use Apie\Core\Lists\ReflectionMethodList; |
| 10 | use Apie\Core\ValueObjects\ValueObjectInterface; |
| 11 | use Apie\DoctrineEntityConverter\Factories\PersistenceLayerFactory; |
| 12 | use Apie\DoctrineEntityConverter\OrmBuilder as DoctrineEntityConverterOrmBuilder; |
| 13 | use Apie\DoctrineEntityDatalayer\DoctrineEntityDatalayer; |
| 14 | use Apie\DoctrineEntityDatalayer\EntityReindexer; |
| 15 | use Apie\DoctrineEntityDatalayer\Factories\DoctrineListFactory; |
| 16 | use Apie\DoctrineEntityDatalayer\Factories\EntityQueryFilterFactory; |
| 17 | use Apie\DoctrineEntityDatalayer\IndexStrategy\DirectIndexStrategy; |
| 18 | use Apie\DoctrineEntityDatalayer\OrmBuilder; |
| 19 | use Apie\Faker\ApieObjectFaker; |
| 20 | use Apie\Fixtures\Attributes\DisableDatalayerTest; |
| 21 | use Apie\StorageMetadata\DomainToStorageConverter; |
| 22 | use Faker\Factory; |
| 23 | use PHPUnit\Framework\Attributes\Test; |
| 24 | use PHPUnit\Framework\TestCase; |
| 25 | |
| 26 | /** |
| 27 | * @mixin TestCase |
| 28 | */ |
| 29 | trait TestWithDoctrineEntityDatalayer |
| 30 | { |
| 31 | /** |
| 32 | * @return class-string<ValueObjectInterface> |
| 33 | */ |
| 34 | abstract public static function className(): string; |
| 35 | |
| 36 | public static function createExampleObject(): object |
| 37 | { |
| 38 | if (!class_exists(Factory::class) || !class_exists(ApieObjectFaker::class)) { |
| 39 | throw new \LogicException('The default createExampleObject() requires apie/faker to be available!'); |
| 40 | } |
| 41 | $generator = Factory::create(); |
| 42 | $generator->addProvider(ApieObjectFaker::createWithDefaultFakers($generator)); |
| 43 | return $generator->fakeClass(static::className()); |
| 44 | } |
| 45 | |
| 46 | #[Test] |
| 47 | public function it_can_be_stored_in_a_property_with_doctrine_entity_datalayer() |
| 48 | { |
| 49 | foreach ((new \ReflectionClass(static::class))->getAttributes() as $attribute) { |
| 50 | if ($attribute->getName() === DisableDatalayerTest::class) { |
| 51 | $this->markTestSkipped('DoctrineEntityDatalayer test disabled for this class'); |
| 52 | } |
| 53 | } |
| 54 | if (!class_exists(DoctrineEntityDatalayer::class)) { |
| 55 | $this->markTestSkipped('DoctrineEntityDatalayer class not loaded'); |
| 56 | } |
| 57 | $className = static::className(); |
| 58 | $entityClass = EntityWithPropertyFactory::createEntityWithProperty($className); |
| 59 | $tempFolder = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('doctrine-'); |
| 60 | if (!@mkdir($tempFolder)) { |
| 61 | $this->markTestSkipped('Can not create temp folder ' . $tempFolder); |
| 62 | } |
| 63 | try { |
| 64 | $entityPath = $tempFolder . DIRECTORY_SEPARATOR . 'entities'; |
| 65 | if (!@mkdir($entityPath)) { |
| 66 | $this->markTestSkipped('Can not create entity folder ' . $entityPath); |
| 67 | } |
| 68 | $proxyPath = $tempFolder . DIRECTORY_SEPARATOR . 'proxies'; |
| 69 | if (!@mkdir($proxyPath)) { |
| 70 | $this->markTestSkipped('Can not create proxy folder ' . $proxyPath); |
| 71 | } |
| 72 | $ormBuilder = new DoctrineEntityConverterOrmBuilder( |
| 73 | new PersistenceLayerFactory(), |
| 74 | new BoundedContextHashmap([ |
| 75 | 'test' => new BoundedContext( |
| 76 | 'test', |
| 77 | new ReflectionClassList([$entityClass]), |
| 78 | new ReflectionMethodList() |
| 79 | ) |
| 80 | ]), |
| 81 | true |
| 82 | ); |
| 83 | $ormBuilder = new OrmBuilder( |
| 84 | $ormBuilder, |
| 85 | buildOnce: false, |
| 86 | runMigrations: true, |
| 87 | devMode: true, |
| 88 | proxyDir: $proxyPath, |
| 89 | cache: null, |
| 90 | path: $entityPath, |
| 91 | connectionConfig: [ |
| 92 | 'driver' => 'pdo_sqlite', |
| 93 | 'memory' => true |
| 94 | ] |
| 95 | ); |
| 96 | $domainToStorageConverter = DomainToStorageConverter::create(FileStorageFactory::create()); |
| 97 | $doctrineListFactory = new DoctrineListFactory( |
| 98 | $ormBuilder, |
| 99 | new EntityQueryFilterFactory(), |
| 100 | $domainToStorageConverter |
| 101 | ); |
| 102 | $testItem = new DoctrineEntityDatalayer( |
| 103 | $ormBuilder, |
| 104 | $domainToStorageConverter, |
| 105 | new DirectIndexStrategy(new EntityReindexer($ormBuilder, Indexer::create())), |
| 106 | $doctrineListFactory |
| 107 | ); |
| 108 | $entity = $entityClass->newInstance($this->createExampleObject()); |
| 109 | $idBeforePersist = $entity->getId(); |
| 110 | $actual = $testItem->persistNew($entity); |
| 111 | $this->assertEquals($idBeforePersist, $actual->getId()->toNative()); |
| 112 | } finally { |
| 113 | system('rm -rf ' . escapeshellarg($tempFolder)); |
| 114 | } |
| 115 | } |
| 116 | } |