diff --git a/src/Command/GenerateSchema.php b/src/Command/GenerateSchema.php index c4772fc..e268e7e 100644 --- a/src/Command/GenerateSchema.php +++ b/src/Command/GenerateSchema.php @@ -7,7 +7,7 @@ use Talleu\RedisOm\Om\Mapping\Entity; use Talleu\RedisOm\Om\RedisFormat; -class GenerateSchema +final class GenerateSchema { public static function generateSchema(string $dir): void { diff --git a/src/Om/Metadata/ClassMetadata.php b/src/Om/Metadata/ClassMetadata.php new file mode 100644 index 0000000..c31145d --- /dev/null +++ b/src/Om/Metadata/ClassMetadata.php @@ -0,0 +1,137 @@ +className; + } + + public function getIdentifier() + { + return $this->identifier; + } + + public function getReflectionClass() + { + return new \ReflectionClass($this->className); + } + + public function isIdentifier(string $fieldName) + { + foreach ($this->identifier as $identifier) { + if ($identifier === $fieldName) { + return true; + } + } + + return false; + } + + public function hasField(string $fieldName) + { + foreach ($this->fieldsMapping as $field) { + if ($field === $fieldName) { + return true; + } + } + + return false; + } + + public function hasAssociation(string $fieldName) + { + foreach ($this->associations as $association) { + if ($association === $fieldName) { + return true; + } + } + + return false; + } + + public function isSingleValuedAssociation(string $fieldName) + { + foreach ($this->associations as $association) { + if ($association === $fieldName) { + return false; + } + } + + return true; + } + + public function isCollectionValuedAssociation(string $fieldName) + { + return false; + } + + public function getFieldNames() + { + return array_values($this->fieldsMapping); + } + + public function getIdentifierFieldNames() + { + return array_values($this->identifier); + } + + public function getAssociationNames() + { + return array_values($this->associations); + } + + public function getTypeOfField(string $fieldName): ?string + { + foreach ($this->typesFields as $field => $type) { + if ($field === $fieldName) { + return $type; + } + } + + return null; + } + + public function isAssociationInverseSide(string $assocName) + { + return false; + } + + public function getIdentifierValues(object $object) + { + return array_values($this->identifier); + } + + public function setIdentifier(array $identifier) + { + $this->identifier = $identifier; + } + + public function setFieldsMapping(array $fields) + { + $this->fieldsMapping = $fields; + } + + public function setAssociations(array $associations) + { + $this->associations = $associations; + } + + public function setTypesFields(array $fields) + { + $this->typesFields = $fields; + } +} diff --git a/src/Om/Metadata/MetadataFactory.php b/src/Om/Metadata/MetadataFactory.php new file mode 100644 index 0000000..8c8fcf0 --- /dev/null +++ b/src/Om/Metadata/MetadataFactory.php @@ -0,0 +1,88 @@ +setIdentifier($this->buildIdentifier($className)); + $classMetadata->setFieldsMapping($this->buildFieldsMapping()); + $classMetadata->setAssociations($this->buildAssociations()); + $classMetadata->setTypesFields($this->buildTypesFields()); + + return $classMetadata; + } + + private function buildIdentifier(string $className): array + { + $this->reflectionClass = new \ReflectionClass($className); + $properties = $this->reflectionClass->getProperties(); + $identifier = []; + foreach ($properties as $property) { + $attributeId = $property->getAttributes(Id::class); + if ($attributeId !== []) { + $identifier[] = $property->getName(); + } + } + + return $identifier; + } + + private function buildFieldsMapping(): array + { + $reflectionProperties = $this->reflectionClass->getProperties(); + $properties = []; + foreach ($reflectionProperties as $property) { + $attributeProperty = $property->getAttributes(Property::class); + if ($attributeProperty !== []) { + $properties[] = $property->getName(); + } + } + + return $properties; + } + + private function buildAssociations(): array + { + $associations = []; + $properties = $this->reflectionClass->getProperties(); + foreach ($properties as $property) { + $attributes = $property->getAttributes(Property::class); + if ($attributes === []) { + continue; + } + + $associationClassName = $attributes[0]->getName(); + $reflectionAssociation = new \ReflectionClass($associationClassName); + $attributesMapping = $reflectionAssociation->getAttributes(Entity::class); + if ($attributesMapping !== []) { + $associations[] = $property->getName(); + } + } + + return $associations; + } + + private function buildTypesFields(): array + { + $fields = []; + $properties = $this->reflectionClass->getProperties(); + foreach ($properties as $property) { + /** @var \ReflectionNamedType|null $propertyType */ + $propertyType = $property->getType(); + $fields[$property->getName()] = $propertyType?->getName(); + } + + return $fields; + } +} diff --git a/src/Om/Metadata/MetadataInterface.php b/src/Om/Metadata/MetadataInterface.php new file mode 100644 index 0000000..b57a0c2 --- /dev/null +++ b/src/Om/Metadata/MetadataInterface.php @@ -0,0 +1,36 @@ +repository; } - public function getClassMetadata(string $className) + public function getClassMetadata(string $className): ClassMetadata { - // TODO: Implement getClassMetadata() method. + return (new MetadataFactory())->createClassMetadata($className); } public function getMetadataFactory() { - // TODO: Implement getMetadataFactory() method. + return new MetadataFactory(); } public function initializeObject(object $obj) { - // TODO: Implement initializeObject() method. + return new $obj(); } public function contains(object $object): bool diff --git a/tests/Fixtures/FixturesGenerator.php b/tests/Fixtures/FixturesGenerator.php index 75c8a60..45fff40 100644 --- a/tests/Fixtures/FixturesGenerator.php +++ b/tests/Fixtures/FixturesGenerator.php @@ -4,15 +4,10 @@ namespace Talleu\RedisOm\Tests\Fixtures; -use Talleu\RedisOm\Tests\Fixtures\Hash\DummyHash; -use Talleu\RedisOm\Tests\Fixtures\Json\DummyJson; - class FixturesGenerator { - public static function generateDummies(string $format): array + public static function generateDummies($dummyClass): array { - $dummyClass = $format === 'JSON' ? DummyJson::class : DummyHash::class; - $dummy = $dummyClass::create( id: 1, age: 20, diff --git a/tests/Fixtures/Hash/PrefixDummyHash.php b/tests/Fixtures/Hash/PrefixDummyHash.php new file mode 100644 index 0000000..35d453a --- /dev/null +++ b/tests/Fixtures/Hash/PrefixDummyHash.php @@ -0,0 +1,13 @@ +value)] +class PrefixDummyJson extends AbstractDummy +{ +} diff --git a/tests/Functionnal/Om/Mapping/PrefixTest.php b/tests/Functionnal/Om/Mapping/PrefixTest.php new file mode 100644 index 0000000..70ee459 --- /dev/null +++ b/tests/Functionnal/Om/Mapping/PrefixTest.php @@ -0,0 +1,49 @@ +createClient()->keys('*'); + foreach ($keys as $key) { + $this->assertStringContainsString('dummy:', $key); + } + + $this->assertInstanceOf(PrefixDummyHash::class, $objectManager->find(PrefixDummyHash::class, 1)); + $this->assertInstanceOf(PrefixDummyHash::class, $objectManager->find(PrefixDummyHash::class, 2)); + $this->assertInstanceOf(PrefixDummyHash::class, $objectManager->find(PrefixDummyHash::class, 3)); + } + + public function testPrefixJson() + { + static::emptyRedis(); + static::generateIndex(); + static::loadRedisFixtures(PrefixDummyJson::class); + + + $objectManager = new RedisObjectManager(); + $keys = $this->createClient()->keys('*'); + foreach ($keys as $key) { + $this->assertStringContainsString('dummy:', $key); + } + + $this->assertInstanceOf(PrefixDummyJson::class, $objectManager->find(PrefixDummyJson::class, 1)); + $this->assertInstanceOf(PrefixDummyJson::class, $objectManager->find(PrefixDummyJson::class, 2)); + $this->assertInstanceOf(PrefixDummyJson::class, $objectManager->find(PrefixDummyJson::class, 3)); + } +} diff --git a/tests/Functionnal/Om/ObjectManagerTest.php b/tests/Functionnal/Om/ObjectManagerTest.php index 1b4e389..d12dfdb 100644 --- a/tests/Functionnal/Om/ObjectManagerTest.php +++ b/tests/Functionnal/Om/ObjectManagerTest.php @@ -19,7 +19,7 @@ public function testPersistAndFlushHash(): void { static::emptyRedis(); static::generateIndex(); - static::loadRedisFixtures(RedisFormat::HASH->value); + static::loadRedisFixtures(); $keys = $this->createClient()->keys('*'); $classNameConverted = RedisClient::convertPrefix(DummyHash::class); @@ -32,7 +32,7 @@ public function testPersistAndFlushJson(): void { static::emptyRedis(); static::generateIndex(); - static::loadRedisFixtures(RedisFormat::JSON->value); + static::loadRedisFixtures(DummyJson::class); $keys = $this->createClient()->keys('*'); $classNameConverted = RedisClient::convertPrefix(DummyJson::class); @@ -45,7 +45,7 @@ public function testFindJson() { static::emptyRedis(); static::generateIndex(); - $dummies = static::loadRedisFixtures(RedisFormat::JSON->value); + $dummies = static::loadRedisFixtures(DummyJson::class); $objectManager = new RedisObjectManager(); /** @var DummyJson $object */ @@ -66,7 +66,7 @@ public function testFindHash() { static::emptyRedis(); static::generateIndex(); - $dummies = static::loadRedisFixtures(RedisFormat::HASH->value); + $dummies = static::loadRedisFixtures(); $objectManager = new RedisObjectManager(); @@ -87,7 +87,7 @@ public function testRemove() { static::emptyRedis(); static::generateIndex(); - $dummies = static::loadRedisFixtures(RedisFormat::HASH->value); + $dummies = static::loadRedisFixtures(); /** @var DummyHash $object */ $object = $dummies[0]; @@ -119,7 +119,7 @@ public function testClear() { static::emptyRedis(); static::generateIndex(); - $dummies = static::loadRedisFixtures(RedisFormat::HASH->value, false); + $dummies = static::loadRedisFixtures(flush: false); $objectManager = new RedisObjectManager(); foreach ($dummies as $dummy) { $objectManager->persist($dummy); @@ -134,7 +134,7 @@ public function testDetach() { static::emptyRedis(); static::generateIndex(); - $dummies = static::loadRedisFixtures(RedisFormat::HASH->value, false); + $dummies = static::loadRedisFixtures(flush: false); $objectManager = new RedisObjectManager(); foreach ($dummies as $dummy) { $objectManager->persist($dummy); diff --git a/tests/Functionnal/Om/Repository/HashModel/HashRepositoryTest.php b/tests/Functionnal/Om/Repository/HashModel/HashRepositoryTest.php index 8304023..30b703f 100644 --- a/tests/Functionnal/Om/Repository/HashModel/HashRepositoryTest.php +++ b/tests/Functionnal/Om/Repository/HashModel/HashRepositoryTest.php @@ -15,7 +15,7 @@ public function testFindAll() { static::emptyRedis(); static::generateIndex(); - static::loadRedisFixtures(RedisFormat::HASH->value); + static::loadRedisFixtures(); $objectManager = new RedisObjectManager(); $repository = $objectManager->getRepository(DummyHash::class); @@ -31,7 +31,7 @@ public function testFindBy() { static::emptyRedis(); static::generateIndex(); - static::loadRedisFixtures(RedisFormat::HASH->value); + static::loadRedisFixtures(); $objectManager = new RedisObjectManager(); $repository = $objectManager->getRepository(DummyHash::class); @@ -49,7 +49,7 @@ public function testFindByOrder() { static::emptyRedis(); static::generateIndex(); - static::loadRedisFixtures(RedisFormat::HASH->value); + static::loadRedisFixtures(); $objectManager = new RedisObjectManager(); $repository = $objectManager->getRepository(DummyHash::class); @@ -79,7 +79,7 @@ public function testFindByMultiCriterias() { static::emptyRedis(); static::generateIndex(); - static::loadRedisFixtures(RedisFormat::HASH->value); + static::loadRedisFixtures(); $objectManager = new RedisObjectManager(); $repository = $objectManager->getRepository(DummyHash::class); @@ -98,7 +98,7 @@ public function testFindOneBy() { static::emptyRedis(); static::generateIndex(); - static::loadRedisFixtures(RedisFormat::HASH->value); + static::loadRedisFixtures(); $objectManager = new RedisObjectManager(); $repository = $objectManager->getRepository(DummyHash::class); diff --git a/tests/Functionnal/Om/Repository/JsonModel/JsonRepositoryTest.php b/tests/Functionnal/Om/Repository/JsonModel/JsonRepositoryTest.php index e2a0ff0..ce2a243 100644 --- a/tests/Functionnal/Om/Repository/JsonModel/JsonRepositoryTest.php +++ b/tests/Functionnal/Om/Repository/JsonModel/JsonRepositoryTest.php @@ -15,7 +15,7 @@ public function testFindAll() { static::emptyRedis(); static::generateIndex(); - static::loadRedisFixtures(RedisFormat::JSON->value); + static::loadRedisFixtures(DummyJson::class); $objectManager = new RedisObjectManager(); $repository = $objectManager->getRepository(DummyJson::class); @@ -31,7 +31,7 @@ public function testFindBy() { static::emptyRedis(); static::generateIndex(); - static::loadRedisFixtures(RedisFormat::JSON->value); + static::loadRedisFixtures(DummyJson::class); $objectManager = new RedisObjectManager(); $repository = $objectManager->getRepository(DummyJson::class); @@ -49,7 +49,7 @@ public function testFindByOrder() { static::emptyRedis(); static::generateIndex(); - static::loadRedisFixtures(RedisFormat::JSON->value); + static::loadRedisFixtures(DummyJson::class); $objectManager = new RedisObjectManager(); $repository = $objectManager->getRepository(DummyJson::class); @@ -81,7 +81,7 @@ public function testFindByMultiCriterias() { static::emptyRedis(); static::generateIndex(); - static::loadRedisFixtures(RedisFormat::JSON->value); + static::loadRedisFixtures(DummyJson::class); $objectManager = new RedisObjectManager(); $repository = $objectManager->getRepository(DummyJson::class); @@ -100,7 +100,7 @@ public function testFindOneBy() { static::emptyRedis(); static::generateIndex(); - static::loadRedisFixtures(RedisFormat::JSON->value); + static::loadRedisFixtures(DummyJson::class); $objectManager = new RedisObjectManager(); $repository = $objectManager->getRepository(DummyJson::class); diff --git a/tests/Metadata/MetadataTest.php b/tests/Metadata/MetadataTest.php new file mode 100644 index 0000000..95478c6 --- /dev/null +++ b/tests/Metadata/MetadataTest.php @@ -0,0 +1,22 @@ +getClassMetadata(DummyHash::class); + + $this->assertEquals(['id'], $classMetadata->getIdentifier()); + $this->assertCount(11, $classMetadata->fieldsMapping); + $this->assertCount(11, $classMetadata->typesFields); + } +} diff --git a/tests/RedisAbstractTestCase.php b/tests/RedisAbstractTestCase.php index 04ca332..ad2ef2c 100644 --- a/tests/RedisAbstractTestCase.php +++ b/tests/RedisAbstractTestCase.php @@ -9,6 +9,7 @@ use Talleu\RedisOm\Om\RedisObjectManager; use Talleu\RedisOm\Tests\Client\Client; use Talleu\RedisOm\Tests\Fixtures\FixturesGenerator; +use Talleu\RedisOm\Tests\Fixtures\Hash\DummyHash; class RedisAbstractTestCase extends TestCase { @@ -27,10 +28,10 @@ public static function generateIndex(): void Runner::generateSchema('tests'); } - public static function loadRedisFixtures(string $format, ?bool $flush = true): array + public static function loadRedisFixtures(?string $dummyClass = DummyHash::class, ?bool $flush = true): array { $objectManager = new RedisObjectManager(); - $dummies = FixturesGenerator::generateDummies($format); + $dummies = FixturesGenerator::generateDummies($dummyClass); foreach ($dummies as $dummy) { $objectManager->persist($dummy); }