From 180b2fde4580a374d27fc2d76cbb3752d7949e8c Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Thu, 14 Dec 2023 13:44:56 +0100 Subject: [PATCH 1/2] Remove typeMap from DocumentManager in tests --- tests/Doctrine/ODM/MongoDB/Tests/BaseTestCase.php | 8 ++++---- .../MongoDB/Tests/Functional/EnsureShardingTest.php | 13 +++++++------ 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/tests/Doctrine/ODM/MongoDB/Tests/BaseTestCase.php b/tests/Doctrine/ODM/MongoDB/Tests/BaseTestCase.php index ad5fe91ca8..9941e7f284 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/BaseTestCase.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/BaseTestCase.php @@ -108,14 +108,14 @@ protected static function createMetadataDriverImpl(): MappingDriver protected static function createTestDocumentManager(): DocumentManager { $config = static::getConfiguration(); - $client = new Client(getenv('DOCTRINE_MONGODB_SERVER') ?: DOCTRINE_MONGODB_SERVER, [], ['typeMap' => ['root' => 'array', 'document' => 'array']]); + $client = new Client(getenv('DOCTRINE_MONGODB_SERVER') ?: DOCTRINE_MONGODB_SERVER); return DocumentManager::create($client, $config); } protected function getServerVersion(): string { - $result = $this->dm->getClient()->selectDatabase(DOCTRINE_MONGODB_DATABASE)->command(['buildInfo' => 1])->toArray()[0]; + $result = $this->dm->getClient()->selectDatabase(DOCTRINE_MONGODB_DATABASE)->command(['buildInfo' => 1], ['typeMap' => DocumentManager::CLIENT_TYPEMAP])->toArray()[0]; return $result['version']; } @@ -123,7 +123,7 @@ protected function getServerVersion(): string /** @psalm-param class-string $className */ protected function skipTestIfNotSharded(string $className): void { - $result = $this->dm->getDocumentDatabase($className)->command(['listCommands' => true])->toArray()[0]; + $result = $this->dm->getDocumentDatabase($className)->command(['listCommands' => true], ['typeMap' => DocumentManager::CLIENT_TYPEMAP])->toArray()[0]; if (array_key_exists('shardCollection', $result['commands'])) { return; @@ -135,7 +135,7 @@ protected function skipTestIfNotSharded(string $className): void /** @psalm-param class-string $className */ protected function skipTestIfSharded(string $className): void { - $result = $this->dm->getDocumentDatabase($className)->command(['listCommands' => true])->toArray()[0]; + $result = $this->dm->getDocumentDatabase($className)->command(['listCommands' => true], ['typeMap' => DocumentManager::CLIENT_TYPEMAP])->toArray()[0]; if (! array_key_exists('shardCollection', $result['commands'])) { return; diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/EnsureShardingTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/EnsureShardingTest.php index e4b3061b76..dd047c5e60 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/EnsureShardingTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/EnsureShardingTest.php @@ -4,6 +4,7 @@ namespace Doctrine\ODM\MongoDB\Tests\Functional; +use Doctrine\ODM\MongoDB\DocumentManager; use Doctrine\ODM\MongoDB\MongoDBException; use Doctrine\ODM\MongoDB\Tests\BaseTestCase; use Documents\Sharded\ShardedByUser; @@ -31,7 +32,7 @@ public function testEnsureShardingForNewCollection(): void $collection = $this->dm->getDocumentCollection($class); $indexes = iterator_to_array($collection->listIndexes()); - $stats = $this->dm->getDocumentDatabase($class)->command(['collstats' => $collection->getCollectionName()])->toArray()[0]; + $stats = $this->dm->getDocumentDatabase($class)->command(['collstats' => $collection->getCollectionName()], ['typeMap' => DocumentManager::CLIENT_TYPEMAP])->toArray()[0]; self::assertCount(2, $indexes); self::assertSame(['k' => 1], $indexes[1]['key']); @@ -45,7 +46,7 @@ public function testEnsureShardingForNewCollectionWithoutCreatingIndexes(): void $collection = $this->dm->getDocumentCollection($class); $indexes = iterator_to_array($collection->listIndexes()); - $stats = $this->dm->getDocumentDatabase($class)->command(['collstats' => $collection->getCollectionName()])->toArray()[0]; + $stats = $this->dm->getDocumentDatabase($class)->command(['collstats' => $collection->getCollectionName()], ['typeMap' => DocumentManager::CLIENT_TYPEMAP])->toArray()[0]; self::assertCount(2, $indexes); self::assertSame(['k' => 1], $indexes[1]['key']); @@ -64,7 +65,7 @@ public function testEnsureShardingForCollectionWithDocuments(): void $this->dm->getSchemaManager()->ensureDocumentSharding($class); $collection = $this->dm->getDocumentCollection($class); - $stats = $this->dm->getDocumentDatabase($class)->command(['collstats' => $collection->getCollectionName()])->toArray()[0]; + $stats = $this->dm->getDocumentDatabase($class)->command(['collstats' => $collection->getCollectionName()], ['typeMap' => DocumentManager::CLIENT_TYPEMAP])->toArray()[0]; self::assertTrue($stats['sharded']); } @@ -83,7 +84,7 @@ public function testEnsureShardingForCollectionWithDocumentsThrowsIndexError(): $this->dm->getSchemaManager()->ensureDocumentSharding($class); $collection = $this->dm->getDocumentCollection($class); - $stats = $this->dm->getDocumentDatabase($class)->command(['collstats' => $collection->getCollectionName()])->toArray()[0]; + $stats = $this->dm->getDocumentDatabase($class)->command(['collstats' => $collection->getCollectionName()], ['typeMap' => DocumentManager::CLIENT_TYPEMAP])->toArray()[0]; self::assertFalse($stats['sharded']); } @@ -97,7 +98,7 @@ public function testEnsureShardingForCollectionWithShardingEnabled(): void $this->dm->getSchemaManager()->ensureDocumentSharding(ShardedOne::class); $collection = $this->dm->getDocumentCollection($class); - $stats = $this->dm->getDocumentDatabase($class)->command(['collstats' => $collection->getCollectionName()])->toArray()[0]; + $stats = $this->dm->getDocumentDatabase($class)->command(['collstats' => $collection->getCollectionName()], ['typeMap' => DocumentManager::CLIENT_TYPEMAP])->toArray()[0]; self::assertTrue($stats['sharded']); } @@ -110,7 +111,7 @@ public function testEnsureDocumentShardingWithShardByReference(): void $this->dm->getSchemaManager()->ensureDocumentSharding($class); $collection = $this->dm->getDocumentCollection($class); - $stats = $this->dm->getDocumentDatabase($class)->command(['collstats' => $collection->getCollectionName()])->toArray()[0]; + $stats = $this->dm->getDocumentDatabase($class)->command(['collstats' => $collection->getCollectionName()], ['typeMap' => DocumentManager::CLIENT_TYPEMAP])->toArray()[0]; $indexes = iterator_to_array($collection->listIndexes()); self::assertTrue($stats['sharded']); From b92572df51beb13fc00a08049628783f45c2926a Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Thu, 14 Dec 2023 13:45:27 +0100 Subject: [PATCH 2/2] Use correct typeMap when fetching GridFS buckets --- lib/Doctrine/ODM/MongoDB/DocumentManager.php | 2 +- .../Repository/DefaultGridFSRepositoryTest.php | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ODM/MongoDB/DocumentManager.php b/lib/Doctrine/ODM/MongoDB/DocumentManager.php index bc26274a09..376ef9327f 100644 --- a/lib/Doctrine/ODM/MongoDB/DocumentManager.php +++ b/lib/Doctrine/ODM/MongoDB/DocumentManager.php @@ -389,7 +389,7 @@ public function getDocumentBucket(string $className): Bucket if (! isset($this->documentBuckets[$className])) { $db = $this->getDocumentDatabase($className); - $options = ['bucketName' => $bucketName]; + $options = ['bucketName' => $bucketName, 'typeMap' => self::CLIENT_TYPEMAP]; if ($metadata->readPreference !== null) { $options['readPreference'] = new ReadPreference($metadata->readPreference, $metadata->readPreferenceTags); } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Repository/DefaultGridFSRepositoryTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Repository/DefaultGridFSRepositoryTest.php index d1f439792a..51ba3d519a 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Repository/DefaultGridFSRepositoryTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Repository/DefaultGridFSRepositoryTest.php @@ -279,6 +279,21 @@ public function testUploadFileWithoutChunkSize(): void self::assertSame(261120, $file->getChunkSize()); } + public function testReadingFileWithMetadata(): void + { + $uploadOptions = new UploadOptions(); + $uploadOptions->metadata = new FileMetadata(); + $uploadOptions->metadata->getEmbedOne()->name = 'foo'; + + $file = $this->getRepository()->uploadFromFile(__FILE__, uploadOptions: $uploadOptions); + $this->dm->detach($file); + + $retrievedFile = $this->getRepository()->find($file->getId()); + self::assertInstanceOf(File::class, $retrievedFile); + self::assertInstanceOf(FileMetadata::class, $retrievedFile->getMetadata()); + self::assertSame('foo', $retrievedFile->getMetadata()->getEmbedOne()->name); + } + /** * @param class-string $className *