Skip to content

Commit

Permalink
Add tests for referenceColumnName defaulting
Browse files Browse the repository at this point in the history
  • Loading branch information
curry684 committed Dec 21, 2024
1 parent 7229699 commit e5ff5b8
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/Tests/ORM/Mapping/AttributeDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,4 @@ class AttributeTransientAttribute implements MappingAttribute
class AttributeTransientClass
{
}

57 changes: 57 additions & 0 deletions tests/Tests/ORM/Mapping/MappingDriverTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()]
Expand Down Expand Up @@ -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,
],
],
]
);
}
}
101 changes: 101 additions & 0 deletions tests/Tests/ORM/Mapping/NamingStrategy/CustomPascalNamingStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Mapping\NamingStrategy;

use Doctrine\ORM\Mapping\NamingStrategy;

/**
* Fully customized naming strategy changing all namings to a PascalCase model. Included to test some behaviours
* regarding fully custom naming strategies.
*/
class CustomPascalNamingStrategy implements NamingStrategy
{
/**
* Returns a table name for an entity class.
*
* @param string $className The fully-qualified class name
* @return string A table name
*/
public function classToTableName(string $className): string
{
if (false !== mb_strpos($className, '\\')) {
return mb_substr($className, mb_strrpos($className, '\\') + 1);
}

return $className;
}

/**
* Returns a column name for a property.
*
* @param string $propertyName A property name
* @param string|null $className The fully-qualified class name
*
* @return string A column name
*/
public function propertyToColumnName(string $propertyName, ?string $className = null): string
{
if (null !== $className && mb_strtolower($propertyName) == mb_strtolower($this->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());
}
}
17 changes: 17 additions & 0 deletions tests/Tests/ORM/Mapping/NamingStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*
Expand All @@ -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'],
];
}

Expand Down Expand Up @@ -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'],
];
}

Expand Down Expand Up @@ -116,6 +130,9 @@ public static function dataReferenceColumnName(): array
// UnderscoreNamingStrategy
[self::underscoreNamingLower(), 'id'],
[self::underscoreNamingUpper(), 'ID'],

// CustomPascalNamingStrategy
[self::customNaming(), 'Id'],
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

<entity name="Doctrine\Tests\ORM\Mapping\BlogPost">

<id name="id" type="integer" column="id">
<generator strategy="NONE"/>
</id>

</entity>

</doctrine-mapping>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

<entity name="Doctrine\Tests\ORM\Mapping\BlogPostComment">

<id name="id" type="integer">
<generator strategy="NONE"/>
</id>

<many-to-one field="blogPost" target-entity="Doctrine\Tests\ORM\Mapping\BlogPost">
<join-columns>
<join-column nullable="false"/>
</join-columns>
</many-to-one>

</entity>

</doctrine-mapping>

0 comments on commit e5ff5b8

Please sign in to comment.