Skip to content

Commit 2996c0a

Browse files
authored
Fix bucket typemap (#2600)
* Remove typeMap from DocumentManager in tests * Use correct typeMap when fetching GridFS buckets
1 parent fc236da commit 2996c0a

File tree

4 files changed

+27
-11
lines changed

4 files changed

+27
-11
lines changed

lib/Doctrine/ODM/MongoDB/DocumentManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ public function getDocumentBucket(string $className): Bucket
389389
if (! isset($this->documentBuckets[$className])) {
390390
$db = $this->getDocumentDatabase($className);
391391

392-
$options = ['bucketName' => $bucketName];
392+
$options = ['bucketName' => $bucketName, 'typeMap' => self::CLIENT_TYPEMAP];
393393
if ($metadata->readPreference !== null) {
394394
$options['readPreference'] = new ReadPreference($metadata->readPreference, $metadata->readPreferenceTags);
395395
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,22 +108,22 @@ protected static function createMetadataDriverImpl(): MappingDriver
108108
protected static function createTestDocumentManager(): DocumentManager
109109
{
110110
$config = static::getConfiguration();
111-
$client = new Client(getenv('DOCTRINE_MONGODB_SERVER') ?: DOCTRINE_MONGODB_SERVER, [], ['typeMap' => ['root' => 'array', 'document' => 'array']]);
111+
$client = new Client(getenv('DOCTRINE_MONGODB_SERVER') ?: DOCTRINE_MONGODB_SERVER);
112112

113113
return DocumentManager::create($client, $config);
114114
}
115115

116116
protected function getServerVersion(): string
117117
{
118-
$result = $this->dm->getClient()->selectDatabase(DOCTRINE_MONGODB_DATABASE)->command(['buildInfo' => 1])->toArray()[0];
118+
$result = $this->dm->getClient()->selectDatabase(DOCTRINE_MONGODB_DATABASE)->command(['buildInfo' => 1], ['typeMap' => DocumentManager::CLIENT_TYPEMAP])->toArray()[0];
119119

120120
return $result['version'];
121121
}
122122

123123
/** @psalm-param class-string $className */
124124
protected function skipTestIfNotSharded(string $className): void
125125
{
126-
$result = $this->dm->getDocumentDatabase($className)->command(['listCommands' => true])->toArray()[0];
126+
$result = $this->dm->getDocumentDatabase($className)->command(['listCommands' => true], ['typeMap' => DocumentManager::CLIENT_TYPEMAP])->toArray()[0];
127127

128128
if (array_key_exists('shardCollection', $result['commands'])) {
129129
return;
@@ -135,7 +135,7 @@ protected function skipTestIfNotSharded(string $className): void
135135
/** @psalm-param class-string $className */
136136
protected function skipTestIfSharded(string $className): void
137137
{
138-
$result = $this->dm->getDocumentDatabase($className)->command(['listCommands' => true])->toArray()[0];
138+
$result = $this->dm->getDocumentDatabase($className)->command(['listCommands' => true], ['typeMap' => DocumentManager::CLIENT_TYPEMAP])->toArray()[0];
139139

140140
if (! array_key_exists('shardCollection', $result['commands'])) {
141141
return;

tests/Doctrine/ODM/MongoDB/Tests/Functional/EnsureShardingTest.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Doctrine\ODM\MongoDB\Tests\Functional;
66

7+
use Doctrine\ODM\MongoDB\DocumentManager;
78
use Doctrine\ODM\MongoDB\MongoDBException;
89
use Doctrine\ODM\MongoDB\Tests\BaseTestCase;
910
use Documents\Sharded\ShardedByUser;
@@ -31,7 +32,7 @@ public function testEnsureShardingForNewCollection(): void
3132

3233
$collection = $this->dm->getDocumentCollection($class);
3334
$indexes = iterator_to_array($collection->listIndexes());
34-
$stats = $this->dm->getDocumentDatabase($class)->command(['collstats' => $collection->getCollectionName()])->toArray()[0];
35+
$stats = $this->dm->getDocumentDatabase($class)->command(['collstats' => $collection->getCollectionName()], ['typeMap' => DocumentManager::CLIENT_TYPEMAP])->toArray()[0];
3536

3637
self::assertCount(2, $indexes);
3738
self::assertSame(['k' => 1], $indexes[1]['key']);
@@ -45,7 +46,7 @@ public function testEnsureShardingForNewCollectionWithoutCreatingIndexes(): void
4546

4647
$collection = $this->dm->getDocumentCollection($class);
4748
$indexes = iterator_to_array($collection->listIndexes());
48-
$stats = $this->dm->getDocumentDatabase($class)->command(['collstats' => $collection->getCollectionName()])->toArray()[0];
49+
$stats = $this->dm->getDocumentDatabase($class)->command(['collstats' => $collection->getCollectionName()], ['typeMap' => DocumentManager::CLIENT_TYPEMAP])->toArray()[0];
4950

5051
self::assertCount(2, $indexes);
5152
self::assertSame(['k' => 1], $indexes[1]['key']);
@@ -64,7 +65,7 @@ public function testEnsureShardingForCollectionWithDocuments(): void
6465
$this->dm->getSchemaManager()->ensureDocumentSharding($class);
6566

6667
$collection = $this->dm->getDocumentCollection($class);
67-
$stats = $this->dm->getDocumentDatabase($class)->command(['collstats' => $collection->getCollectionName()])->toArray()[0];
68+
$stats = $this->dm->getDocumentDatabase($class)->command(['collstats' => $collection->getCollectionName()], ['typeMap' => DocumentManager::CLIENT_TYPEMAP])->toArray()[0];
6869

6970
self::assertTrue($stats['sharded']);
7071
}
@@ -83,7 +84,7 @@ public function testEnsureShardingForCollectionWithDocumentsThrowsIndexError():
8384
$this->dm->getSchemaManager()->ensureDocumentSharding($class);
8485

8586
$collection = $this->dm->getDocumentCollection($class);
86-
$stats = $this->dm->getDocumentDatabase($class)->command(['collstats' => $collection->getCollectionName()])->toArray()[0];
87+
$stats = $this->dm->getDocumentDatabase($class)->command(['collstats' => $collection->getCollectionName()], ['typeMap' => DocumentManager::CLIENT_TYPEMAP])->toArray()[0];
8788

8889
self::assertFalse($stats['sharded']);
8990
}
@@ -97,7 +98,7 @@ public function testEnsureShardingForCollectionWithShardingEnabled(): void
9798
$this->dm->getSchemaManager()->ensureDocumentSharding(ShardedOne::class);
9899

99100
$collection = $this->dm->getDocumentCollection($class);
100-
$stats = $this->dm->getDocumentDatabase($class)->command(['collstats' => $collection->getCollectionName()])->toArray()[0];
101+
$stats = $this->dm->getDocumentDatabase($class)->command(['collstats' => $collection->getCollectionName()], ['typeMap' => DocumentManager::CLIENT_TYPEMAP])->toArray()[0];
101102

102103
self::assertTrue($stats['sharded']);
103104
}
@@ -110,7 +111,7 @@ public function testEnsureDocumentShardingWithShardByReference(): void
110111
$this->dm->getSchemaManager()->ensureDocumentSharding($class);
111112

112113
$collection = $this->dm->getDocumentCollection($class);
113-
$stats = $this->dm->getDocumentDatabase($class)->command(['collstats' => $collection->getCollectionName()])->toArray()[0];
114+
$stats = $this->dm->getDocumentDatabase($class)->command(['collstats' => $collection->getCollectionName()], ['typeMap' => DocumentManager::CLIENT_TYPEMAP])->toArray()[0];
114115
$indexes = iterator_to_array($collection->listIndexes());
115116

116117
self::assertTrue($stats['sharded']);

tests/Doctrine/ODM/MongoDB/Tests/Repository/DefaultGridFSRepositoryTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,21 @@ public function testUploadFileWithoutChunkSize(): void
279279
self::assertSame(261120, $file->getChunkSize());
280280
}
281281

282+
public function testReadingFileWithMetadata(): void
283+
{
284+
$uploadOptions = new UploadOptions();
285+
$uploadOptions->metadata = new FileMetadata();
286+
$uploadOptions->metadata->getEmbedOne()->name = 'foo';
287+
288+
$file = $this->getRepository()->uploadFromFile(__FILE__, uploadOptions: $uploadOptions);
289+
$this->dm->detach($file);
290+
291+
$retrievedFile = $this->getRepository()->find($file->getId());
292+
self::assertInstanceOf(File::class, $retrievedFile);
293+
self::assertInstanceOf(FileMetadata::class, $retrievedFile->getMetadata());
294+
self::assertSame('foo', $retrievedFile->getMetadata()->getEmbedOne()->name);
295+
}
296+
282297
/**
283298
* @param class-string<T> $className
284299
*

0 commit comments

Comments
 (0)