Skip to content

Commit 6aa33d3

Browse files
committed
Add helper to create client and manager objects
1 parent 4493fe6 commit 6aa33d3

14 files changed

+53
-51
lines changed

tests/ClientFunctionalTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ private function doSetUp()
2828
{
2929
parent::setUp();
3030

31-
$this->client = new Client(static::getUri());
31+
$this->client = static::createTestClient();
3232
$this->client->dropDatabase($this->getDatabaseName());
3333
}
3434

tests/DocumentationExamplesTest.php

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

55
use MongoDB\BSON\ObjectId;
66
use MongoDB\BSON\UTCDateTime;
7-
use MongoDB\Client;
87
use MongoDB\Database;
98
use MongoDB\Driver\Cursor;
109
use MongoDB\Driver\Exception\Exception;
@@ -1270,7 +1269,7 @@ public function testTransactions_intro_example_1()
12701269

12711270
$this->assertNotNull('This test intentionally performs no assertions');
12721271

1273-
$client = new Client(static::getUri());
1272+
$client = static::createTestClient();
12741273

12751274
/* The WC is required: https://docs.mongodb.com/manual/core/transactions/#transactions-and-locks */
12761275
$client->hr->dropCollection('employees', ['writeConcern' => new WriteConcern('majority')]);
@@ -1445,7 +1444,7 @@ public function testTransactions_retry_example_3()
14451444

14461445
$this->assertNotNull('This test intentionally performs no assertions');
14471446

1448-
$client = new Client(static::getUri());
1447+
$client = static::createTestClient();
14491448

14501449
/* The WC is required: https://docs.mongodb.com/manual/core/transactions/#transactions-and-locks */
14511450
$client->hr->dropCollection('employees', ['writeConcern' => new WriteConcern('majority')]);
@@ -1470,7 +1469,7 @@ public function testCausalConsistency()
14701469
$this->assertNotNull('This test intentionally performs no assertions');
14711470

14721471
// Prep
1473-
$client = new Client(static::getUri());
1472+
$client = static::createTestClient();
14741473
$items = $client->selectDatabase(
14751474
'test',
14761475
[ 'writeConcern' => new WriteConcern(WriteConcern::MAJORITY) ]

tests/FunctionalTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private function doSetUp()
5151
{
5252
parent::setUp();
5353

54-
$this->manager = new Manager(static::getUri());
54+
$this->manager = static::createTestManager();
5555
$this->configuredFailPoints = [];
5656
}
5757

@@ -86,7 +86,7 @@ public static function getUri($allowMultipleMongoses = false)
8686
return $uri;
8787
}
8888

89-
$manager = new Manager($uri);
89+
$manager = static::createTestManager($uri);
9090
if ($manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY))->getType() !== Server::TYPE_MONGOS) {
9191
return $uri;
9292
}

tests/Operation/FindAndModifyFunctionalTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace MongoDB\Tests\Operation;
44

55
use MongoDB\Driver\BulkWrite;
6-
use MongoDB\Driver\Manager;
76
use MongoDB\Driver\ReadPreference;
87
use MongoDB\Model\BSONDocument;
98
use MongoDB\Operation\FindAndModify;
@@ -17,7 +16,7 @@ class FindAndModifyFunctionalTest extends FunctionalTestCase
1716
*/
1817
public function testManagerReadConcernIsOmitted()
1918
{
20-
$manager = new Manager(static::getUri(), ['readConcernLevel' => 'majority']);
19+
$manager = static::createTestManager(null, ['readConcernLevel' => 'majority']);
2120
$server = $manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
2221

2322
(new CommandObserver())->observe(

tests/Operation/WatchFunctionalTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use MongoDB\Driver\Exception\ConnectionTimeoutException;
1212
use MongoDB\Driver\Exception\LogicException;
1313
use MongoDB\Driver\Exception\ServerException;
14-
use MongoDB\Driver\Manager;
1514
use MongoDB\Driver\Monitoring\CommandSucceededEvent;
1615
use MongoDB\Driver\ReadPreference;
1716
use MongoDB\Driver\WriteConcern;
@@ -166,7 +165,7 @@ public function testNextResumesAfterConnectionException()
166165
/* In order to trigger a dropped connection, we'll use a new client with
167166
* a socket timeout that is less than the change stream's maxAwaitTimeMS
168167
* option. */
169-
$manager = new Manager(static::getUri(), ['socketTimeoutMS' => 50]);
168+
$manager = static::createTestManager(null, ['socketTimeoutMS' => 50]);
170169
$primaryServer = $manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
171170

172171
$operation = new Watch($manager, $this->getDatabaseName(), $this->getCollectionName(), [], $this->defaultOptions);

tests/SpecTests/AtlasDataLakeSpecTest.php

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

33
namespace MongoDB\Tests\SpecTests;
44

5-
use MongoDB\Client;
65
use MongoDB\Driver\Command;
76
use MongoDB\Driver\Cursor;
87
use MongoDB\Tests\CommandObserver;
@@ -135,7 +134,7 @@ public function testKillCursors()
135134

136135
(new CommandObserver())->observe(
137136
function () {
138-
$client = new Client(static::getUri());
137+
$client = static::createTestClient();
139138
$client->test->driverdata->find([], ['batchSize' => 2, 'limit' => 3]);
140139
},
141140
function (array $event) use (&$cursorId, &$cursorNamespace) {
@@ -205,7 +204,7 @@ public function testConnectWithoutAuth()
205204

206205
$uri = $parts['scheme'] . '://' . $parts['host'] . $port . $path . $query;
207206

208-
$client = new Client($uri);
207+
$client = static::createTestClient($uri);
209208
$cursor = $client->selectDatabase($this->getDatabaseName())->command(['ping' => 1]);
210209

211210
$this->assertInstanceOf(Cursor::class, $cursor);
@@ -217,7 +216,7 @@ public function testConnectWithoutAuth()
217216
*/
218217
public function testConnectwithSCRAMSHA1()
219218
{
220-
$client = new Client(static::getUri(), ['authMechanism' => 'SCRAM-SHA-1']);
219+
$client = static::createTestClient(null, ['authMechanism' => 'SCRAM-SHA-1']);
221220
$cursor = $client->selectDatabase($this->getDatabaseName())->command(['ping' => 1]);
222221

223222
$this->assertInstanceOf(Cursor::class, $cursor);
@@ -229,7 +228,7 @@ public function testConnectwithSCRAMSHA1()
229228
*/
230229
public function testConnectwithSCRAMSHA256()
231230
{
232-
$client = new Client(static::getUri(), ['authMechanism' => 'SCRAM-SHA-256']);
231+
$client = static::createTestClient(null, ['authMechanism' => 'SCRAM-SHA-256']);
233232
$cursor = $client->selectDatabase($this->getDatabaseName())->command(['ping' => 1]);
234233

235234
$this->assertInstanceOf(Cursor::class, $cursor);

tests/SpecTests/ClientSideEncryptionSpecTest.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Closure;
66
use MongoDB\BSON\Binary;
77
use MongoDB\BSON\Int64;
8-
use MongoDB\Client;
98
use MongoDB\Collection;
109
use MongoDB\Driver\ClientEncryption;
1110
use MongoDB\Driver\Exception\AuthenticationException;
@@ -206,7 +205,7 @@ public function testDataKeyAndDoubleEncryption(string $providerName, $masterKey)
206205
'keyVaultClient' => $client,
207206
];
208207

209-
$clientEncrypted = new Client(static::getUri(), [], ['autoEncryption' => $autoEncryptionOpts]);
208+
$clientEncrypted = static::createTestClient(null, [], ['autoEncryption' => $autoEncryptionOpts]);
210209
$clientEncryption = $clientEncrypted->createClientEncryption($encryptionOpts);
211210

212211
$commands = [];
@@ -323,7 +322,7 @@ public function testExternalKeyVault($withExternalKeyVault)
323322
];
324323

325324
if ($withExternalKeyVault) {
326-
$encryptionOpts['keyVaultClient'] = new Client(static::getUri(), ['username' => 'fake-user', 'password' => 'fake-pwd']);
325+
$encryptionOpts['keyVaultClient'] = static::createTestClient(null, ['username' => 'fake-user', 'password' => 'fake-pwd']);
327326
}
328327

329328
$autoEncryptionOpts = $encryptionOpts + [
@@ -332,7 +331,7 @@ public function testExternalKeyVault($withExternalKeyVault)
332331
],
333332
];
334333

335-
$clientEncrypted = new Client(static::getUri(), [], ['autoEncryption' => $autoEncryptionOpts]);
334+
$clientEncrypted = static::createTestClient(null, [], ['autoEncryption' => $autoEncryptionOpts]);
336335
$clientEncryption = $clientEncrypted->createClientEncryption($encryptionOpts);
337336

338337
try {
@@ -469,7 +468,7 @@ public function testBSONSizeLimitsAndBatchSplitting(Closure $test)
469468
'keyVaultClient' => $client,
470469
];
471470

472-
$clientEncrypted = new Client(static::getUri(), [], ['autoEncryption' => $autoEncryptionOpts]);
471+
$clientEncrypted = static::createTestClient(null, [], ['autoEncryption' => $autoEncryptionOpts]);
473472

474473
$collection = $clientEncrypted->selectCollection('db', 'coll');
475474

@@ -483,7 +482,7 @@ public function testBSONSizeLimitsAndBatchSplitting(Closure $test)
483482
*/
484483
public function testViewsAreProhibited()
485484
{
486-
$client = new Client(static::getUri());
485+
$client = static::createTestClient();
487486

488487
$client->selectCollection('db', 'view')->drop();
489488
$client->selectDatabase('db')->command(['create' => 'view', 'viewOn' => 'coll']);
@@ -495,7 +494,7 @@ public function testViewsAreProhibited()
495494
],
496495
];
497496

498-
$clientEncrypted = new Client(static::getUri(), [], ['autoEncryption' => $autoEncryptionOpts]);
497+
$clientEncrypted = static::createTestClient(null, [], ['autoEncryption' => $autoEncryptionOpts]);
499498

500499
try {
501500
$clientEncrypted->selectCollection('db', 'view')->insertOne(['foo' => 'bar']);
@@ -557,7 +556,7 @@ public function testCorpus($schemaMap = true)
557556
$corpus = (array) $this->decodeJson(file_get_contents(__DIR__ . '/client-side-encryption/corpus/corpus.json'));
558557
$corpusCopied = [];
559558

560-
$clientEncrypted = new Client(static::getUri(), [], ['autoEncryption' => $autoEncryptionOpts]);
559+
$clientEncrypted = static::createTestClient(null, [], ['autoEncryption' => $autoEncryptionOpts]);
561560
$clientEncryption = $clientEncrypted->createClientEncryption($encryptionOpts);
562561

563562
$collection = $clientEncrypted->selectCollection('db', 'coll');
@@ -621,7 +620,7 @@ public function testCorpus($schemaMap = true)
621620
*/
622621
public function testCustomEndpoint(Closure $test)
623622
{
624-
$client = new Client(static::getUri());
623+
$client = static::createTestClient();
625624

626625
$clientEncryption = $client->createClientEncryption([
627626
'keyVaultNamespace' => 'keyvault.datakeys',
@@ -758,7 +757,7 @@ public function testBypassSpawningMongocryptdViaBypassSpawn()
758757
],
759758
];
760759

761-
$clientEncrypted = new Client(static::getUri(), [], ['autoEncryption' => $autoEncryptionOpts]);
760+
$clientEncrypted = static::createTestClient(null, [], ['autoEncryption' => $autoEncryptionOpts]);
762761

763762
try {
764763
$clientEncrypted->selectCollection('db', 'coll')->insertOne(['encrypted' => 'test']);
@@ -787,11 +786,11 @@ public function testBypassSpawningMongocryptdViaBypassAutoEncryption()
787786
],
788787
];
789788

790-
$clientEncrypted = new Client(static::getUri(), [], ['autoEncryption' => $autoEncryptionOpts]);
789+
$clientEncrypted = static::createTestClient(null, [], ['autoEncryption' => $autoEncryptionOpts]);
791790

792791
$clientEncrypted->selectCollection('db', 'coll')->insertOne(['encrypted' => 'test']);
793792

794-
$clientMongocryptd = new Client('mongodb://localhost:27021');
793+
$clientMongocryptd = static::createTestClient('mongodb://localhost:27021');
795794

796795
$this->expectException(ConnectionTimeoutException::class);
797796
$clientMongocryptd->selectDatabase('db')->command(['isMaster' => true]);

tests/SpecTests/Context.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public static function fromChangeStreams(stdClass $test, $databaseName, $collect
9393
{
9494
$o = new self($databaseName, $collectionName);
9595

96-
$o->client = new Client(FunctionalTestCase::getUri());
96+
$o->client = FunctionalTestCase::createTestClient();
9797

9898
return $o;
9999
}
@@ -132,10 +132,10 @@ public static function fromClientSideEncryption(stdClass $test, $databaseName, $
132132
$o->outcomeCollectionName = $test->outcome->collection->name;
133133
}
134134

135-
$o->client = new Client(FunctionalTestCase::getUri(), $clientOptions, $driverOptions);
135+
$o->client = FunctionalTestCase::createTestClient(null, $clientOptions, $driverOptions);
136136

137137
if ($autoEncryptionOptions !== []) {
138-
$o->encryptedClient = new Client(FunctionalTestCase::getUri(), $clientOptions, $driverOptions + ['autoEncryption' => $autoEncryptionOptions]);
138+
$o->encryptedClient = FunctionalTestCase::createTestClient(null, $clientOptions, $driverOptions + ['autoEncryption' => $autoEncryptionOptions]);
139139
}
140140

141141
return $o;
@@ -145,7 +145,7 @@ public static function fromCommandMonitoring(stdClass $test, $databaseName, $col
145145
{
146146
$o = new self($databaseName, $collectionName);
147147

148-
$o->client = new Client(FunctionalTestCase::getUri());
148+
$o->client = FunctionalTestCase::createTestClient();
149149

150150
return $o;
151151
}
@@ -169,7 +169,7 @@ public static function fromCrud(stdClass $test, $databaseName, $collectionName)
169169
'readPreference' => new ReadPreference('primary'),
170170
];
171171

172-
$o->client = new Client(FunctionalTestCase::getUri(), $clientOptions);
172+
$o->client = FunctionalTestCase::createTestClient(null, $clientOptions);
173173

174174
return $o;
175175
}
@@ -184,7 +184,7 @@ public static function fromReadWriteConcern(stdClass $test, $databaseName, $coll
184184

185185
$clientOptions = isset($test->clientOptions) ? (array) $test->clientOptions : [];
186186

187-
$o->client = new Client(FunctionalTestCase::getUri(), $clientOptions);
187+
$o->client = FunctionalTestCase::createTestClient(null, $clientOptions);
188188

189189
return $o;
190190
}
@@ -197,7 +197,7 @@ public static function fromRetryableReads(stdClass $test, $databaseName, $collec
197197

198198
$clientOptions = isset($test->clientOptions) ? (array) $test->clientOptions : [];
199199

200-
$o->client = new Client(FunctionalTestCase::getUri(), $clientOptions);
200+
$o->client = FunctionalTestCase::createTestClient(null, $clientOptions);
201201

202202
return $o;
203203
}
@@ -212,7 +212,7 @@ public static function fromRetryableWrites(stdClass $test, $databaseName, $colle
212212
$o->outcomeCollectionName = $test->outcome->collection->name;
213213
}
214214

215-
$o->client = new Client(FunctionalTestCase::getUri($useMultipleMongoses), $clientOptions);
215+
$o->client = FunctionalTestCase::createTestClient(FunctionalTestCase::getUri($useMultipleMongoses), $clientOptions);
216216

217217
return $o;
218218
}
@@ -237,7 +237,7 @@ public static function fromTransactions(stdClass $test, $databaseName, $collecti
237237
* re-using a previously persisted libmongoc client object. */
238238
$clientOptions += ['p' => mt_rand()];
239239

240-
$o->client = new Client(FunctionalTestCase::getUri($useMultipleMongoses), $clientOptions);
240+
$o->client = FunctionalTestCase::createTestClient(FunctionalTestCase::getUri($useMultipleMongoses), $clientOptions);
241241

242242
$session0Options = isset($test->sessionOptions->session0) ? (array) $test->sessionOptions->session0 : [];
243243
$session1Options = isset($test->sessionOptions->session1) ? (array) $test->sessionOptions->session1 : [];

tests/SpecTests/CrudSpecTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace MongoDB\Tests\SpecTests;
44

5-
use MongoDB\Client;
65
use MongoDB\Driver\Exception\BulkWriteException;
76
use stdClass;
87
use function basename;
@@ -144,7 +143,7 @@ public function testErrInfoIsPropagated()
144143
],
145144
]);
146145

147-
$client = new Client(static::getUri());
146+
$client = FunctionalTestCase::createTestClient();
148147

149148
try {
150149
$client->selectCollection($this->getDatabaseName(), $this->getCollectionName())->insertOne(['fail' => 1]);

tests/SpecTests/PrimaryStepDownSpecTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ private function doSetUp()
3838
{
3939
parent::setUp();
4040

41-
$this->client = new Client(static::getUri(), ['retryWrites' => false, 'heartbeatFrequencyMS' => 500, 'serverSelectionTimeoutMS' => 20000, 'serverSelectionTryOnce' => false]);
41+
$this->client = self::createTestClient(null, ['retryWrites' => false, 'heartbeatFrequencyMS' => 500, 'serverSelectionTimeoutMS' => 20000, 'serverSelectionTryOnce' => false]);
4242

4343
$this->dropAndRecreateCollection();
4444
$this->collection = $this->client->selectCollection($this->getDatabaseName(), $this->getCollectionName());

tests/SpecTests/TransactionsSpecTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44

55
use MongoDB\BSON\Int64;
66
use MongoDB\BSON\Timestamp;
7-
use MongoDB\Client;
87
use MongoDB\Driver\Command;
98
use MongoDB\Driver\Exception\ServerException;
10-
use MongoDB\Driver\Manager;
119
use MongoDB\Driver\ReadPreference;
1210
use MongoDB\Driver\Server;
1311
use stdClass;
@@ -209,7 +207,7 @@ public function testStartingNewTransactionOnPinnedSessionUnpinsSession()
209207
$this->markTestSkipped('Mongos pinning tests can only run on sharded clusters using replica sets');
210208
}
211209

212-
$client = new Client($this->getUri(true));
210+
$client = self::createTestClient($this->getUri(true));
213211

214212
$session = $client->startSession();
215213
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName());
@@ -247,7 +245,7 @@ public function testRunningNonTransactionOperationOnPinnedSessionUnpinsSession()
247245
$this->markTestSkipped('Mongos pinning tests can only run on sharded clusters using replica sets');
248246
}
249247

250-
$client = new Client($this->getUri(true));
248+
$client = self::createTestClient($this->getUri(true));
251249

252250
$session = $client->startSession();
253251
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName());
@@ -292,7 +290,7 @@ protected function createTestCollection()
292290
*/
293291
private static function killAllSessions()
294292
{
295-
$manager = new Manager(static::getUri());
293+
$manager = static::createTestManager();
296294
$primary = $manager->selectServer(new ReadPreference('primary'));
297295

298296
$servers = $primary->getType() === Server::TYPE_MONGOS

0 commit comments

Comments
 (0)