diff --git a/lib/Doctrine/ODM/MongoDB/DocumentManager.php b/lib/Doctrine/ODM/MongoDB/DocumentManager.php index e8d3c552f3..2e32218c92 100644 --- a/lib/Doctrine/ODM/MongoDB/DocumentManager.php +++ b/lib/Doctrine/ODM/MongoDB/DocumentManager.php @@ -734,27 +734,47 @@ public function createReference(object $document, array $referenceMapping) * class that is not defined in the discriminator map may only have a * discriminator field and no value, so default to the full class name. */ - if (isset($class->discriminatorField)) { - $reference[$class->discriminatorField] = $class->discriminatorValue ?? $class->name; + $discriminatorField = null; + $discriminatorValue = null; + if (isset($referenceMapping['discriminatorField'])) { + $discriminatorField = $referenceMapping['discriminatorField']; + if (isset($referenceMapping['discriminatorMap'])) { + $pos = array_search($class->name, $referenceMapping['discriminatorMap']); + if ($pos !== false) { + $discriminatorValue = $pos; + } + } else { + $discriminatorValue = $class->name; + } + } else { + $discriminatorField = $class->discriminatorField; + $discriminatorValue = $class->discriminatorValue; } + if (isset($discriminatorField)) { + if ($discriminatorValue === null) { + throw MappingException::unlistedClassInDiscriminatorMap($class->name); + } + $reference[$discriminatorField] = $discriminatorValue; + /* Add a discriminator value if the referenced document is not mapped * explicitly to a targetDocument class. */ - if (! isset($referenceMapping['targetDocument'])) { + } elseif (! isset($referenceMapping['targetDocument']) && $storeAs !== 'dbRefWithDb' && isset($referenceMapping['discriminatorField'])) { /* ??? && isset($referenceMapping['discriminatorValue'] */ $discriminatorField = $referenceMapping['discriminatorField']; - $discriminatorValue = isset($referenceMapping['discriminatorMap']) - ? array_search($class->name, $referenceMapping['discriminatorMap']) - : $class->name; - - /* If the discriminator value was not found in the map, use the full - * class name. In the future, it may be preferable to throw an - * exception here (perhaps based on some strictness option). - * - * @see PersistenceBuilder::prepareEmbeddedDocumentValue() - */ - if ($discriminatorValue === false) { + + $discriminatorMap = null; + if (isset($referenceMapping['discriminatorMap'])) { + $discriminatorMap = $referenceMapping['discriminatorMap']; + } + if (! isset($discriminatorMap)) { $discriminatorValue = $class->name; + } else { + $discriminatorValue = array_search($class->name, $discriminatorMap); + + if ($discriminatorValue === false) { + throw MappingException::unlistedClassInDiscriminatorMap($class->name); + } } $reference[$discriminatorField] = $discriminatorValue; diff --git a/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php b/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php index e937584ecf..9c744d39ce 100644 --- a/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php +++ b/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php @@ -28,6 +28,7 @@ use function count; use function get_class; use function in_array; +use function interface_exists; use function is_array; use function is_string; use function is_subclass_of; @@ -1902,6 +1903,28 @@ public function mapField(array $mapping) : array $mapping['discriminatorField'] = self::DEFAULT_DISCRIMINATOR_FIELD; } + if (isset($mapping['targetDocument']) && ! class_exists($mapping['targetDocument']) && ! interface_exists($mapping['targetDocument'])) { + $fullClassName = $this->reflClass->getNamespaceName() . '\\' . $mapping['targetDocument']; + if (! class_exists($fullClassName) && ! interface_exists($mapping['targetDocument'])) { + throw MappingException::nonExistingClass($mapping['targetDocument']); + } + + $mapping['targetDocument'] = $fullClassName; + } + + if (isset($mapping['discriminatorMap'])) { + foreach ($mapping['discriminatorMap'] as $value => $targetDocument) { + if (class_exists($targetDocument) || interface_exists($targetDocument)) { + continue; + } + $fullClassName = $this->reflClass->getNamespaceName() . '\\' . $targetDocument; + if (! class_exists($fullClassName) && ! interface_exists($targetDocument)) { + throw MappingException::nonExistingClass($fullClassName); + } + $mapping['discriminatorMap'][$value] = $fullClassName; + } + } + if (isset($mapping['version'])) { $mapping['notSaved'] = true; $this->setVersionMapping($mapping); diff --git a/lib/Doctrine/ODM/MongoDB/Mapping/MappingException.php b/lib/Doctrine/ODM/MongoDB/Mapping/MappingException.php index 02a804aaf9..e184a3c469 100644 --- a/lib/Doctrine/ODM/MongoDB/Mapping/MappingException.php +++ b/lib/Doctrine/ODM/MongoDB/Mapping/MappingException.php @@ -64,6 +64,11 @@ public static function invalidClassInDiscriminatorMap(string $className, string return new self(sprintf("Document class '%s' used in the discriminator map of class '%s' does not exist.", $className, $owningClass)); } + public static function unlistedClassInDiscriminatorMap(string $className) : self + { + return new self(sprintf('Document class "%s" is unlisted in the discriminator map.', $className)); + } + public static function invalidDiscriminatorValue(string $value, string $owningClass) : self { return new self(sprintf("Discriminator value '%s' used in the declaration of class '%s' does not exist.", $value, $owningClass)); diff --git a/lib/Doctrine/ODM/MongoDB/Persisters/PersistenceBuilder.php b/lib/Doctrine/ODM/MongoDB/Persisters/PersistenceBuilder.php index 89af48b529..54fef32df9 100644 --- a/lib/Doctrine/ODM/MongoDB/Persisters/PersistenceBuilder.php +++ b/lib/Doctrine/ODM/MongoDB/Persisters/PersistenceBuilder.php @@ -6,6 +6,7 @@ use Doctrine\ODM\MongoDB\DocumentManager; use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; +use Doctrine\ODM\MongoDB\Mapping\MappingException; use Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionInterface; use Doctrine\ODM\MongoDB\Types\Type; use Doctrine\ODM\MongoDB\UnitOfWork; @@ -96,7 +97,13 @@ public function prepareInsertData($document) // add discriminator if the class has one if (isset($class->discriminatorField)) { - $insertData[$class->discriminatorField] = $class->discriminatorValue ?? $class->name; + if ($class->discriminatorValue === null) { + if (! empty($class->discriminatorMap)) { + throw MappingException::unlistedClassInDiscriminatorMap($class->name); + } + $class->discriminatorValue = $class->name; + } + $insertData[$class->discriminatorField] = $class->discriminatorValue; } return $insertData; @@ -277,7 +284,13 @@ public function prepareUpsertData($document) // add discriminator if the class has one if (isset($class->discriminatorField)) { - $updateData['$set'][$class->discriminatorField] = $class->discriminatorValue ?? $class->name; + if ($class->discriminatorValue === null) { + if (! empty($class->discriminatorMap)) { + throw MappingException::unlistedClassInDiscriminatorMap($class->name); + } + $class->discriminatorValue = $class->name; + } + $updateData['$set'][$class->discriminatorField] = $class->discriminatorValue; } return $updateData; @@ -377,19 +390,15 @@ public function prepareEmbeddedDocumentValue(array $embeddedMapping, $embeddedDo /* Add a discriminator value if the embedded document is not mapped * explicitly to a targetDocument class. */ - if (! isset($embeddedMapping['targetDocument'])) { + if (! isset($embeddedMapping['targetDocument']) && isset($embeddedMapping['discriminatorField'])) { $discriminatorField = $embeddedMapping['discriminatorField']; - $discriminatorValue = isset($embeddedMapping['discriminatorMap']) - ? array_search($class->name, $embeddedMapping['discriminatorMap']) - : $class->name; - - /* If the discriminator value was not found in the map, use the full - * class name. In the future, it may be preferable to throw an - * exception here (perhaps based on some strictness option). - * - * @see DocumentManager::createDBRef() - */ - if ($discriminatorValue === false) { + if (! empty($embeddedMapping['discriminatorMap'])) { + $discriminatorValue = array_search($class->name, $embeddedMapping['discriminatorMap']); + + if ($discriminatorValue === false) { + throw MappingException::unlistedClassInDiscriminatorMap($class->name); + } + } else { $discriminatorValue = $class->name; } @@ -401,7 +410,13 @@ public function prepareEmbeddedDocumentValue(array $embeddedMapping, $embeddedDo * discriminator field and no value, so default to the full class name. */ if (isset($class->discriminatorField)) { - $embeddedDocumentValue[$class->discriminatorField] = $class->discriminatorValue ?? $class->name; + if ($class->discriminatorValue === null) { + if (! empty($class->discriminatorMap)) { + throw MappingException::unlistedClassInDiscriminatorMap($class->name); + } + $class->discriminatorValue = $class->name; + } + $embeddedDocumentValue[$class->discriminatorField] = $class->discriminatorValue; } // Ensure empty embedded documents are stored as BSON objects diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/EmbeddedTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/EmbeddedTest.php index e3d5286320..f4dd1ca9c8 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/EmbeddedTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/EmbeddedTest.php @@ -596,7 +596,7 @@ public function testEmbeddedDocumentWithDifferentFieldNameAnnotation() $embedded = new EmbeddedDocumentWithId(); $embedded->id = (string) new ObjectId(); - $firstEmbedded = new EmbedDocumentWithAnotherEmbed(); + $firstEmbedded = new EmbeddedDocumentWithAnotherEmbedded(); $firstEmbedded->embed = $embedded; $secondEmbedded = clone $firstEmbedded; @@ -668,7 +668,7 @@ class ChangeEmbeddedWithNameAnnotationTest /** * @ODM\EmbeddedDocument */ -class EmbedDocumentWithAnotherEmbed +class EmbeddedDocumentWithAnotherEmbedded { /** @ODM\EmbedOne(targetDocument=EmbeddedDocumentWithId::class, name="m_id") */ public $embed; diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH267Test.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH267Test.php index 0918703b0b..8b7d0a24d6 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH267Test.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH267Test.php @@ -67,7 +67,7 @@ class GH267User /** @ODM\Field(type="string") */ protected $name; - /** @ODM\ReferenceOne(name="company", targetDocument=GH267Company::class, discriminatorMap={"seller"="SellerCompany", "buyer"="BuyerCompany"}, inversedBy="users") */ + /** @ODM\ReferenceOne(name="company", targetDocument=GH267Company::class, discriminatorMap={"seller"="GH267SellerCompany", "buyer"="GH267BuyerCompany"}, inversedBy="users") */ protected $company; public function __construct($name) diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH788Test.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH788Test.php index a266744eeb..131cc8ae3c 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH788Test.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH788Test.php @@ -6,6 +6,7 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; +use Doctrine\ODM\MongoDB\Mapping\MappingException; use function get_class; class GH788Test extends BaseTest @@ -19,7 +20,6 @@ public function testDocumentWithDiscriminatorMap() $unlisted->name = 'unlisted'; $this->dm->persist($listed); - $this->dm->persist($unlisted); $this->dm->flush(); $this->dm->clear(); @@ -27,17 +27,11 @@ public function testDocumentWithDiscriminatorMap() $this->assertInstanceOf(GH788DocumentListed::class, $doc); $this->assertEquals('listed', $doc->name); - $doc = $this->dm->find(get_class($unlisted), $unlisted->id); - $this->assertInstanceOf(GH788DocumentUnlisted::class, $doc); - $this->assertEquals('unlisted', $doc->name); - - /* Attempting to find the unlisted class by the parent class will not - * work, as DocumentPersister::addDiscriminatorToPreparedQuery() adds - * discriminator criteria to the query, which limits the results to - * known, listed classes. - */ - $doc = $this->dm->find(get_class($listed), $unlisted->id); - $this->assertNull($doc); + $this->dm->persist($unlisted); + + $this->expectException(MappingException::class); + $this->expectExceptionMessage('Document class "' . GH788DocumentUnlisted::class . '" is unlisted in the discriminator map.'); + $this->dm->flush(); } public function testEmbedManyWithExternalDiscriminatorMap() @@ -53,17 +47,10 @@ public function testEmbedManyWithExternalDiscriminatorMap() $doc->externEmbedMany[] = $unlisted; $this->dm->persist($doc); - $this->dm->flush(); - $this->dm->clear(); - - $doc = $this->dm->find(get_class($doc), $doc->id); - $collection = $doc->externEmbedMany; - $this->assertCount(2, $collection); - $this->assertInstanceOf(GH788ExternEmbedListed::class, $collection[0]); - $this->assertEquals('listed', $collection[0]->name); - $this->assertInstanceOf(GH788ExternEmbedUnlisted::class, $collection[1]); - $this->assertEquals('unlisted', $collection[1]->name); + $this->expectException(MappingException::class); + $this->expectExceptionMessage('Document class "' . GH788ExternEmbedUnlisted::class . '" is unlisted in the discriminator map.'); + $this->dm->flush(); } public function testEmbedManyWithInlineDiscriminatorMap() @@ -79,17 +66,10 @@ public function testEmbedManyWithInlineDiscriminatorMap() $doc->inlineEmbedMany[] = $unlisted; $this->dm->persist($doc); - $this->dm->flush(); - $this->dm->clear(); - $doc = $this->dm->find(get_class($doc), $doc->id); - $collection = $doc->inlineEmbedMany; - - $this->assertCount(2, $collection); - $this->assertInstanceOf(GH788InlineEmbedListed::class, $collection[0]); - $this->assertEquals('listed', $collection[0]->name); - $this->assertInstanceOf(GH788InlineEmbedUnlisted::class, $collection[1]); - $this->assertEquals('unlisted', $collection[1]->name); + $this->expectException(MappingException::class); + $this->expectExceptionMessage('Document class "' . GH788InlineEmbedUnlisted::class . '" is unlisted in the discriminator map.'); + $this->dm->flush(); } public function testEmbedManyWithNoTargetAndExternalDiscriminatorMap() @@ -105,17 +85,10 @@ public function testEmbedManyWithNoTargetAndExternalDiscriminatorMap() $doc->noTargetEmbedMany[] = $unlisted; $this->dm->persist($doc); - $this->dm->flush(); - $this->dm->clear(); - - $doc = $this->dm->find(get_class($doc), $doc->id); - $collection = $doc->noTargetEmbedMany; - $this->assertCount(2, $collection); - $this->assertInstanceOf(GH788ExternEmbedListed::class, $collection[0]); - $this->assertEquals('listed', $collection[0]->name); - $this->assertInstanceOf(GH788ExternEmbedUnlisted::class, $collection[1]); - $this->assertEquals('unlisted', $collection[1]->name); + $this->expectException(MappingException::class); + $this->expectExceptionMessage('Document class "' . GH788ExternEmbedUnlisted::class . '" is unlisted in the discriminator map.'); + $this->dm->flush(); } public function testEmbedOneWithExternalDiscriminatorMap() @@ -127,13 +100,10 @@ public function testEmbedOneWithExternalDiscriminatorMap() $doc->externEmbedOne = $unlisted; $this->dm->persist($doc); - $this->dm->flush(); - $this->dm->clear(); - - $doc = $this->dm->find(get_class($doc), $doc->id); - $this->assertInstanceOf(GH788ExternEmbedUnlisted::class, $doc->externEmbedOne); - $this->assertEquals('unlisted', $doc->externEmbedOne->name); + $this->expectException(MappingException::class); + $this->expectExceptionMessage('Document class "' . GH788ExternEmbedUnlisted::class . '" is unlisted in the discriminator map.'); + $this->dm->flush(); } public function testEmbedOneWithInlineDiscriminatorMap() @@ -145,13 +115,10 @@ public function testEmbedOneWithInlineDiscriminatorMap() $doc->inlineEmbedOne = $unlisted; $this->dm->persist($doc); - $this->dm->flush(); - $this->dm->clear(); - - $doc = $this->dm->find(get_class($doc), $doc->id); - $this->assertInstanceOf(GH788InlineEmbedUnlisted::class, $doc->inlineEmbedOne); - $this->assertEquals('unlisted', $doc->inlineEmbedOne->name); + $this->expectException(MappingException::class); + $this->expectExceptionMessage('Document class "' . GH788InlineEmbedUnlisted::class . '" is unlisted in the discriminator map.'); + $this->dm->flush(); } public function testEmbedOneWithNoTargetAndExternalDiscriminatorMap() @@ -163,13 +130,10 @@ public function testEmbedOneWithNoTargetAndExternalDiscriminatorMap() $doc->noTargetEmbedOne = $unlisted; $this->dm->persist($doc); - $this->dm->flush(); - $this->dm->clear(); - $doc = $this->dm->find(get_class($doc), $doc->id); - - $this->assertInstanceOf(GH788ExternEmbedUnlisted::class, $doc->noTargetEmbedOne); - $this->assertEquals('unlisted', $doc->noTargetEmbedOne->name); + $this->expectException(MappingException::class); + $this->expectExceptionMessage('Document class "' . GH788ExternEmbedUnlisted::class . '" is unlisted in the discriminator map.'); + $this->dm->flush(); } public function testRefManyWithExternalDiscriminatorMap() @@ -185,19 +149,10 @@ public function testRefManyWithExternalDiscriminatorMap() $doc->externRefMany[] = $unlisted; $this->dm->persist($doc); - $this->dm->flush(); - $this->dm->clear(); - $doc = $this->dm->find(get_class($doc), $doc->id); - $collection = $doc->externRefMany; - - $this->assertCount(2, $collection); - $this->assertInstanceOf(GH788ExternRefListed::class, $collection[0]); - $this->assertEquals($listed->id, $collection[0]->id); - $this->assertEquals('listed', $collection[0]->name); - $this->assertInstanceOf(GH788ExternRefUnlisted::class, $collection[1]); - $this->assertEquals($unlisted->id, $collection[1]->id); - $this->assertEquals('unlisted', $collection[1]->name); + $this->expectException(MappingException::class); + $this->expectExceptionMessage('Document class "' . GH788ExternRefUnlisted::class . '" is unlisted in the discriminator map.'); + $this->dm->flush(); } public function testRefManyWithInlineDiscriminatorMap() @@ -213,19 +168,10 @@ public function testRefManyWithInlineDiscriminatorMap() $doc->inlineRefMany[] = $unlisted; $this->dm->persist($doc); - $this->dm->flush(); - $this->dm->clear(); - - $doc = $this->dm->find(get_class($doc), $doc->id); - $collection = $doc->inlineRefMany; - $this->assertCount(2, $collection); - $this->assertInstanceOf(GH788InlineRefListed::class, $collection[0]); - $this->assertEquals($listed->id, $collection[0]->id); - $this->assertEquals('listed', $collection[0]->name); - $this->assertInstanceOf(GH788InlineRefUnlisted::class, $collection[1]); - $this->assertEquals($unlisted->id, $collection[1]->id); - $this->assertEquals('unlisted', $collection[1]->name); + $this->expectException(MappingException::class); + $this->expectExceptionMessage('Document class "' . GH788InlineRefUnlisted::class . '" is unlisted in the discriminator map.'); + $this->dm->flush(); } public function testRefManyWithNoTargetAndExternalDiscriminatorMap() @@ -241,19 +187,10 @@ public function testRefManyWithNoTargetAndExternalDiscriminatorMap() $doc->noTargetRefMany[] = $unlisted; $this->dm->persist($doc); - $this->dm->flush(); - $this->dm->clear(); - - $doc = $this->dm->find(get_class($doc), $doc->id); - $collection = $doc->noTargetRefMany; - $this->assertCount(2, $collection); - $this->assertInstanceOf(GH788ExternRefListed::class, $collection[0]); - $this->assertEquals($listed->id, $collection[0]->id); - $this->assertEquals('listed', $collection[0]->name); - $this->assertInstanceOf(GH788ExternRefUnlisted::class, $collection[1]); - $this->assertEquals($unlisted->id, $collection[1]->id); - $this->assertEquals('unlisted', $collection[1]->name); + $this->expectException(MappingException::class); + $this->expectExceptionMessage('Document class "' . GH788ExternRefUnlisted::class . '" is unlisted in the discriminator map.'); + $this->dm->flush(); } public function testRefOneWithExternalDiscriminatorMap() @@ -265,14 +202,10 @@ public function testRefOneWithExternalDiscriminatorMap() $doc->externRefOne = $unlisted; $this->dm->persist($doc); - $this->dm->flush(); - $this->dm->clear(); - - $doc = $this->dm->find(get_class($doc), $doc->id); - $this->assertInstanceOf(GH788ExternRefUnlisted::class, $doc->externRefOne); - $this->assertEquals($unlisted->id, $doc->externRefOne->id); - $this->assertEquals('unlisted', $doc->externRefOne->name); + $this->expectException(MappingException::class); + $this->expectExceptionMessage('Document class "' . GH788ExternRefUnlisted::class . '" is unlisted in the discriminator map.'); + $this->dm->flush(); } public function testRefOneWithInlineDiscriminatorMap() @@ -284,14 +217,10 @@ public function testRefOneWithInlineDiscriminatorMap() $doc->inlineRefOne = $unlisted; $this->dm->persist($doc); - $this->dm->flush(); - $this->dm->clear(); - $doc = $this->dm->find(get_class($doc), $doc->id); - - $this->assertInstanceOf(GH788InlineRefUnlisted::class, $doc->inlineRefOne); - $this->assertEquals($unlisted->id, $doc->inlineRefOne->id); - $this->assertEquals('unlisted', $doc->inlineRefOne->name); + $this->expectException(MappingException::class); + $this->expectExceptionMessage('Document class "' . GH788InlineRefUnlisted::class . '" is unlisted in the discriminator map.'); + $this->dm->flush(); } public function testRefOneWithNoTargetAndExternalDiscriminatorMap() @@ -303,14 +232,10 @@ public function testRefOneWithNoTargetAndExternalDiscriminatorMap() $doc->noTargetRefOne = $unlisted; $this->dm->persist($doc); - $this->dm->flush(); - $this->dm->clear(); - - $doc = $this->dm->find(get_class($doc), $doc->id); - $this->assertInstanceOf(GH788ExternRefUnlisted::class, $doc->noTargetRefOne); - $this->assertEquals($unlisted->id, $doc->noTargetRefOne->id); - $this->assertEquals('unlisted', $doc->noTargetRefOne->name); + $this->expectException(MappingException::class); + $this->expectExceptionMessage('Document class "' . GH788ExternRefUnlisted::class . '" is unlisted in the discriminator map.'); + $this->dm->flush(); } } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH942Test.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH942Test.php index 33c4ecc527..811cc2fb0c 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH942Test.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH942Test.php @@ -5,6 +5,7 @@ namespace Doctrine\ODM\MongoDB\Tests\Functional\Ticket; use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; +use Doctrine\ODM\MongoDB\Mapping\MappingException; use Doctrine\ODM\MongoDB\Tests\BaseTest; use MongoDB\BSON\ObjectId; @@ -34,7 +35,6 @@ public function testDiscriminatorValueUsesClassNameIfNotInMap() $child->name = 'child'; $this->dm->persist($parent); - $this->dm->persist($child); $this->dm->flush(); $this->dm->clear(); @@ -44,11 +44,11 @@ public function testDiscriminatorValueUsesClassNameIfNotInMap() $this->assertSame('parent', $parent['name']); $this->assertSame('p', $parent['type']); - $child = $this->dm->getDocumentCollection(GH942DocumentChild::CLASSNAME) - ->findOne(['_id' => new ObjectId($child->id)]); + $this->dm->persist($child); - $this->assertSame('child', $child['name']); - $this->assertSame(GH942DocumentChild::CLASSNAME, $child['type']); + $this->expectException(MappingException::class); + $this->expectExceptionMessage('Document class "' . GH942DocumentChild::class . '" is unlisted in the discriminator map.'); + $this->dm->flush(); } } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Mapping/BasicInheritanceMappingTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Mapping/BasicInheritanceMappingTest.php index 36e63cf387..508589e533 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Mapping/BasicInheritanceMappingTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Mapping/BasicInheritanceMappingTest.php @@ -7,6 +7,7 @@ use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory; use Doctrine\ODM\MongoDB\Tests\BaseTest; +use Doctrine\ODM\MongoDB\Tests\Functional\MappedSuperclassRelated1; use function serialize; use function unserialize; diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataFactoryTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataFactoryTest.php index 055d4e9ac3..d217860d8f 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataFactoryTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataFactoryTest.php @@ -117,3 +117,8 @@ class TestDocument1 private $other; private $association; } + +class Other +{ + private $name; +} diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest.php index c5056183d0..6bc599a8d5 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest.php @@ -15,7 +15,7 @@ use Documents\Account; use Documents\Address; use Documents\Album; -use Documents\Bar; +use Documents\Bars\Bar; use Documents\CmsUser; use Documents\SpecialUser; use Documents\User; diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Mapping/Driver/fixtures/xml/TestDocuments.User.dcm.xml b/tests/Doctrine/ODM/MongoDB/Tests/Mapping/Driver/fixtures/xml/TestDocuments.User.dcm.xml index cf6c837cb0..495d2ac465 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Mapping/Driver/fixtures/xml/TestDocuments.User.dcm.xml +++ b/tests/Doctrine/ODM/MongoDB/Tests/Mapping/Driver/fixtures/xml/TestDocuments.User.dcm.xml @@ -18,7 +18,7 @@ - +