From e5ff5b85b2200866a24f2c73c586306d6484875d Mon Sep 17 00:00:00 2001 From: Niels Keurentjes Date: Sat, 21 Dec 2024 23:09:56 +0100 Subject: [PATCH] Add tests for referenceColumnName defaulting --- .../Tests/ORM/Mapping/AttributeDriverTest.php | 1 + .../ORM/Mapping/MappingDriverTestCase.php | 57 ++++++++++ .../CustomPascalNamingStrategy.php | 101 ++++++++++++++++++ .../Tests/ORM/Mapping/NamingStrategyTest.php | 17 +++ ...octrine.Tests.ORM.Mapping.BlogPost.dcm.xml | 16 +++ ....Tests.ORM.Mapping.BlogPostComment.dcm.xml | 22 ++++ 6 files changed, 214 insertions(+) create mode 100644 tests/Tests/ORM/Mapping/NamingStrategy/CustomPascalNamingStrategy.php create mode 100644 tests/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.BlogPost.dcm.xml create mode 100644 tests/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.BlogPostComment.dcm.xml diff --git a/tests/Tests/ORM/Mapping/AttributeDriverTest.php b/tests/Tests/ORM/Mapping/AttributeDriverTest.php index b16ec788d33..0681d10ae5e 100644 --- a/tests/Tests/ORM/Mapping/AttributeDriverTest.php +++ b/tests/Tests/ORM/Mapping/AttributeDriverTest.php @@ -131,3 +131,4 @@ class AttributeTransientAttribute implements MappingAttribute class AttributeTransientClass { } + diff --git a/tests/Tests/ORM/Mapping/MappingDriverTestCase.php b/tests/Tests/ORM/Mapping/MappingDriverTestCase.php index 10d9d4ba965..1f91a02de89 100644 --- a/tests/Tests/ORM/Mapping/MappingDriverTestCase.php +++ b/tests/Tests/ORM/Mapping/MappingDriverTestCase.php @@ -62,6 +62,7 @@ use Doctrine\Tests\Models\TypedProperties\UserTypedWithCustomTypedField; use Doctrine\Tests\Models\Upsertable\Insertable; use Doctrine\Tests\Models\Upsertable\Updatable; +use Doctrine\Tests\ORM\Mapping\NamingStrategy\CustomPascalNamingStrategy; use Doctrine\Tests\OrmTestCase; use PHPUnit\Framework\Attributes\Depends; use stdClass; @@ -946,6 +947,16 @@ public function testEnumType(): void self::assertEquals(Suit::class, $metadata->fieldMappings['suit']->enumType); } + + public function testCustomNamingStrategyIsRespected(): void + { + $ns = new CustomPascalNamingStrategy(); + $metadata = $this->createClassMetadata(BlogPostComment::class, $ns); + + self::assertEquals('id', $metadata->fieldNames['Id']); + self::assertEquals('Id', $metadata->associationMappings['blogPost']->joinColumns[0]->referencedColumnName); + self::assertFalse($metadata->associationMappings['blogPost']->joinColumns[0]->nullable); + } } #[ORM\Entity()] @@ -1547,3 +1558,49 @@ public static function loadMetadata(ClassMetadata $metadata): void class GH10288EnumTypeBoss extends GH10288EnumTypePerson { } + +/** + * Two small related entities to test default namings with barebone attributes + */ +#[Entity] +class BlogPost +{ + #[Id, Column, GeneratedValue(strategy: 'NONE')] + public int $id; +} + +#[Entity] +class BlogPostComment +{ + #[Id, Column, GeneratedValue(strategy: 'AUTO')] + public int $id; + + #[ORM\ManyToOne, ORM\JoinColumn(nullable: false)] + public BlogPost $blogPost; + + public static function loadMetadata(ClassMetadata $metadata): void + { + $metadata->mapField( + [ + 'id' => true, + 'fieldName' => 'id', + 'type' => 'integer', + ], + ); + $metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO); + + $metadata->mapManyToOne( + [ + 'fieldName' => 'blogPost', + 'targetEntity' => BlogPost::class, + 'joinColumns' => + [ + 0 => + [ + 'nullable' => false, + ], + ], + ] + ); + } +} diff --git a/tests/Tests/ORM/Mapping/NamingStrategy/CustomPascalNamingStrategy.php b/tests/Tests/ORM/Mapping/NamingStrategy/CustomPascalNamingStrategy.php new file mode 100644 index 00000000000..104665aaa4e --- /dev/null +++ b/tests/Tests/ORM/Mapping/NamingStrategy/CustomPascalNamingStrategy.php @@ -0,0 +1,101 @@ +classToTableName($className)) . 'id') { + return 'Id'; + } + + return ucfirst($propertyName); + } + + /** + * Returns a column name for an embedded property. + */ + public function embeddedFieldToColumnName(string $propertyName, string $embeddedColumnName, ?string $className = null, $embeddedClassName = null): string + { + throw new \LogicException(sprintf('Method %s is not implemented', __METHOD__)); + } + + /** + * Returns the default reference column name. + * + * @return string A column name + */ + public function referenceColumnName(): string + { + return 'Id'; + } + + /** + * Returns a join column name for a property. + * + * @return string A join column name + */ + public function joinColumnName(string $propertyName, string $className): string + { + return ucfirst($propertyName) . $this->referenceColumnName(); + } + + /** + * Returns a join table name. + * + * @param string $sourceEntity The source entity + * @param string $targetEntity The target entity + * @param string|null $propertyName A property name + * + * @return string A join table name + */ + public function joinTableName(string $sourceEntity, string $targetEntity, ?string $propertyName = null): string + { + return $this->classToTableName($sourceEntity) . $this->classToTableName($targetEntity); + } + + /** + * Returns the foreign key column name for the given parameters. + * + * @param string $entityName An entity + * @param string|null $referencedColumnName A property + * + * @return string A join column name + */ + public function joinKeyColumnName(string $entityName, ?string $referencedColumnName = null): string + { + return $this->classToTableName($entityName) . ($referencedColumnName ?: $this->referenceColumnName()); + } +} \ No newline at end of file diff --git a/tests/Tests/ORM/Mapping/NamingStrategyTest.php b/tests/Tests/ORM/Mapping/NamingStrategyTest.php index ecade647392..18ee29fbe4c 100644 --- a/tests/Tests/ORM/Mapping/NamingStrategyTest.php +++ b/tests/Tests/ORM/Mapping/NamingStrategyTest.php @@ -7,6 +7,7 @@ use Doctrine\ORM\Mapping\DefaultNamingStrategy; use Doctrine\ORM\Mapping\NamingStrategy; use Doctrine\ORM\Mapping\UnderscoreNamingStrategy; +use Doctrine\Tests\ORM\Mapping\NamingStrategy\CustomPascalNamingStrategy; use Doctrine\Tests\ORM\Mapping\NamingStrategy\JoinColumnClassNamingStrategy; use Doctrine\Tests\OrmTestCase; use PHPUnit\Framework\Attributes\DataProvider; @@ -33,6 +34,11 @@ private static function underscoreNamingUpper(): UnderscoreNamingStrategy return new UnderscoreNamingStrategy(CASE_UPPER); } + private static function customNaming(): CustomPascalNamingStrategy + { + return new CustomPascalNamingStrategy(); + } + /** * Data Provider for NamingStrategy#classToTableName * @@ -56,6 +62,10 @@ public static function dataClassToTableName(): array [self::underscoreNamingUpper(), 'NAME', '\Some\Class\Name'], [self::underscoreNamingUpper(), 'NAME2_TEST', '\Some\Class\Name2Test'], [self::underscoreNamingUpper(), 'NAME2TEST', '\Some\Class\Name2test'], + + // CustomPascalNamingStrategy + [self::customNaming(), 'SomeClassName', 'SomeClassName'], + [self::customNaming(), 'Name2Test', '\Some\Class\Name2Test'], ]; } @@ -89,6 +99,10 @@ public static function dataPropertyToColumnName(): array [self::underscoreNamingUpper(), 'SOME_PROPERTY', 'SOME_PROPERTY', 'Some\Class'], [self::underscoreNamingUpper(), 'BASE64_ENCODED', 'base64Encoded', 'Some\Class'], [self::underscoreNamingUpper(), 'BASE64ENCODED', 'base64encoded', 'Some\Class'], + + // CustomPascalNamingStrategy + [self::customNaming(), 'SomeProperty', 'someProperty', 'Some\Class'], + [self::customNaming(), 'Base64Encoded', 'base64Encoded', 'Some\Class'], ]; } @@ -116,6 +130,9 @@ public static function dataReferenceColumnName(): array // UnderscoreNamingStrategy [self::underscoreNamingLower(), 'id'], [self::underscoreNamingUpper(), 'ID'], + + // CustomPascalNamingStrategy + [self::customNaming(), 'Id'], ]; } diff --git a/tests/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.BlogPost.dcm.xml b/tests/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.BlogPost.dcm.xml new file mode 100644 index 00000000000..48eda68df15 --- /dev/null +++ b/tests/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.BlogPost.dcm.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + diff --git a/tests/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.BlogPostComment.dcm.xml b/tests/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.BlogPostComment.dcm.xml new file mode 100644 index 00000000000..58c63565296 --- /dev/null +++ b/tests/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.BlogPostComment.dcm.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + +