From d89617fc824c835e7b92e30788840d84e1a5d110 Mon Sep 17 00:00:00 2001 From: watari Date: Fri, 9 Nov 2018 07:43:06 +0200 Subject: [PATCH 1/3] Updated persisting logic to throw exception of class is not listed in discriminator map. --- lib/Doctrine/ODM/MongoDB/DocumentManager.php | 48 ++++-- .../ODM/MongoDB/Mapping/ClassMetadata.php | 23 +++ .../ODM/MongoDB/Mapping/MappingException.php | 5 + .../MongoDB/Persisters/PersistenceBuilder.php | 43 +++-- .../MongoDB/Tests/Functional/EmbeddedTest.php | 4 +- .../Tests/Functional/Ticket/GH267Test.php | 2 +- .../Tests/Functional/Ticket/GH788Test.php | 159 +++++------------- .../Tests/Functional/Ticket/GH942Test.php | 10 +- .../Mapping/BasicInheritanceMappingTest.php | 1 + .../Mapping/ClassMetadataFactoryTest.php | 5 + .../Tests/Mapping/ClassMetadataTest.php | 2 +- .../fixtures/xml/TestDocuments.User.dcm.xml | 2 +- 12 files changed, 149 insertions(+), 155 deletions(-) diff --git a/lib/Doctrine/ODM/MongoDB/DocumentManager.php b/lib/Doctrine/ODM/MongoDB/DocumentManager.php index e8d3c552f3..e9da0e16e9 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'])) { $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..277a0ceea8 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($fullClassName)) { + 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($fullClassName)) { + 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..f50e0db345 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; @@ -379,17 +392,13 @@ public function prepareEmbeddedDocumentValue(array $embeddedMapping, $embeddedDo */ if (! isset($embeddedMapping['targetDocument'])) { $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 @@ - + From a15ec3b534d4230d2ad784a018033a0324c4b126 Mon Sep 17 00:00:00 2001 From: watari Date: Mon, 12 Nov 2018 20:40:01 +0200 Subject: [PATCH 2/3] Removed additional checks of class existence for targetDocument and discriminator map from ClassMetadata class. Replaced tests and docs class name literals by `::class` constants. Updated docs according to new behaviour with unregistered classes. --- .../resolve-target-document-listener.rst | 2 +- docs/en/reference/aggregation-builder.rst | 4 +-- docs/en/reference/annotations-reference.rst | 32 +++++++++-------- docs/en/reference/basic-mapping.rst | 4 +-- docs/en/reference/embedded-mapping.rst | 8 ++--- docs/en/reference/inheritance-mapping.rst | 4 +-- docs/en/reference/reference-mapping.rst | 8 ++--- lib/Doctrine/ODM/MongoDB/DocumentManager.php | 35 +++++++++++-------- .../ODM/MongoDB/Mapping/ClassMetadata.php | 23 ------------ .../ODM/MongoDB/Tests/DocumentManagerTest.php | 2 +- .../Tests/Functional/AtomicSetTest.php | 6 ++-- .../Functional/DocumentPersisterTest.php | 12 +++---- .../Functional/NestedCollectionsTest.php | 12 +++---- .../Tests/Functional/TestTargetDocument.php | 2 +- .../Tests/Functional/Ticket/GH267Test.php | 2 +- .../Tests/Functional/Ticket/GH426Test.php | 2 +- .../Tests/Functional/Ticket/GH788Test.php | 8 ++--- .../Tests/Functional/Ticket/GH927Test.php | 2 +- .../Tests/Functional/Ticket/MODM65Test.php | 2 +- .../Tests/Mapping/ClassMetadataTest.php | 4 +-- .../Documents/GlobalNamespaceDocument.php | 2 +- .../ODM/MongoDB/Tests/Query/BuilderTest.php | 16 ++++----- .../Doctrine/ODM/MongoDB/Tests/QueryTest.php | 14 ++++---- .../ResolveTargetDocumentListenerTest.php | 8 ++--- tests/Documents/Bars/Bar.php | 2 +- .../Ecommerce/ConfigurableProduct.php | 2 +- tests/Documents/Ecommerce/Money.php | 2 +- tests/Documents/Ecommerce/Option.php | 4 +-- tests/Documents/Ecommerce/StockItem.php | 2 +- tests/Documents/Functional/FavoritesUser.php | 4 +-- tests/Documents/Functional/VirtualHost.php | 2 +- .../Functional/VirtualHostDirective.php | 2 +- tests/Documents/ReferenceUser.php | 2 +- 33 files changed, 111 insertions(+), 125 deletions(-) diff --git a/docs/en/cookbook/resolve-target-document-listener.rst b/docs/en/cookbook/resolve-target-document-listener.rst index 8d60d2eacc..eb82a29454 100644 --- a/docs/en/cookbook/resolve-target-document-listener.rst +++ b/docs/en/cookbook/resolve-target-document-listener.rst @@ -75,7 +75,7 @@ An Invoice document: class Invoice { /** - * @ODM\ReferenceOne(targetDocument="Acme\InvoiceModule\Model\InvoiceSubjectInterface") + * @ODM\ReferenceOne(targetDocument=Acme\InvoiceModule\Model\InvoiceSubjectInterface::class) * @var InvoiceSubjectInterface */ protected $subject; diff --git a/docs/en/reference/aggregation-builder.rst b/docs/en/reference/aggregation-builder.rst index 5a79dbbd88..ce12998b11 100644 --- a/docs/en/reference/aggregation-builder.rst +++ b/docs/en/reference/aggregation-builder.rst @@ -479,7 +479,7 @@ pipeline stages. Take the following relationship for example: /** * @ReferenceMany( - * targetDocument="Documents\Item", + * targetDocument=Documents\Item::class, * cascade="all", * storeAs="id" * ) @@ -504,7 +504,7 @@ to be considered when looking up one-to-one relationships: /** * @ReferenceOne( - * targetDocument="Documents\Item", + * targetDocument=Documents\Item::class, * cascade="all", * storeAs="id" * ) diff --git a/docs/en/reference/annotations-reference.rst b/docs/en/reference/annotations-reference.rst index 5412fd8fda..32318a11eb 100644 --- a/docs/en/reference/annotations-reference.rst +++ b/docs/en/reference/annotations-reference.rst @@ -73,7 +73,7 @@ correspond to a value from the configured discriminator map. * @Document * @InheritanceType("SINGLE_COLLECTION") * @DiscriminatorField("type") - * @DiscriminatorMap({"person" = "Person", "employee" = "Employee"}) + * @DiscriminatorMap({"person" = Person::class, "employee" = Employee::class}) * @DefaultDiscriminatorValue("person") */ class Person @@ -115,9 +115,11 @@ during hydration to select the instantiation class. This annotation is required for the top-most class in a :ref:`single collection inheritance ` hierarchy. It takes an array as its only argument, which maps keys to class names. The -class names may be fully qualified or relative to the current namespace. When +class constant `::class` must be used as source of class name. When a document is persisted to the database, its class name key will be stored in -the discriminator field instead of the |FQCN|. +the discriminator field instead of the |FQCN|. If class of persisted to the +database document is not registered in discriminator map and discriminator map +is not empty - ``\Doctrine\ODM\MongoDB\Mapping\MappingException`` will be thrown. .. code-block:: php @@ -127,7 +129,7 @@ the discriminator field instead of the |FQCN|. * @Document * @InheritanceType("SINGLE_COLLECTION") * @DiscriminatorField("type") - * @DiscriminatorMap({"person" = "Person", "employee" = "Employee"}) + * @DiscriminatorMap({"person" = Person::class, "employee" = Employee::class}) */ class Person { @@ -219,8 +221,8 @@ Optional attributes: * strategy="set", * discriminatorField="type", * discriminatorMap={ - * "book"="Documents\BookTag", - * "song"="Documents\SongTag" + * "book"=Documents\BookTag::class, + * "song"=Documents\SongTag::class * }, * defaultDiscriminatorValue="book" * ) @@ -269,8 +271,8 @@ Optional attributes: * @EmbedOne( * discriminatorField="type", * discriminatorMap={ - * "user"="Documents\User", - * "author"="Documents\Author" + * "user"=Documents\User::class, + * "author"=Documents\Author::class * }, * defaultDiscriminatorValue="user" * ) @@ -596,7 +598,7 @@ Examples: * @Document * @InheritanceType("SINGLE_COLLECTION") * @DiscriminatorField("type") - * @DiscriminatorMap({"person"="Person", "employee"="Employee"}) + * @DiscriminatorMap({"person"=Person::class, "employee"=Employee::class}) */ class Person { @@ -964,13 +966,13 @@ Optional attributes: /** * @ReferenceMany( * strategy="set", - * targetDocument="Documents\Item", + * targetDocument=Documents\Item::class, * cascade="all", * sort={"sort_field": "asc"} * discriminatorField="type", * discriminatorMap={ - * "book"="Documents\BookItem", - * "song"="Documents\SongItem" + * "book"=Documents\BookItem::class, + * "song"=Documents\SongItem::class * }, * defaultDiscriminatorValue="book" * ) @@ -1030,12 +1032,12 @@ Optional attributes: /** * @ReferenceOne( - * targetDocument="Documents\Item", + * targetDocument=Documents\Item::class, * cascade="all", * discriminatorField="type", * discriminatorMap={ - * "book"="Documents\BookItem", - * "song"="Documents\SongItem" + * "book"=Documents\BookItem::class, + * "song"=Documents\SongItem::class * }, * defaultDiscriminatorValue="book" * ) diff --git a/docs/en/reference/basic-mapping.rst b/docs/en/reference/basic-mapping.rst index 75dd321fd8..6bd68fbc82 100644 --- a/docs/en/reference/basic-mapping.rst +++ b/docs/en/reference/basic-mapping.rst @@ -520,7 +520,7 @@ the collection. Here is an example: /** * @Document(collection="my_documents") * @DiscriminatorField("type") - * @DiscriminatorMap({"article"="Article", "album"="Album"}) + * @DiscriminatorMap({"article"=Article::class, "album"=Album::class}) */ class Article { @@ -530,7 +530,7 @@ the collection. Here is an example: /** * @Document(collection="my_documents") * @DiscriminatorField("type") - * @DiscriminatorMap({"article"="Article", "album"="Album"}) + * @DiscriminatorMap({"article"=Article::class, "album"=Album::class}) */ class Album { diff --git a/docs/en/reference/embedded-mapping.rst b/docs/en/reference/embedded-mapping.rst index 6f736a704d..d154bcc686 100644 --- a/docs/en/reference/embedded-mapping.rst +++ b/docs/en/reference/embedded-mapping.rst @@ -164,8 +164,8 @@ in each embedded document: /** * @EmbedMany( * discriminatorMap={ - * "download"="DownloadTask", - * "build"="BuildTask" + * "download"=DownloadTask::class, + * "build"=BuildTask::class * } * ) */ @@ -201,8 +201,8 @@ discriminator: /** * @EmbedMany( * discriminatorMap={ - * "download"="DownloadTask", - * "build"="BuildTask" + * "download"=DownloadTask::class, + * "build"=BuildTask::class * }, * defaultDiscriminatorValue="download" * ) diff --git a/docs/en/reference/inheritance-mapping.rst b/docs/en/reference/inheritance-mapping.rst index b3c7066409..902f15bddb 100644 --- a/docs/en/reference/inheritance-mapping.rst +++ b/docs/en/reference/inheritance-mapping.rst @@ -72,7 +72,7 @@ Simple example: * @Document * @InheritanceType("SINGLE_COLLECTION") * @DiscriminatorField("type") - * @DiscriminatorMap({"person"="Person", "employee"="Employee"}) + * @DiscriminatorMap({"person"=Person::class, "employee"=Employee::class}) */ class Person { @@ -149,7 +149,7 @@ discriminator field: * @Document * @InheritanceType("SINGLE_COLLECTION") * @DiscriminatorField("type") - * @DiscriminatorMap({"person"="Person", "employee"="Employee"}) + * @DiscriminatorMap({"person"=Person::class, "employee"=Employee::class}) * @DefaultDiscriminatorValue("person") */ class Person diff --git a/docs/en/reference/reference-mapping.rst b/docs/en/reference/reference-mapping.rst index 37fb7607ef..9fa8016cb3 100644 --- a/docs/en/reference/reference-mapping.rst +++ b/docs/en/reference/reference-mapping.rst @@ -208,8 +208,8 @@ in each `DBRef`_ object: /** * @ReferenceMany( * discriminatorMap={ - * "album"="Album", - * "song"="Song" + * "album"=Album::class, + * "song"=Song::class * } * ) */ @@ -244,8 +244,8 @@ a certain class, you can optionally specify a default discriminator value: /** * @ReferenceMany( * discriminatorMap={ - * "album"="Album", - * "song"="Song" + * "album"=Album::class, + * "song"=Song::class * }, * defaultDiscriminatorValue="album" * ) diff --git a/lib/Doctrine/ODM/MongoDB/DocumentManager.php b/lib/Doctrine/ODM/MongoDB/DocumentManager.php index e9da0e16e9..be05556436 100644 --- a/lib/Doctrine/ODM/MongoDB/DocumentManager.php +++ b/lib/Doctrine/ODM/MongoDB/DocumentManager.php @@ -730,12 +730,24 @@ public function createReference(object $document, array $referenceMapping) throw new InvalidArgumentException(sprintf('Reference type %s is invalid.', $storeAs)); } - /* If the class has a discriminator (field and value), use it. A child - * 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. - */ + return $reference + $this->getDiscriminatorData($referenceMapping, $class); + } + + /** + * Build discriminator portion of reference for specified reference mapping and class metadata. + * + * @param array $referenceMapping Mappings of reference for which discriminator data is created. + * @param ClassMetadata $class Metadata of reference document class. + * + * @return array with next structure [{discriminator field} => {discriminator value}] + * + * @throws MappingException When discriminator map is present and reference class in not registered in it. + */ + private function getDiscriminatorData(array $referenceMapping, ClassMetadata $class) : array + { $discriminatorField = null; $discriminatorValue = null; + $discriminatorData = []; if (isset($referenceMapping['discriminatorField'])) { $discriminatorField = $referenceMapping['discriminatorField']; if (isset($referenceMapping['discriminatorMap'])) { @@ -751,15 +763,11 @@ public function createReference(object $document, array $referenceMapping) $discriminatorValue = $class->discriminatorValue; } - if (isset($discriminatorField)) { + if ($discriminatorField !== null) { 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. - */ + $discriminatorData = [$discriminatorField => $discriminatorValue]; } elseif (! isset($referenceMapping['targetDocument'])) { $discriminatorField = $referenceMapping['discriminatorField']; @@ -767,7 +775,7 @@ public function createReference(object $document, array $referenceMapping) if (isset($referenceMapping['discriminatorMap'])) { $discriminatorMap = $referenceMapping['discriminatorMap']; } - if (! isset($discriminatorMap)) { + if ($discriminatorMap === null) { $discriminatorValue = $class->name; } else { $discriminatorValue = array_search($class->name, $discriminatorMap); @@ -776,11 +784,10 @@ public function createReference(object $document, array $referenceMapping) throw MappingException::unlistedClassInDiscriminatorMap($class->name); } } - - $reference[$discriminatorField] = $discriminatorValue; + $discriminatorData = [$discriminatorField => $discriminatorValue]; } - return $reference; + return $discriminatorData; } /** diff --git a/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php b/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php index 277a0ceea8..e937584ecf 100644 --- a/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php +++ b/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php @@ -28,7 +28,6 @@ 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; @@ -1903,28 +1902,6 @@ 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($fullClassName)) { - 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($fullClassName)) { - throw MappingException::nonExistingClass($fullClassName); - } - $mapping['discriminatorMap'][$value] = $fullClassName; - } - } - if (isset($mapping['version'])) { $mapping['notSaved'] = true; $this->setVersionMapping($mapping); diff --git a/tests/Doctrine/ODM/MongoDB/Tests/DocumentManagerTest.php b/tests/Doctrine/ODM/MongoDB/Tests/DocumentManagerTest.php index 9a27c85255..d76fb4dfab 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/DocumentManagerTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/DocumentManagerTest.php @@ -256,7 +256,7 @@ class WrongSimpleRefDocument /** @ODM\Id */ public $id; - /** @ODM\ReferenceOne(targetDocument="Documents\Tournament\Participant", storeAs="id") */ + /** @ODM\ReferenceOne(targetDocument=Documents\Tournament\Participant::class, storeAs="id") */ public $ref; } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/AtomicSetTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/AtomicSetTest.php index 17505d6987..c2f4ace883 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/AtomicSetTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/AtomicSetTest.php @@ -561,13 +561,13 @@ class AtomicSetUser /** @ODM\Field(type="string") */ public $surname; - /** @ODM\EmbedMany(strategy="atomicSet", targetDocument="Documents\Phonenumber") */ + /** @ODM\EmbedMany(strategy="atomicSet", targetDocument=Documents\Phonenumber::class) */ public $phonenumbers; - /** @ODM\EmbedMany(strategy="atomicSetArray", targetDocument="Documents\Phonenumber") */ + /** @ODM\EmbedMany(strategy="atomicSetArray", targetDocument=Documents\Phonenumber::class) */ public $phonenumbersArray; - /** @ODM\EmbedMany(strategy="atomicSet", targetDocument="Documents\Phonebook") */ + /** @ODM\EmbedMany(strategy="atomicSet", targetDocument=Documents\Phonebook::class) */ public $phonebooks; /** @ODM\EmbedMany(strategy="atomicSet", targetDocument=AtomicSetInception::class) */ diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/DocumentPersisterTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/DocumentPersisterTest.php index c88938c224..67f36714e6 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/DocumentPersisterTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/DocumentPersisterTest.php @@ -603,11 +603,11 @@ class DocumentPersisterTestDocument /** * @ODM\EmbedOne( - * targetDocument="Doctrine\ODM\MongoDB\Tests\Functional\AbstractDocumentPersisterTestDocumentAssociation", + * targetDocument=Doctrine\ODM\MongoDB\Tests\Functional\AbstractDocumentPersisterTestDocumentAssociation::class, * discriminatorField="type", * discriminatorMap={ - * "reference"="Doctrine\ODM\MongoDB\Tests\Functional\DocumentPersisterTestDocumentReference", - * "embed"="Doctrine\ODM\MongoDB\Tests\Functional\DocumentPersisterTestDocumentEmbed" + * "reference"=Doctrine\ODM\MongoDB\Tests\Functional\DocumentPersisterTestDocumentReference::class, + * "embed"=Doctrine\ODM\MongoDB\Tests\Functional\DocumentPersisterTestDocumentEmbed::class * }, * name="associationName" * ) @@ -659,11 +659,11 @@ abstract class AbstractDocumentPersisterTestDocumentAssociation /** * @ODM\EmbedOne( - * targetDocument="Doctrine\ODM\MongoDB\Tests\Functional\AbstractDocumentPersisterTestDocumentAssociation", + * targetDocument=Doctrine\ODM\MongoDB\Tests\Functional\AbstractDocumentPersisterTestDocumentAssociation::class, * discriminatorField="type", * discriminatorMap={ - * "reference"="Doctrine\ODM\MongoDB\Tests\Functional\DocumentPersisterTestDocumentReference", - * "embed"="Doctrine\ODM\MongoDB\Tests\Functional\DocumentPersisterTestDocumentEmbed" + * "reference"=Doctrine\ODM\MongoDB\Tests\Functional\DocumentPersisterTestDocumentReference::class, + * "embed"=Doctrine\ODM\MongoDB\Tests\Functional\DocumentPersisterTestDocumentEmbed::class * }, * name="associationName" * ) diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/NestedCollectionsTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/NestedCollectionsTest.php index 42ce5b9fc9..bc19eac08d 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/NestedCollectionsTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/NestedCollectionsTest.php @@ -86,21 +86,21 @@ class DocWithNestedCollections /** @ODM\Id */ public $id; - /** @ODM\EmbedMany(strategy="atomicSet", targetDocument="Documents\Phonebook") */ + /** @ODM\EmbedMany(strategy="atomicSet", targetDocument=Documents\Phonebook::class) */ public $atomicSet; - /** @ODM\EmbedMany(strategy="atomicSetArray", targetDocument="Documents\Phonebook") */ + /** @ODM\EmbedMany(strategy="atomicSetArray", targetDocument=Documents\Phonebook::class) */ public $atomicSetArray; - /** @ODM\EmbedMany(strategy="set", targetDocument="Documents\Phonebook") */ + /** @ODM\EmbedMany(strategy="set", targetDocument=Documents\Phonebook::class) */ public $set; - /** @ODM\EmbedMany(strategy="setArray", targetDocument="Documents\Phonebook") */ + /** @ODM\EmbedMany(strategy="setArray", targetDocument=Documents\Phonebook::class) */ public $setArray; - /** @ODM\EmbedMany(strategy="pushAll", targetDocument="Documents\Phonebook") */ + /** @ODM\EmbedMany(strategy="pushAll", targetDocument=Documents\Phonebook::class) */ public $pushAll; - /** @ODM\EmbedMany(strategy="addToSet", targetDocument="Documents\Phonebook") */ + /** @ODM\EmbedMany(strategy="addToSet", targetDocument=Documents\Phonebook::class) */ public $addToSet; } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/TestTargetDocument.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/TestTargetDocument.php index a7fcf12b20..ed7c89e88a 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/TestTargetDocument.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/TestTargetDocument.php @@ -25,7 +25,7 @@ class TargetDocumentTestDocument /** @ODM\Id */ public $id; - /** @ODM\ReferenceOne(targetDocument="Doctrine\ODM\MongoDB\Tests\Functional\TargetDocumentTestReference") */ + /** @ODM\ReferenceOne(targetDocument=Doctrine\ODM\MongoDB\Tests\Functional\TargetDocumentTestReference::class) */ public $reference; } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH267Test.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH267Test.php index 8b7d0a24d6..d602ef0e72 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"="GH267SellerCompany", "buyer"="GH267BuyerCompany"}, inversedBy="users") */ + /** @ODM\ReferenceOne(name="company", targetDocument=GH267Company::class, discriminatorMap={"seller"=GH267SellerCompany::class, "buyer"=GH267BuyerCompany::class}, inversedBy="users") */ protected $company; public function __construct($name) diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH426Test.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH426Test.php index 01093d8671..fe0817ac7b 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH426Test.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH426Test.php @@ -51,7 +51,7 @@ class GH426Field /** @ODM\Id */ public $id; - /** @ODM\ReferenceOne(inversedBy="fields", discriminatorMap={"f":"GH426Form"}, discriminatorField="type", cascade={"all"}) */ + /** @ODM\ReferenceOne(inversedBy="fields", discriminatorMap={"f":GH426Form::class}, discriminatorField="type", cascade={"all"}) */ public $form; public function __construct(GH426Form $form) diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH788Test.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH788Test.php index 131cc8ae3c..e1dcbba06a 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH788Test.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH788Test.php @@ -261,7 +261,7 @@ class GH788Document * @ODM\EmbedMany( * discriminatorField="type", * discriminatorMap={ - * "b"="GH788InlineEmbedListed" + * "b"=GH788InlineEmbedListed::class * } * ) */ @@ -271,7 +271,7 @@ class GH788Document * @ODM\EmbedOne( * discriminatorField="type", * discriminatorMap={ - * "b"="GH788InlineEmbedListed" + * "b"=GH788InlineEmbedListed::class * } * ) */ @@ -281,7 +281,7 @@ class GH788Document * @ODM\ReferenceMany( * discriminatorField="type", * discriminatorMap={ - * "c"="GH788InlineRefListed" + * "c"=GH788InlineRefListed::class * }, * cascade="all" * ) @@ -292,7 +292,7 @@ class GH788Document * @ODM\ReferenceOne( * discriminatorField="type", * discriminatorMap={ - * "c"="GH788InlineRefListed" + * "c"=GH788InlineRefListed::class * }, * cascade="all" * ) diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH927Test.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH927Test.php index 76bf152cb0..458e3976a1 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH927Test.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH927Test.php @@ -25,7 +25,7 @@ class GH927Parent /** @ODM\Id */ protected $id; - /** @ODM\ReferenceOne(targetDocument="Doctrine\ODM\MongoDB\Tests\Functional\Ticket\GH927Reference") */ + /** @ODM\ReferenceOne(targetDocument=Doctrine\ODM\MongoDB\Tests\Functional\Ticket\GH927Reference::class) */ protected $reference; } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/MODM65Test.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/MODM65Test.php index 9d1481909e..3f22b3d264 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/MODM65Test.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/MODM65Test.php @@ -40,7 +40,7 @@ class MODM65User * @ODM\EmbedOne( * discriminatorField="php", * discriminatorMap={ - * "fbu"="Doctrine\ODM\MongoDB\Tests\Functional\Ticket\MODM65SocialNetworkUser" + * "fbu"=Doctrine\ODM\MongoDB\Tests\Functional\Ticket\MODM65SocialNetworkUser::class * }, * name="snu" * ) diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest.php index 6bc599a8d5..fd376535d3 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest.php @@ -819,9 +819,9 @@ class EmbeddedAssociationsCascadeTest /** @ODM\Id */ public $id; - /** @ODM\EmbedOne(targetDocument="Documents\Address") */ + /** @ODM\EmbedOne(targetDocument=Documents\Address::class) */ public $address; - /** @ODM\EmbedOne(targetDocument="Documents\Address") */ + /** @ODM\EmbedOne(targetDocument=Documents\Address::class) */ public $addresses; } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Mapping/Documents/GlobalNamespaceDocument.php b/tests/Doctrine/ODM/MongoDB/Tests/Mapping/Documents/GlobalNamespaceDocument.php index 607cff504f..aaaf576f3c 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Mapping/Documents/GlobalNamespaceDocument.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Mapping/Documents/GlobalNamespaceDocument.php @@ -21,7 +21,7 @@ class DoctrineGlobal_Article /** @ODM\ReferenceMany(targetDocument=DoctrineGlobal_User::class) */ protected $author; - /** @ODM\ReferenceMany(targetDocument="\DoctrineGlobal_User") */ + /** @ODM\ReferenceMany(targetDocument=\DoctrineGlobal_User::class) */ protected $editor; } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Query/BuilderTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Query/BuilderTest.php index 6e1016bc20..c1ce91c008 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Query/BuilderTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Query/BuilderTest.php @@ -942,16 +942,16 @@ class ParentClass */ class ChildA extends ParentClass { - /** @ODM\ReferenceOne(targetDocument="Documents\Feature") */ + /** @ODM\ReferenceOne(targetDocument=Documents\Feature::class) */ public $featureFull; - /** @ODM\ReferenceMany(targetDocument="Documents\Feature") */ + /** @ODM\ReferenceMany(targetDocument=Documents\Feature::class) */ public $featureFullMany; - /** @ODM\ReferenceOne(targetDocument="Documents\Feature") */ + /** @ODM\ReferenceOne(targetDocument=Documents\Feature::class) */ public $conflict; - /** @ODM\ReferenceMany(targetDocument="Documents\Feature") */ + /** @ODM\ReferenceMany(targetDocument=Documents\Feature::class) */ public $conflictMany; } @@ -960,16 +960,16 @@ class ChildA extends ParentClass */ class ChildB extends ParentClass { - /** @ODM\ReferenceOne(targetDocument="Documents\Feature", storeAs="id") */ + /** @ODM\ReferenceOne(targetDocument=Documents\Feature::class, storeAs="id") */ public $featureSimple; - /** @ODM\ReferenceMany(targetDocument="Documents\Feature", storeAs="id") */ + /** @ODM\ReferenceMany(targetDocument=Documents\Feature::class, storeAs="id") */ public $featureSimpleMany; - /** @ODM\ReferenceOne(targetDocument="Documents\Feature", storeAs="id") */ + /** @ODM\ReferenceOne(targetDocument=Documents\Feature::class, storeAs="id") */ public $conflict; - /** @ODM\ReferenceMany(targetDocument="Documents\Feature", storeAs="id") */ + /** @ODM\ReferenceMany(targetDocument=Documents\Feature::class, storeAs="id") */ public $conflictMany; } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php b/tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php index c4a9d533a8..3aa5f819c5 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php @@ -513,7 +513,7 @@ class Person /** @ODM\ReferenceOne(storeAs="dbRefWithDb") */ public $bestFriend; - /** @ODM\ReferenceOne(storeAs="id", targetDocument="Doctrine\ODM\MongoDB\Tests\Person") */ + /** @ODM\ReferenceOne(storeAs="id", targetDocument=Doctrine\ODM\MongoDB\Tests\Person::class) */ public $bestFriendSimple; /** @ODM\ReferenceOne */ @@ -522,7 +522,7 @@ class Person /** @ODM\ReferenceMany(storeAs="dbRefWithDb") */ public $friends = []; - /** @ODM\ReferenceMany(storeAs="id", targetDocument="Doctrine\ODM\MongoDB\Tests\Person") */ + /** @ODM\ReferenceMany(storeAs="id", targetDocument=Doctrine\ODM\MongoDB\Tests\Person::class) */ public $friendsSimple = []; /** @ODM\ReferenceMany */ @@ -540,7 +540,7 @@ public function __construct($firstName) /** @ODM\EmbeddedDocument */ class Pet { - /** @ODM\ReferenceOne(name="pO", targetDocument="Doctrine\ODM\MongoDB\Tests\Person") */ + /** @ODM\ReferenceOne(name="pO", targetDocument=Doctrine\ODM\MongoDB\Tests\Person::class) */ public $owner; /** @ODM\Field(type="string") */ @@ -556,18 +556,18 @@ public function __construct($name, Person $owner) /** @ODM\EmbeddedDocument */ class EmbedTest { - /** @ODM\EmbedOne(name="eO", targetDocument="Doctrine\ODM\MongoDB\Tests\EmbedTest") */ + /** @ODM\EmbedOne(name="eO", targetDocument=Doctrine\ODM\MongoDB\Tests\EmbedTest::class) */ public $embeddedOne; - /** @ODM\EmbedMany(name="e1", targetDocument="Doctrine\ODM\MongoDB\Tests\EmbedTest") */ + /** @ODM\EmbedMany(name="e1", targetDocument=Doctrine\ODM\MongoDB\Tests\EmbedTest::class) */ public $embeddedMany; /** @ODM\Field(name="n", type="string") */ public $name; - /** @ODM\ReferenceOne(name="p", targetDocument="Doctrine\ODM\MongoDB\Tests\Person") */ + /** @ODM\ReferenceOne(name="p", targetDocument=Doctrine\ODM\MongoDB\Tests\Person::class) */ public $person; - /** @ODM\EmbedOne(name="eP", targetDocument="Doctrine\ODM\MongoDB\Tests\Pet") */ + /** @ODM\EmbedOne(name="eP", targetDocument=Doctrine\ODM\MongoDB\Tests\Pet::class) */ public $pet; } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Tools/ResolveTargetDocumentListenerTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Tools/ResolveTargetDocumentListenerTest.php index cf03fea4a5..2b42663789 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Tools/ResolveTargetDocumentListenerTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Tools/ResolveTargetDocumentListenerTest.php @@ -70,16 +70,16 @@ class ResolveTargetDocument implements ResolveTargetInterface /** @ODM\Id */ private $id; - /** @ODM\ReferenceOne(targetDocument="Doctrine\ODM\MongoDB\Tests\Tools\ResolveTargetInterface") */ + /** @ODM\ReferenceOne(targetDocument=Doctrine\ODM\MongoDB\Tests\Tools\ResolveTargetInterface::class) */ private $refOne; - /** @ODM\ReferenceMany(targetDocument="Doctrine\ODM\MongoDB\Tests\Tools\TargetInterface") */ + /** @ODM\ReferenceMany(targetDocument=Doctrine\ODM\MongoDB\Tests\Tools\TargetInterface::class) */ private $refMany; - /** @ODM\EmbedOne(targetDocument="Doctrine\ODM\MongoDB\Tests\Tools\ResolveTargetInterface") */ + /** @ODM\EmbedOne(targetDocument=Doctrine\ODM\MongoDB\Tests\Tools\ResolveTargetInterface::class) */ private $embedOne; - /** @ODM\EmbedMany(targetDocument="Doctrine\ODM\MongoDB\Tests\Tools\TargetInterface") */ + /** @ODM\EmbedMany(targetDocument=Doctrine\ODM\MongoDB\Tests\Tools\TargetInterface::class) */ private $embedMany; public function getId() diff --git a/tests/Documents/Bars/Bar.php b/tests/Documents/Bars/Bar.php index a173f8f333..d5a85fe2de 100644 --- a/tests/Documents/Bars/Bar.php +++ b/tests/Documents/Bars/Bar.php @@ -15,7 +15,7 @@ class Bar /** @ODM\Field(type="string") */ private $name; - /** @ODM\EmbedMany(targetDocument="Documents\Bars\Location") */ + /** @ODM\EmbedMany(targetDocument=Documents\Bars\Location::class) */ private $locations = []; public function __construct($name = null) diff --git a/tests/Documents/Ecommerce/ConfigurableProduct.php b/tests/Documents/Ecommerce/ConfigurableProduct.php index 27981f7b79..dd04504992 100644 --- a/tests/Documents/Ecommerce/ConfigurableProduct.php +++ b/tests/Documents/Ecommerce/ConfigurableProduct.php @@ -22,7 +22,7 @@ class ConfigurableProduct /** @ODM\Field(type="string") */ protected $name; - /** @ODM\EmbedMany(targetDocument="Documents\Ecommerce\Option") */ + /** @ODM\EmbedMany(targetDocument=Documents\Ecommerce\Option::class) */ protected $options = []; /** @var Documents\Option */ diff --git a/tests/Documents/Ecommerce/Money.php b/tests/Documents/Ecommerce/Money.php index 816b26c5cc..c44afea6d7 100644 --- a/tests/Documents/Ecommerce/Money.php +++ b/tests/Documents/Ecommerce/Money.php @@ -13,7 +13,7 @@ class Money /** @ODM\Field(type="float") */ protected $amount; - /** @ODM\ReferenceOne(targetDocument="Documents\Ecommerce\Currency", cascade="all") */ + /** @ODM\ReferenceOne(targetDocument=Documents\Ecommerce\Currency::class, cascade="all") */ protected $currency; public function __construct($amount, Currency $currency) diff --git a/tests/Documents/Ecommerce/Option.php b/tests/Documents/Ecommerce/Option.php index 3e078a65f7..e8f7ef7083 100644 --- a/tests/Documents/Ecommerce/Option.php +++ b/tests/Documents/Ecommerce/Option.php @@ -23,14 +23,14 @@ class Option protected $name; /** - * @ODM\EmbedOne(targetDocument="Documents\Ecommerce\Money") + * @ODM\EmbedOne(targetDocument=Documents\Ecommerce\Money::class) * * @var float */ protected $money; /** - * @ODM\ReferenceOne(targetDocument="Documents\Ecommerce\StockItem", cascade="all") + * @ODM\ReferenceOne(targetDocument=Documents\Ecommerce\StockItem::class, cascade="all") * * @var Documents\StockItem */ diff --git a/tests/Documents/Ecommerce/StockItem.php b/tests/Documents/Ecommerce/StockItem.php index 7bb52c9a29..3bde510a85 100644 --- a/tests/Documents/Ecommerce/StockItem.php +++ b/tests/Documents/Ecommerce/StockItem.php @@ -20,7 +20,7 @@ class StockItem /** @ODM\Field(type="int") */ private $inventory; - /** @ODM\EmbedOne(targetDocument="Documents\Ecommerce\Money") */ + /** @ODM\EmbedOne(targetDocument=Documents\Ecommerce\Money::class) */ private $cost; public function getId() diff --git a/tests/Documents/Functional/FavoritesUser.php b/tests/Documents/Functional/FavoritesUser.php index 54617cfbca..1f15599d74 100644 --- a/tests/Documents/Functional/FavoritesUser.php +++ b/tests/Documents/Functional/FavoritesUser.php @@ -19,8 +19,8 @@ class FavoritesUser * @ODM\ReferenceMany( * discriminatorField="type", * discriminatorMap={ - * "group"="Documents\Group", - * "project"="Documents\Project" + * "group"=Documents\Group::class, + * "project"=Documents\Project::class * } * ) */ diff --git a/tests/Documents/Functional/VirtualHost.php b/tests/Documents/Functional/VirtualHost.php index db772def9c..ddf2667bee 100644 --- a/tests/Documents/Functional/VirtualHost.php +++ b/tests/Documents/Functional/VirtualHost.php @@ -12,7 +12,7 @@ class VirtualHost { /** @ODM\Id */ protected $id; - /** @ODM\EmbedOne(targetDocument="Documents\Functional\VirtualHostDirective") */ + /** @ODM\EmbedOne(targetDocument=Documents\Functional\VirtualHostDirective::class) */ protected $vhostDirective; public function getId() diff --git a/tests/Documents/Functional/VirtualHostDirective.php b/tests/Documents/Functional/VirtualHostDirective.php index 49c26a33f3..e0635d4827 100644 --- a/tests/Documents/Functional/VirtualHostDirective.php +++ b/tests/Documents/Functional/VirtualHostDirective.php @@ -17,7 +17,7 @@ class VirtualHostDirective protected $name; /** @ODM\Field(type="string") */ protected $value; - /** @ODM\EmbedMany(targetDocument="Documents\Functional\VirtualHostDirective") */ + /** @ODM\EmbedMany(targetDocument=Documents\Functional\VirtualHostDirective::class) */ protected $directives; diff --git a/tests/Documents/ReferenceUser.php b/tests/Documents/ReferenceUser.php index d7a686f93e..4a3c4b117e 100644 --- a/tests/Documents/ReferenceUser.php +++ b/tests/Documents/ReferenceUser.php @@ -73,7 +73,7 @@ class ReferenceUser public $referencedUsers = []; /** - * @ODM\EmbedMany(targetDocument="Documents\IndirectlyReferencedUser") + * @ODM\EmbedMany(targetDocument=Documents\IndirectlyReferencedUser::class) * * @var IndirectlyReferencedUser[] */ From b4162dc971faa099cbaa35d28b02c093f0fc7368 Mon Sep 17 00:00:00 2001 From: watari Date: Wed, 28 Nov 2018 12:06:46 +0200 Subject: [PATCH 3/3] Updated docs according to suggestions from review. --- docs/en/reference/annotations-reference.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/en/reference/annotations-reference.rst b/docs/en/reference/annotations-reference.rst index 32318a11eb..8f3c1d9cb0 100644 --- a/docs/en/reference/annotations-reference.rst +++ b/docs/en/reference/annotations-reference.rst @@ -115,11 +115,11 @@ during hydration to select the instantiation class. This annotation is required for the top-most class in a :ref:`single collection inheritance ` hierarchy. It takes an array as its only argument, which maps keys to class names. The -class constant `::class` must be used as source of class name. When +class names must be fully qualified. Using the ``::class constant`` is supported. When a document is persisted to the database, its class name key will be stored in -the discriminator field instead of the |FQCN|. If class of persisted to the -database document is not registered in discriminator map and discriminator map -is not empty - ``\Doctrine\ODM\MongoDB\Mapping\MappingException`` will be thrown. +the discriminator field instead of the |FQCN|. If the discriminator map is non-empty +and it does not contain the class name of the persisted document, a +``\Doctrine\ODM\MongoDB\Mapping\MappingException`` will be thrown. .. code-block:: php