diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php index d36ea308464..7a7c2bc6d1a 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php @@ -31,6 +31,7 @@ use Doctrine\Persistence\Mapping\ClassMetadata as ClassMetadataInterface; use Doctrine\Persistence\Mapping\Driver\MappingDriver; use Doctrine\Persistence\Mapping\ReflectionService; +use ReflectionClass; use ReflectionException; use function assert; use function interface_exists; @@ -278,6 +279,11 @@ protected function validateRuntimeMetadata($class, $parent) if ( ! $class->discriminatorColumn) { throw MappingException::missingDiscriminatorColumn($class->name); } + foreach ($class->subClasses as $subClass) { + if ((new ReflectionClass($subClass))->name !== $subClass) { + throw MappingException::invalidClassInDiscriminatorMap($subClass, $class->name); + } + } } } else if ($class->isMappedSuperclass && $class->name == $class->rootEntityName && (count($class->discriminatorMap) || $class->discriminatorColumn)) { // second condition is necessary for mapped superclasses in the middle of an inheritance hierarchy diff --git a/tests/Doctrine/Tests/Models/CaseSensitiveDiscriminatorMap/Cube.php b/tests/Doctrine/Tests/Models/CaseSensitiveDiscriminatorMap/Cube.php new file mode 100644 index 00000000000..0c608f7453b --- /dev/null +++ b/tests/Doctrine/Tests/Models/CaseSensitiveDiscriminatorMap/Cube.php @@ -0,0 +1,10 @@ +setInheritanceType(ClassMetadataInfo::INHERITANCE_TYPE_SINGLE_TABLE); + $metadata->setDiscriminatorColumn([ + 'name' => 'discr', + 'type' => 'string', + 'length' => 32, + ]); + $metadata->setDiscriminatorMap([ + 'cube' => cube::class, + ]); + $metadata->mapField([ + 'fieldName' => 'id', + 'type' => 'string', + 'length' => null, + 'precision' => 0, + 'scale' => 0, + 'nullable' => false, + 'unique' => false, + 'id' => true, + 'columnName' => 'id', + ]); + $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO); + } +} diff --git a/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php b/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php index 502ed479e28..eb2388b9e52 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php @@ -14,6 +14,7 @@ use Doctrine\ORM\Mapping\UnderscoreNamingStrategy; use Doctrine\Persistence\Mapping\RuntimeReflectionService; use Doctrine\Tests\Models\Cache\City; +use Doctrine\Tests\Models\CaseSensitiveDiscriminatorMap; use Doctrine\Tests\Models\CMS\CmsAddress; use Doctrine\Tests\Models\CMS\CmsAddressListener; use Doctrine\Tests\Models\CMS\CmsUser; @@ -37,6 +38,7 @@ use Doctrine\Tests\Models\DDC964\DDC964Admin; use Doctrine\Tests\Models\DDC964\DDC964Guest; use Doctrine\Tests\OrmTestCase; +use function class_exists; abstract class AbstractMappingDriverTest extends OrmTestCase { @@ -1079,6 +1081,17 @@ public function testDiscriminatorColumnDefaultName() $this->assertEquals('dtype', $class->discriminatorColumn['name']); } + public function testInvalidSubClassCase() + { + class_exists(CaseSensitiveDiscriminatorMap\Cube::class); + + $this->expectException(MappingException::class); + $this->expectExceptionMessage('Entity class \'Doctrine\Tests\Models\CaseSensitiveDiscriminatorMap\cube\' used in the discriminator map of class \'Doctrine\Tests\Models\CaseSensitiveDiscriminatorMap\Shape\' does not exist.'); + + $em = $this->_getTestEntityManager(); + $factory = $this->createClassMetadataFactory($em); + $factory->getMetadataFor(CaseSensitiveDiscriminatorMap\Shape::class); + } } /** @@ -1344,7 +1357,6 @@ public static function loadMetadata(ClassMetadataInfo $metadata) } } - /** * @Entity */ diff --git a/tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.Models.CaseSensitiveDiscriminatorMap.Shape.php b/tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.Models.CaseSensitiveDiscriminatorMap.Shape.php new file mode 100644 index 00000000000..5299f8246c9 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.Models.CaseSensitiveDiscriminatorMap.Shape.php @@ -0,0 +1,28 @@ +setInheritanceType(ClassMetadataInfo::INHERITANCE_TYPE_SINGLE_TABLE); +$metadata->setDiscriminatorColumn([ + 'name' => 'discr', + 'type' => 'string', + 'length' => 32, +]); +$metadata->setDiscriminatorMap([ + 'cube' => cube::class, +]); +$metadata->mapField([ + 'fieldName' => 'id', + 'type' => 'string', + 'length' => null, + 'precision' => 0, + 'scale' => 0, + 'nullable' => false, + 'unique' => false, + 'id' => true, + 'columnName' => 'id', +]); +$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO); diff --git a/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.CaseSensitiveDiscriminatorMap.Shape.dcm.xml b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.CaseSensitiveDiscriminatorMap.Shape.dcm.xml new file mode 100644 index 00000000000..51139c14bc3 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.CaseSensitiveDiscriminatorMap.Shape.dcm.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + diff --git a/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.Models.CaseSensitiveDiscriminatorMap.Shape.dcm.yml b/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.Models.CaseSensitiveDiscriminatorMap.Shape.dcm.yml new file mode 100644 index 00000000000..062f9ddc99f --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.Models.CaseSensitiveDiscriminatorMap.Shape.dcm.yml @@ -0,0 +1,14 @@ +Doctrine\Tests\Models\CaseSensitiveDiscriminatorMap\Shape: + type: entity + inheritanceType: SINGLE_TABLE + discriminatorMap: + cube: Doctrine\Tests\Models\CaseSensitiveDiscriminatorMap\cube + discriminatorColumn: + type: string + name: discr + length: 32 + id: + id: + type: integer + generator: + strategy: AUTO