Skip to content

Commit 77b28b3

Browse files
authored
Support version 2.0 of the MongoDB driver (#2683)
1 parent 2680203 commit 77b28b3

16 files changed

+181
-72
lines changed

.github/workflows/continuous-integration.yml

+9-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ jobs:
1616
runs-on: "ubuntu-20.04"
1717

1818
strategy:
19+
fail-fast: false
1920
matrix:
2021
php-version:
2122
- "8.1"
@@ -41,7 +42,7 @@ jobs:
4142
- dependencies: "lowest"
4243
php-version: "8.1"
4344
mongodb-version: "5.0"
44-
driver-version: "1.17.0"
45+
driver-version: "1.21.0"
4546
topology: "server"
4647
symfony-version: "stable"
4748
proxy: "lazy-ghost"
@@ -68,6 +69,13 @@ jobs:
6869
dependencies: "highest"
6970
symfony-version: "stable"
7071
proxy: "proxy-manager"
72+
# Test with ext-2.0
73+
- topology: "server"
74+
php-version: "8.2"
75+
mongodb-version: "7.0"
76+
driver-version: "mongodb/[email protected]"
77+
dependencies: "highest"
78+
symfony-version: "7"
7179
# Test with a 5.0 sharded cluster
7280
# Currently disabled due to a bug where MongoDB reports "sharding status unknown"
7381
# - topology: "sharded_cluster"

benchmark/BaseBench.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ public function clearDatabase(): void
6868
return;
6969
}
7070

71-
$collections = $client->selectDatabase(self::DATABASE_NAME)->listCollections();
71+
$collections = $client->getDatabase(self::DATABASE_NAME)->listCollections();
7272

7373
foreach ($collections as $collection) {
7474
// See https://jira.mongodb.org/browse/SERVER-16541
7575
if ($collection->getName() === 'system.indexes') {
7676
continue;
7777
}
7878

79-
$client->selectCollection(self::DATABASE_NAME, $collection->getName())->drop();
79+
$client->getCollection(self::DATABASE_NAME, $collection->getName())->drop();
8080
}
8181
}
8282

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@
2222
],
2323
"require": {
2424
"php": "^8.1",
25-
"ext-mongodb": "^1.17",
25+
"ext-mongodb": "^1.21 || ^2.0",
2626
"doctrine/cache": "^1.11 || ^2.0",
2727
"doctrine/collections": "^1.5 || ^2.0",
2828
"doctrine/event-manager": "^1.0 || ^2.0",
2929
"doctrine/instantiator": "^1.1 || ^2",
3030
"doctrine/persistence": "^3.2 || ^4",
3131
"friendsofphp/proxy-manager-lts": "^1.0",
3232
"jean85/pretty-package-versions": "^1.3.0 || ^2.0.1",
33-
"mongodb/mongodb": "^1.17.0",
33+
"mongodb/mongodb": "^1.21 || ^2.0@dev",
3434
"psr/cache": "^1.0 || ^2.0 || ^3.0",
3535
"symfony/console": "^5.4 || ^6.0 || ^7.0",
3636
"symfony/deprecation-contracts": "^2.2 || ^3.0",

lib/Doctrine/ODM/MongoDB/DocumentManager.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ public function getDocumentDatabase(string $className): Database
322322
$db = $metadata->getDatabase();
323323
$db = $db ?: $this->config->getDefaultDB();
324324
$db = $db ?: 'doctrine';
325-
$this->documentDatabases[$className] = $this->client->selectDatabase($db);
325+
$this->documentDatabases[$className] = $this->client->getDatabase($db);
326326

327327
return $this->documentDatabases[$className];
328328
}
@@ -364,7 +364,7 @@ public function getDocumentCollection(string $className): Collection
364364
$options['readPreference'] = new ReadPreference($metadata->readPreference, $metadata->readPreferenceTags);
365365
}
366366

367-
$this->documentCollections[$className] = $db->selectCollection($collectionName, $options);
367+
$this->documentCollections[$className] = $db->getCollection($collectionName, $options);
368368
}
369369

370370
return $this->documentCollections[$className];

lib/Doctrine/ODM/MongoDB/Id/IncrementGenerator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function generate(DocumentManager $dm, object $document)
5656

5757
$key = $this->key ?: $dm->getDocumentCollection($className)->getCollectionName();
5858
$collectionName = $this->collection ?: 'doctrine_increment_ids';
59-
$collection = $db->selectCollection($collectionName);
59+
$collection = $db->getCollection($collectionName);
6060

6161
/*
6262
* Unable to use '$inc' and '$setOnInsert' together due to known bug.

lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php

+2
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,8 @@ public function loadAll(array $criteria = [], ?array $sort = null, ?int $limit =
544544
assert($this->collection instanceof Collection);
545545
$baseCursor = $this->collection->find($criteria, $options);
546546

547+
assert($baseCursor instanceof CursorInterface && $baseCursor instanceof SplIterator);
548+
547549
return $this->wrapCursor($baseCursor);
548550
}
549551

lib/Doctrine/ODM/MongoDB/SchemaManager.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ public function ensureDocumentSharding(string $documentName, ?WriteConcern $writ
956956
public function enableShardingForDbByDocumentName(string $documentName): void
957957
{
958958
$dbName = $this->dm->getDocumentDatabase($documentName)->getDatabaseName();
959-
$adminDb = $this->dm->getClient()->selectDatabase('admin');
959+
$adminDb = $this->dm->getClient()->getDatabase('admin');
960960

961961
try {
962962
$adminDb->command(['enableSharding' => $dbName]);
@@ -976,7 +976,7 @@ private function runShardCollectionCommand(string $documentName, ?WriteConcern $
976976
$class = $this->dm->getClassMetadata($documentName);
977977
$dbName = $this->dm->getDocumentDatabase($documentName)->getDatabaseName();
978978
$shardKey = $class->getShardKey();
979-
$adminDb = $this->dm->getClient()->selectDatabase('admin');
979+
$adminDb = $this->dm->getClient()->getDatabase('admin');
980980

981981
$shardKeyPart = [];
982982
foreach ($shardKey['keys'] as $key => $order) {

phpstan-baseline.neon

+55-35
Original file line numberDiff line numberDiff line change
@@ -50,31 +50,11 @@ parameters:
5050
count: 1
5151
path: lib/Doctrine/ODM/MongoDB/Aggregation/Stage/GraphLookup.php
5252

53-
-
54-
message: "#^Method Doctrine\\\\ODM\\\\MongoDB\\\\Aggregation\\\\Stage\\\\Lookup\\:\\:getExpression\\(\\) return type has no value type specified in iterable type array\\.$#"
55-
count: 1
56-
path: lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Lookup.php
57-
58-
-
59-
message: "#^PHPDoc tag @return with type mixed is not subtype of native type array\\.$#"
60-
count: 1
61-
path: lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Lookup.php
62-
6353
-
6454
message: "#^Unable to resolve the template type T in call to method Doctrine\\\\ODM\\\\MongoDB\\\\DocumentManager\\:\\:getClassMetadata\\(\\)$#"
6555
count: 1
6656
path: lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Lookup.php
6757

68-
-
69-
message: "#^Method Doctrine\\\\ODM\\\\MongoDB\\\\Aggregation\\\\Stage\\\\Merge\\:\\:whenMatched\\(\\) has parameter \\$whenMatched with no value type specified in iterable type array\\.$#"
70-
count: 1
71-
path: lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Merge.php
72-
73-
-
74-
message: "#^Property Doctrine\\\\ODM\\\\MongoDB\\\\Aggregation\\\\Stage\\\\Merge\\:\\:\\$whenMatched type has no value type specified in iterable type array\\.$#"
75-
count: 1
76-
path: lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Merge.php
77-
7858
-
7959
message: "#^Unable to resolve the template type T in call to method Doctrine\\\\ODM\\\\MongoDB\\\\DocumentManager\\:\\:getClassMetadata\\(\\)$#"
8060
count: 1
@@ -530,11 +510,6 @@ parameters:
530510
count: 1
531511
path: lib/Doctrine/ODM/MongoDB/PersistentCollection.php
532512

533-
-
534-
message: "#^Right side of && is always true\\.$#"
535-
count: 1
536-
path: lib/Doctrine/ODM/MongoDB/PersistentCollection.php
537-
538513
-
539514
message: "#^Unable to resolve the template type T in call to method Doctrine\\\\ODM\\\\MongoDB\\\\DocumentManager\\:\\:getClassMetadata\\(\\)$#"
540515
count: 1
@@ -960,16 +935,6 @@ parameters:
960935
count: 1
961936
path: tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataLoadEventTest.php
962937

963-
-
964-
message: "#^Parameter \\#1 \\$mapping of method Doctrine\\\\ODM\\\\MongoDB\\\\Mapping\\\\ClassMetadata\\<class@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest\\.php\\:232\\>\\:\\:mapField\\(\\) expects array\\{type\\?\\: string, fieldName\\?\\: string, name\\?\\: string, strategy\\?\\: string, association\\?\\: int, id\\?\\: bool, isOwningSide\\?\\: bool, collectionClass\\?\\: class\\-string, \\.\\.\\.\\}, array\\{fieldName\\: 'enum', enumType\\: 'Documents\\\\\\\\Card'\\} given\\.$#"
965-
count: 1
966-
path: tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest.php
967-
968-
-
969-
message: "#^Parameter \\#1 \\$mapping of method Doctrine\\\\ODM\\\\MongoDB\\\\Mapping\\\\ClassMetadata\\<class@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest\\.php\\:251\\>\\:\\:mapField\\(\\) expects array\\{type\\?\\: string, fieldName\\?\\: string, name\\?\\: string, strategy\\?\\: string, association\\?\\: int, id\\?\\: bool, isOwningSide\\?\\: bool, collectionClass\\?\\: class\\-string, \\.\\.\\.\\}, array\\{fieldName\\: 'enum', enumType\\: 'Documents\\\\\\\\SuitNonBacked'\\} given\\.$#"
970-
count: 1
971-
path: tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest.php
972-
973938
-
974939
message: "#^Property DoctrineGlobal_User\\:\\:\\$email is unused\\.$#"
975940
count: 1
@@ -1030,16 +995,71 @@ parameters:
1030995
count: 2
1031996
path: tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php
1032997

998+
-
999+
message: "#^Method Doctrine\\\\ODM\\\\MongoDB\\\\Tests\\\\QueryTest\\:\\:createCursorMock\\(\\) return type has no value type specified in iterable type Traversable\\.$#"
1000+
count: 1
1001+
path: tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php
1002+
1003+
-
1004+
message: "#^Method class@anonymous/tests/Doctrine/ODM/MongoDB/Tests/QueryTest\\.php\\:561\\:\\:current\\(\\) return type has no value type specified in iterable type array\\.$#"
1005+
count: 1
1006+
path: tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php
1007+
1008+
-
1009+
message: "#^Method class@anonymous/tests/Doctrine/ODM/MongoDB/Tests/QueryTest\\.php\\:561\\:\\:setTypeMap\\(\\) has parameter \\$typemap with no value type specified in iterable type array\\.$#"
1010+
count: 1
1011+
path: tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php
1012+
1013+
-
1014+
message: "#^Method class@anonymous/tests/Doctrine/ODM/MongoDB/Tests/QueryTest\\.php\\:561\\:\\:toArray\\(\\) return type has no value type specified in iterable type array\\.$#"
1015+
count: 1
1016+
path: tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php
1017+
10331018
-
10341019
message: "#^Parameter \\#4 \\$query of class Doctrine\\\\ODM\\\\MongoDB\\\\Query\\\\Query constructor expects array\\{distinct\\?\\: string, hint\\?\\: array\\<string, \\-1\\|1\\>\\|string, limit\\?\\: int, maxTimeMS\\?\\: int, multiple\\?\\: bool, new\\?\\: bool, newObj\\?\\: array\\<string, mixed\\>, query\\?\\: array\\<string, mixed\\>, \\.\\.\\.\\}, array\\{type\\: \\-1\\} given\\.$#"
10351020
count: 1
10361021
path: tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php
10371022

1023+
-
1024+
message: "#^Return type \\(MongoDB\\\\BSON\\\\Int64\\) of method class@anonymous/tests/Doctrine/ODM/MongoDB/Tests/QueryTest\\.php\\:561\\:\\:getId\\(\\) should be compatible with return type \\(MongoDB\\\\Driver\\\\CursorId\\) of method MongoDB\\\\Driver\\\\CursorInterface\\:\\:getId\\(\\)$#"
1025+
count: 1
1026+
path: tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php
1027+
10381028
-
10391029
message: "#^Used constant DOCTRINE_MONGODB_DATABASE not found\\.$#"
10401030
count: 1
10411031
path: tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php
10421032

1033+
-
1034+
message: "#^Instantiated class MongoDB\\\\Model\\\\CollectionInfoCommandIterator not found\\.$#"
1035+
count: 1
1036+
path: tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php
1037+
1038+
-
1039+
message: "#^Instantiated class MongoDB\\\\Model\\\\IndexInfoIteratorIterator not found\\.$#"
1040+
count: 1
1041+
path: tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php
1042+
1043+
-
1044+
message: "#^Method Doctrine\\\\ODM\\\\MongoDB\\\\Tests\\\\SchemaManagerTest\\:\\:createCollectionIterator\\(\\) has parameter \\$collections with no value type specified in iterable type array\\.$#"
1045+
count: 1
1046+
path: tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php
1047+
1048+
-
1049+
message: "#^Method Doctrine\\\\ODM\\\\MongoDB\\\\Tests\\\\SchemaManagerTest\\:\\:createCollectionIterator\\(\\) should return Iterator but returns MongoDB\\\\Model\\\\CollectionInfoCommandIterator\\.$#"
1050+
count: 1
1051+
path: tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php
1052+
1053+
-
1054+
message: "#^Method Doctrine\\\\ODM\\\\MongoDB\\\\Tests\\\\SchemaManagerTest\\:\\:createIndexIterator\\(\\) has parameter \\$indexes with no value type specified in iterable type array\\.$#"
1055+
count: 1
1056+
path: tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php
1057+
1058+
-
1059+
message: "#^Method Doctrine\\\\ODM\\\\MongoDB\\\\Tests\\\\SchemaManagerTest\\:\\:createIndexIterator\\(\\) should return Iterator but returns MongoDB\\\\Model\\\\IndexInfoIteratorIterator\\.$#"
1060+
count: 1
1061+
path: tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php
1062+
10431063
-
10441064
message: "#^Property Doctrine\\\\ODM\\\\MongoDB\\\\Tests\\\\Tools\\\\GH297\\\\User\\:\\:\\$id is never written, only read\\.$#"
10451065
count: 1

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ public function tearDown(): void
6565
return;
6666
}
6767

68-
$collections = $client->selectDatabase(DOCTRINE_MONGODB_DATABASE)->listCollections();
68+
$collections = $client->getDatabase(DOCTRINE_MONGODB_DATABASE)->listCollections();
6969

7070
foreach ($collections as $collection) {
7171
// See https://jira.mongodb.org/browse/SERVER-16541
7272
if (preg_match('#^system\.#', $collection->getName())) {
7373
continue;
7474
}
7575

76-
$client->selectCollection(DOCTRINE_MONGODB_DATABASE, $collection->getName())->drop();
76+
$client->getCollection(DOCTRINE_MONGODB_DATABASE, $collection->getName())->drop();
7777
}
7878
}
7979

@@ -137,7 +137,7 @@ protected static function createTestDocumentManager(): DocumentManager
137137

138138
protected function getServerVersion(): string
139139
{
140-
$result = $this->dm->getClient()->selectDatabase(DOCTRINE_MONGODB_DATABASE)->command(['buildInfo' => 1], ['typeMap' => DocumentManager::CLIENT_TYPEMAP])->toArray()[0];
140+
$result = $this->dm->getClient()->getDatabase(DOCTRINE_MONGODB_DATABASE)->command(['buildInfo' => 1], ['typeMap' => DocumentManager::CLIENT_TYPEMAP])->toArray()[0];
141141

142142
return $result['version'];
143143
}

tests/Doctrine/ODM/MongoDB/Tests/Events/TransactionalLifecycleEventsTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function setUp(): void
2525

2626
public function tearDown(): void
2727
{
28-
$this->dm->getClient()->selectDatabase('admin')->command([
28+
$this->dm->getClient()->getDatabase('admin')->command([
2929
'configureFailPoint' => 'failCommand',
3030
'mode' => 'off',
3131
]);
@@ -188,7 +188,7 @@ protected static function createTestDocumentManager(): DocumentManager
188188

189189
private function createFailPoint(string $failCommand): void
190190
{
191-
$this->dm->getClient()->selectDatabase('admin')->command([
191+
$this->dm->getClient()->getDatabase('admin')->command([
192192
'configureFailPoint' => 'failCommand',
193193
'mode' => ['times' => 1],
194194
'data' => [

tests/Doctrine/ODM/MongoDB/Tests/Id/IncrementGeneratorTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function testIdGeneratorWithStartingValue(): void
1717
$generator = new IncrementGenerator();
1818
$generator->setStartingId(10);
1919

20-
$collection = $this->dm->getClient()->selectCollection(DOCTRINE_MONGODB_DATABASE, 'doctrine_increment_ids');
20+
$collection = $this->dm->getClient()->getCollection(DOCTRINE_MONGODB_DATABASE, 'doctrine_increment_ids');
2121

2222
self::assertSame(10, $generator->generate($this->dm, new User()));
2323
$result = $collection->findOne(['_id' => 'users']);
@@ -34,7 +34,7 @@ public function testUsesOneAsStartingValueIfNotOverridden(): void
3434

3535
self::assertSame(1, $generator->generate($this->dm, new User()));
3636

37-
$collection = $this->dm->getClient()->selectCollection(DOCTRINE_MONGODB_DATABASE, 'doctrine_increment_ids');
37+
$collection = $this->dm->getClient()->getCollection(DOCTRINE_MONGODB_DATABASE, 'doctrine_increment_ids');
3838
$result = $collection->findOne(['_id' => 'users']);
3939
self::assertSame(1, $result['current_id']);
4040
}

tests/Doctrine/ODM/MongoDB/Tests/Proxy/Factory/ProxyFactoryTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ public function testProxyInitializeWithException(): void
3232
$database = $this->createMock(Database::class);
3333

3434
$this->client->expects($this->once())
35-
->method('selectDatabase')
35+
->method('getDatabase')
3636
->willReturn($database);
3737

3838
$database->expects($this->once())
39-
->method('selectCollection')
39+
->method('getCollection')
4040
->willReturn($collection);
4141

4242
$collection->expects($this->once())

0 commit comments

Comments
 (0)