Skip to content

Commit

Permalink
Set type maps on collection level (#2288)
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus authored Feb 28, 2021
1 parent fe3913c commit b0989bc
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 41 deletions.
16 changes: 1 addition & 15 deletions lib/Doctrine/ODM/MongoDB/DocumentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,13 @@ protected function __construct(?Client $client = null, ?Configuration $config =
'mongodb://127.0.0.1',
[],
[
'typeMap' => self::CLIENT_TYPEMAP,
'driver' => [
'name' => 'doctrine-odm',
'version' => self::getVersion(),
],
]
);

$this->checkTypeMap();

$metadataFactoryClassName = $this->config->getClassMetadataFactoryName();
$this->metadataFactory = new $metadataFactoryClassName();
$this->metadataFactory->setDocumentManager($this);
Expand Down Expand Up @@ -360,7 +357,7 @@ public function getDocumentCollection(string $className): Collection
if (! isset($this->documentCollections[$className])) {
$db = $this->getDocumentDatabase($className);

$options = [];
$options = ['typeMap' => self::CLIENT_TYPEMAP];
if ($metadata->readPreference !== null) {
$options['readPreference'] = new ReadPreference($metadata->readPreference, $metadata->readPreferenceTags);
}
Expand Down Expand Up @@ -867,17 +864,6 @@ public function getFilterCollection(): FilterCollection
return $this->filterCollection;
}

private function checkTypeMap(): void
{
$typeMap = $this->client->getTypeMap();

foreach (self::CLIENT_TYPEMAP as $part => $expectedType) {
if (! isset($typeMap[$part]) || $typeMap[$part] !== $expectedType) {
throw MongoDBException::invalidTypeMap($part, $expectedType);
}
}
}

private static function getVersion(): string
{
if (self::$version === null) {
Expand Down
24 changes: 0 additions & 24 deletions tests/Doctrine/ODM/MongoDB/Tests/DocumentManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Doctrine\Common\EventManager;
use Doctrine\ODM\MongoDB\Aggregation\Builder as AggregationBuilder;
use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory;
Expand Down Expand Up @@ -231,29 +230,6 @@ public function testDifferentStoreAsDbReferences()
$this->assertCount(1, $dbRef);
$this->assertArrayHasKey('id', $dbRef);
}

/**
* @dataProvider dataInvalidTypeMap
*/
public function testThrowsExceptionOnInvalidTypeMap(string $expectedMessage, Client $client): void
{
$this->expectException(MongoDBException::class);
$this->expectExceptionMessage($expectedMessage);
DocumentManager::create($client);
}

public function dataInvalidTypeMap(): array
{
$noTypeMap = new Client();
$invalidDocumentTypeMap = new Client('mongodb://127.0.0.1', [], ['typeMap' => ['root' => 'array', 'document' => stdClass::class]]);
$invalidRootTypeMap = new Client('mongodb://127.0.0.1', [], ['typeMap' => ['root' => stdClass::class, 'document' => 'array']]);

return [
'No typeMap' => ['Invalid typemap provided. Type "array" is required for "root".', $noTypeMap],
'Invalid document' => ['Invalid typemap provided. Type "array" is required for "document".', $invalidDocumentTypeMap],
'Invalid root' => ['Invalid typemap provided. Type "array" is required for "root".', $invalidRootTypeMap],
];
}
}

/** @ODM\Document */
Expand Down
3 changes: 1 addition & 2 deletions tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ public function setUp(): void
{
parent::setUp();

$client = $this->createMock(Client::class);
$client->method('getTypeMap')->willReturn(DocumentManager::CLIENT_TYPEMAP);
$client = $this->createMock(Client::class);
$this->dm = DocumentManager::create($client, $this->dm->getConfiguration(), $this->createMock(EventManager::class));

foreach ($this->dm->getMetadataFactory()->getAllMetadata() as $cm) {
Expand Down

0 comments on commit b0989bc

Please sign in to comment.