Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bucket typemap #2600

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/DocumentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Doctrine/ODM/MongoDB/Tests/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,22 @@ 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'];
}

/** @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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']);
Expand All @@ -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']);
Expand All @@ -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']);
}
Expand All @@ -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']);
}
Expand All @@ -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']);
}
Expand All @@ -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']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> $className
*
Expand Down
Loading