Skip to content

Commit

Permalink
Merge pull request #2602 from doctrine/2.6.x-merge-up-into-2.7.x_xIl3…
Browse files Browse the repository at this point in the history
…t7Dt

Merge release 2.6.2 into 2.7.x
  • Loading branch information
alcaeus authored Dec 19, 2023
2 parents 3171964 + a51bfc0 commit bec87ea
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 20 deletions.
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
13 changes: 5 additions & 8 deletions lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@
use function strtoupper;
use function trigger_deprecation;

use const PHP_VERSION_ID;

/**
* A <tt>ClassMetadata</tt> instance holds all the object-document mapping metadata
* of a document and it's references.
Expand Down Expand Up @@ -2616,17 +2614,16 @@ private function validateAndCompleteTypedFieldMapping(array $mapping): array
return $mapping;
}

if (PHP_VERSION_ID >= 80100 && ! $type->isBuiltin() && enum_exists($type->getName())) {
$mapping['enumType'] = $type->getName();

if (! $type->isBuiltin() && enum_exists($type->getName())) {
$reflection = new ReflectionEnum($type->getName());
$type = $reflection->getBackingType();

if ($type === null) {
throw MappingException::nonBackedEnumMapped($this->name, $mapping['fieldName'], $mapping['enumType']);
if (! $reflection->isBacked()) {
throw MappingException::nonBackedEnumMapped($this->name, $mapping['fieldName'], $reflection->getName());
}

$type = $reflection->getBackingType();
assert($type instanceof ReflectionNamedType);
$mapping['enumType'] = $reflection->getName();
}

switch ($type->getName()) {
Expand Down
10 changes: 10 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,16 @@ parameters:
count: 1
path: lib/Doctrine/ODM/MongoDB/Hydrator/HydratorFactory.php

-
message: "#^Method Doctrine\\\\ODM\\\\MongoDB\\\\Mapping\\\\ClassMetadata\\:\\:validateAndCompleteTypedFieldMapping\\(\\) should return array\\{type\\?\\: string, fieldName\\?\\: string, name\\?\\: string, strategy\\?\\: string, association\\?\\: int, id\\?\\: bool, isOwningSide\\?\\: bool, collectionClass\\?\\: class\\-string, \\.\\.\\.\\} but returns array\\{type\\?\\: string, fieldName\\?\\: string, name\\?\\: string, strategy\\?\\: string, association\\?\\: int, id\\?\\: bool, isOwningSide\\?\\: bool, collectionClass\\?\\: class\\-string, \\.\\.\\.\\}\\.$#"
count: 1
path: lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php

-
message: "#^Parameter \\#2 \\$enumType of class Doctrine\\\\Persistence\\\\Reflection\\\\EnumReflectionProperty constructor expects class\\-string\\<BackedEnum\\>, class\\-string\\<UnitEnum\\> given\\.$#"
count: 1
path: lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php

-
message: "#^Parameter \\#2 \\$mapping of method Doctrine\\\\ODM\\\\MongoDB\\\\Mapping\\\\Driver\\\\XmlDriver\\:\\:addFieldMapping\\(\\) expects array\\{type\\?\\: string, fieldName\\?\\: string, name\\?\\: string, strategy\\?\\: string, association\\?\\: int, id\\?\\: bool, isOwningSide\\?\\: bool, collectionClass\\?\\: class\\-string, \\.\\.\\.\\}, array\\<string, non\\-empty\\-array\\<int, string\\>\\|bool\\|string\\> given\\.$#"
count: 1
Expand Down
3 changes: 2 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.15.0@5c774aca4746caf3d239d9c8cadb9f882ca29352">
<files psalm-version="5.17.0@c620f6e80d0abfca532b00bda366062aaedf6e5d">
<file src="lib/Doctrine/ODM/MongoDB/Aggregation/Aggregation.php">
<MissingTemplateParam>
<code>IteratorAggregate</code>
Expand Down Expand Up @@ -82,6 +82,7 @@
<file src="lib/Doctrine/ODM/MongoDB/Mapping/Driver/AnnotationDriver.php">
<InvalidArgument>
<code>$mapping</code>
<code>$options</code>
</InvalidArgument>
</file>
<file src="lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php">
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

0 comments on commit bec87ea

Please sign in to comment.