Skip to content

Commit a935640

Browse files
JJarrieIonBazan
andauthored
Schema:create fails with single collection inheritance (#2392)
* Schema:create fails with single collection inheritance * add test * Add assertion of inheritance of single collection type for Tournament class Co-authored-by: Ion Bazan <[email protected]>
1 parent 008030f commit a935640

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

lib/Doctrine/ODM/MongoDB/SchemaManager.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,12 +390,22 @@ public function updateDocumentValidator(string $documentName, ?int $maxTimeMs =
390390
*/
391391
public function createCollections(?int $maxTimeMs = null, ?WriteConcern $writeConcern = null): void
392392
{
393+
$singleInheritanceProcessed = [];
394+
393395
foreach ($this->metadataFactory->getAllMetadata() as $class) {
394396
assert($class instanceof ClassMetadata);
395397
if ($class->isMappedSuperclass || $class->isEmbeddedDocument || $class->isQueryResultDocument) {
396398
continue;
397399
}
398400

401+
if ($class->inheritanceType === ClassMetadata::INHERITANCE_TYPE_SINGLE_COLLECTION) {
402+
if (in_array($class->collection, $singleInheritanceProcessed)) {
403+
continue;
404+
}
405+
406+
$singleInheritanceProcessed[] = $class->collection;
407+
}
408+
399409
$this->createDocumentCollection($class->name, $maxTimeMs, $writeConcern);
400410
}
401411
}

tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Documents\Sharded\ShardedOne;
2121
use Documents\Sharded\ShardedOneWithDifferentKey;
2222
use Documents\SimpleReferenceUser;
23+
use Documents\Tournament\Tournament;
2324
use Documents\UserName;
2425
use InvalidArgumentException;
2526
use MongoDB\Client;
@@ -37,6 +38,7 @@
3738
use PHPUnit\Framework\Constraint\IsEqual;
3839
use PHPUnit\Framework\MockObject\MockObject;
3940

41+
use function array_count_values;
4042
use function array_map;
4143
use function assert;
4244
use function class_exists;
@@ -608,11 +610,18 @@ public function testCreateView(array $expectedWriteOptions, ?int $maxTimeMs, ?Wr
608610
*/
609611
public function testCreateCollections(array $expectedWriteOptions, ?int $maxTimeMs, ?WriteConcern $writeConcern): void
610612
{
611-
foreach ($this->documentDatabases as $class => $database) {
613+
$class = $this->dm->getClassMetadata(Tournament::class);
614+
self::assertSame(ClassMetadata::INHERITANCE_TYPE_SINGLE_COLLECTION, $class->inheritanceType);
615+
616+
$createdCollections = [];
617+
foreach ($this->documentDatabases as $database) {
612618
$database
613619
->expects($this->atLeastOnce())
614620
->method('createCollection')
615-
->with($this->anything(), $this->writeOptions($expectedWriteOptions));
621+
->with($this->anything(), $this->writeOptions($expectedWriteOptions))
622+
->willReturnCallback(static function (string $collectionName) use (&$createdCollections): void {
623+
$createdCollections[] = $collectionName;
624+
});
616625

617626
$database
618627
->expects($this->atLeastOnce())
@@ -621,6 +630,7 @@ public function testCreateCollections(array $expectedWriteOptions, ?int $maxTime
621630
}
622631

623632
$this->schemaManager->createCollections($maxTimeMs, $writeConcern);
633+
self::assertSame(1, array_count_values($createdCollections)['Tournament']);
624634
}
625635

626636
/**

0 commit comments

Comments
 (0)