Skip to content

Commit 0873458

Browse files
committed
Update comments & use createCollection and dropCollection when appropriate
1 parent 7667f7f commit 0873458

9 files changed

+22
-56
lines changed

tests/DocumentationExamplesTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,8 +1234,6 @@ public function testIndex_example_2(): void
12341234
// Start Transactions Intro Example 1
12351235
private function updateEmployeeInfo1(\MongoDB\Client $client, \MongoDB\Driver\Session $session): void
12361236
{
1237-
$this->dropCollection('hr', 'employees');
1238-
$this->dropCollection('reporting', 'events');
12391237
$session->startTransaction([
12401238
'readConcern' => new \MongoDB\Driver\ReadConcern('snapshot'),
12411239
'writeConcern' => new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY),
@@ -1292,13 +1290,12 @@ public function testTransactions_intro_example_1(): void
12921290

12931291
$client = static::createTestClient();
12941292

1295-
/* The WC is required: https://mongodb.com/docs/manual/core/transactions/#transactions-and-locks */
12961293
$this->dropCollection('hr', 'employees');
12971294
$this->dropCollection('reporting', 'events');
12981295

12991296
/* Collections need to be created before a transaction starts */
1300-
$client->hr->createCollection('employees', ['writeConcern' => new WriteConcern('majority')]);
1301-
$client->reporting->createCollection('events', ['writeConcern' => new WriteConcern('majority')]);
1297+
$this->createCollection('hr', 'employees');
1298+
$this->createCollection('reporting', 'events');
13021299

13031300
$session = $client->startSession();
13041301

@@ -1472,7 +1469,6 @@ public function testTransactions_retry_example_3(): void
14721469

14731470
$client = static::createTestClient();
14741471

1475-
/* The WC is required: https://mongodb.com/docs/manual/core/transactions/#transactions-and-locks */
14761472
$this->dropCollection('hr', 'employees');
14771473
$this->dropCollection('reporting', 'events');
14781474

tests/FunctionalTestCase.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use MongoDB\Operation\CreateCollection;
1717
use MongoDB\Operation\DatabaseCommand;
1818
use MongoDB\Operation\DropCollection;
19-
use MongoDB\Operation\DropDatabase;
2019
use MongoDB\Operation\ListCollections;
2120
use stdClass;
2221
use UnexpectedValueException;
@@ -260,11 +259,12 @@ public static function getModuleInfo(string $row): ?string
260259
}
261260

262261
/**
263-
* Creates the test collection with the specified options and ensures it is dropped again during tearDown().
262+
* Creates the test collection with the specified options and ensures it is
263+
* dropped again during tearDown().
264264
*
265-
* If the "writeConcern" option is not specified but is supported by the
266-
* server, a majority write concern will be used. This is helpful for tests
267-
* using transactions or secondary reads.
265+
* A majority write concern is applied by default to ensure that the
266+
* transaction can acquire the required locks.
267+
* See: https://www.mongodb.com/docs/manual/core/transactions/#transactions-and-operations
268268
*/
269269
protected function createCollection(?string $databaseName = null, ?string $collectionName = null, array $options = []): void
270270
{
@@ -285,6 +285,10 @@ protected function createCollection(?string $databaseName = null, ?string $colle
285285

286286
/**
287287
* Drops the test collection and ensures it is dropped again during tearDown().
288+
*
289+
* A majority write concern is applied by default to ensure that the
290+
* transaction can acquire the required locks.
291+
* See: https://www.mongodb.com/docs/manual/core/transactions/#transactions-and-operations
288292
*/
289293
protected function dropCollection(?string $databaseName = null, ?string $collectionName = null): void
290294
{
@@ -303,7 +307,7 @@ protected function dropCollection(?string $databaseName = null, ?string $collect
303307
$this->collectionsToCleanup[$databaseName][$collectionName] = true;
304308
}
305309

306-
protected function cleanupCollections(): void
310+
private function cleanupCollections(): void
307311
{
308312
foreach ($this->collectionsToCleanup as $databaseName => $collectionNames) {
309313
foreach ($collectionNames as $collectionName => $unused) {

tests/Model/ChangeStreamIteratorTest.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
use MongoDB\Driver\Exception\LogicException;
1212
use MongoDB\Exception\InvalidArgumentException;
1313
use MongoDB\Model\ChangeStreamIterator;
14-
use MongoDB\Operation\CreateCollection;
15-
use MongoDB\Operation\DropCollection;
1614
use MongoDB\Operation\Find;
1715
use MongoDB\Tests\CommandObserver;
1816
use MongoDB\Tests\FunctionalTestCase;
@@ -30,11 +28,8 @@ public function setUp(): void
3028
{
3129
parent::setUp();
3230

33-
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
34-
$operation->execute($this->getPrimaryServer());
35-
36-
$operation = new CreateCollection($this->getDatabaseName(), $this->getCollectionName(), ['capped' => true, 'size' => 8192]);
37-
$operation->execute($this->getPrimaryServer());
31+
$this->dropCollection();
32+
$this->createCollection(null, null, ['capped' => true, 'size' => 8192]);
3833

3934
$this->collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName());
4035
}

tests/Model/IndexInfoFunctionalTest.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,8 @@ public function setUp(): void
1414
{
1515
parent::setUp();
1616

17+
$this->dropCollection();
1718
$this->collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName());
18-
$this->collection->drop();
19-
}
20-
21-
public function tearDown(): void
22-
{
23-
if (! $this->hasFailed()) {
24-
$this->collection->drop();
25-
}
26-
27-
parent::tearDown();
2819
}
2920

3021
public function testIs2dSphere(): void

tests/Operation/ExplainFunctionalTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use MongoDB\Driver\BulkWrite;
66
use MongoDB\Operation\Aggregate;
77
use MongoDB\Operation\Count;
8-
use MongoDB\Operation\CreateCollection;
98
use MongoDB\Operation\Delete;
109
use MongoDB\Operation\DeleteMany;
1110
use MongoDB\Operation\DeleteOne;
@@ -137,8 +136,7 @@ public function testFindMaxAwait(): void
137136
'size' => 1048576,
138137
];
139138

140-
$operation = new CreateCollection($databaseName, $cappedCollectionName, $cappedCollectionOptions);
141-
$operation->execute($this->getPrimaryServer());
139+
$this->createCollection($databaseName, $cappedCollectionName, $cappedCollectionOptions);
142140

143141
$this->createFixtures(2);
144142

tests/Operation/FindFunctionalTest.php

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

55
use MongoDB\Driver\BulkWrite;
66
use MongoDB\Driver\ReadPreference;
7-
use MongoDB\Operation\CreateCollection;
87
use MongoDB\Operation\CreateIndexes;
98
use MongoDB\Operation\Find;
109
use MongoDB\Tests\CommandObserver;
@@ -161,8 +160,7 @@ public function testMaxAwaitTimeMS(): void
161160
'size' => 1048576,
162161
];
163162

164-
$operation = new CreateCollection($databaseName, $cappedCollectionName, $cappedCollectionOptions);
165-
$operation->execute($this->getPrimaryServer());
163+
$this->createCollection($databaseName, $cappedCollectionName, $cappedCollectionOptions);
166164

167165
// Insert documents into the capped collection.
168166
$bulkWrite = new BulkWrite(['ordered' => true]);

tests/Operation/FunctionalTestCase.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,6 @@ public function setUp(): void
1818
$this->dropCollection();
1919
}
2020

21-
public function tearDown(): void
22-
{
23-
if (! $this->hasFailed()) {
24-
$this->dropCollection();
25-
}
26-
27-
parent::tearDown();
28-
}
29-
3021
protected function createDefaultReadConcern()
3122
{
3223
return new ReadConcern();

tests/Operation/ListIndexesFunctionalTest.php

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

55
use MongoDB\Model\IndexInfo;
66
use MongoDB\Model\IndexInfoIterator;
7-
use MongoDB\Operation\DropCollection;
87
use MongoDB\Operation\InsertOne;
98
use MongoDB\Operation\ListIndexes;
109
use MongoDB\Tests\CommandObserver;
@@ -13,8 +12,7 @@ class ListIndexesFunctionalTest extends FunctionalTestCase
1312
{
1413
public function testListIndexesForNewlyCreatedCollection(): void
1514
{
16-
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
17-
$operation->execute($this->getPrimaryServer());
15+
$this->dropCollection();
1816

1917
$insertOne = new InsertOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1]);
2018
$writeResult = $insertOne->execute($this->getPrimaryServer());
@@ -36,8 +34,7 @@ public function testListIndexesForNewlyCreatedCollection(): void
3634

3735
public function testListIndexesForNonexistentCollection(): void
3836
{
39-
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
40-
$operation->execute($this->getPrimaryServer());
37+
$this->dropCollection();
4138

4239
$operation = new ListIndexes($this->getDatabaseName(), $this->getCollectionName());
4340
$indexes = $operation->execute($this->getPrimaryServer());

tests/Operation/MapReduceFunctionalTest.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use MongoDB\BSON\Javascript;
66
use MongoDB\Driver\BulkWrite;
77
use MongoDB\MapReduceResult;
8-
use MongoDB\Operation\DropCollection;
98
use MongoDB\Operation\Find;
109
use MongoDB\Operation\MapReduce;
1110
use MongoDB\Tests\CommandObserver;
@@ -51,6 +50,7 @@ public function testDefaultWriteConcernIsOmitted(): void
5150
{
5251
// Collection must exist for mapReduce command
5352
$this->createCollection();
53+
$this->dropCollection(null, $this->getCollectionName() . '.output');
5454

5555
(new CommandObserver())->observe(
5656
function (): void {
@@ -69,9 +69,6 @@ function (array $event): void {
6969
$this->assertObjectNotHasAttribute('writeConcern', $event['started']->getCommand());
7070
}
7171
);
72-
73-
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName() . '.output');
74-
$operation->execute($this->getPrimaryServer());
7572
}
7673

7774
public function testFinalize(): void
@@ -273,6 +270,7 @@ public function testTypeMapOptionWithOutputCollection(?array $typeMap, array $ex
273270
$map = new Javascript('function() { emit(this.x, this.y); }');
274271
$reduce = new Javascript('function(key, values) { return Array.sum(values); }');
275272
$out = $this->getCollectionName() . '.output';
273+
$this->dropCollection(null, $out);
276274

277275
$operation = new MapReduce($this->getDatabaseName(), $this->getCollectionName(), $map, $reduce, $out, ['typeMap' => $typeMap]);
278276
$results = iterator_to_array($operation->execute($this->getPrimaryServer()));
@@ -283,16 +281,14 @@ public function testTypeMapOptionWithOutputCollection(?array $typeMap, array $ex
283281
$cursor = $operation->execute($this->getPrimaryServer());
284282

285283
$this->assertEquals($this->sortResults($expectedDocuments), $this->sortResults(iterator_to_array($cursor)));
286-
287-
$operation = new DropCollection($this->getDatabaseName(), $out);
288-
$operation->execute($this->getPrimaryServer());
289284
}
290285

291286
/**
292287
* Create data fixtures.
293288
*/
294289
private function createFixtures(int $n): void
295290
{
291+
$this->dropCollection();
296292
$bulkWrite = new BulkWrite(['ordered' => true]);
297293

298294
for ($i = 1; $i <= $n; $i++) {

0 commit comments

Comments
 (0)