Skip to content

Commit

Permalink
Make all mapped database types case insensitive (#6321)
Browse files Browse the repository at this point in the history
<!-- Fill in the relevant information below to help triage your pull
request. -->

|      Q       |   A
|------------- | -----------
| Type         | improvement
| Fixed issues | <!-- use #NUM format to reference an issue -->

#### Summary

<!-- Provide a summary of your change. -->
Currently, a custom mapping type has to return an array of lowercase
strings in `getMappedDatabaseTypes` or else Doctrine fails to retrieve
the added type as both `AbstractPlatform::getDoctrineTypeMapping` and
`AbstractPlatform::hasDoctrineTypeMappingFor` do a lowercase conversion
before the look-up. A devolper should not depend on internal
representation.

This PR addresses this by storing the mapped type in lowercase to
`AbstractPlatform#doctrineTypeMapping`. This is in line with
`AbstractPlatform::registerDoctrineTypeMapping`, which does a lowercase
conversion as well.
  • Loading branch information
Rainrider committed Mar 3, 2024
1 parent b4329d5 commit 6fc2230
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ private function initializeAllDoctrineTypeMappings(): void

foreach (Type::getTypesMap() as $typeName => $className) {
foreach (Type::getType($typeName)->getMappedDatabaseTypes($this) as $dbType) {
$dbType = strtolower($dbType);
$this->doctrineTypeMapping[$dbType] = $typeName;
}
}
Expand Down
34 changes: 34 additions & 0 deletions tests/Platforms/AbstractPlatformTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,40 @@ public function testRegisterDoctrineMappingType(): void
self::assertEquals(Types::INTEGER, $this->platform->getDoctrineTypeMapping('foo'));
}

public function testCaseInsensitiveDoctrineTypeMappingFromType(): void
{
$type = new class () extends Type {
/**
* {@inheritDoc}
*/
public function getMappedDatabaseTypes(AbstractPlatform $platform): array
{
return ['TESTTYPE'];
}

public function getName(): string
{
return 'testtype';
}

/**
* {@inheritDoc}
*/
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
return $platform->getDecimalTypeDeclarationSQL($column);
}
};

if (Type::hasType($type->getName())) {
Type::overrideType($type->getName(), get_class($type));
} else {
Type::addType($type->getName(), get_class($type));
}

self::assertSame($type->getName(), $this->platform->getDoctrineTypeMapping('TeStTyPe'));
}

public function testRegisterUnknownDoctrineMappingType(): void
{
$this->expectException(Exception::class);
Expand Down

0 comments on commit 6fc2230

Please sign in to comment.