Skip to content

Commit 8f62c6f

Browse files
committed
Use the collection instance to drop it with its options
1 parent 0738070 commit 8f62c6f

File tree

3 files changed

+26
-38
lines changed

3 files changed

+26
-38
lines changed

tests/DocumentationExamplesTest.php

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -953,9 +953,8 @@ public function testChangeStreamExample_1_4(): void
953953
$this->markTestSkipped('Test does not apply on sharded clusters: need more than a single getMore call on the change stream.');
954954
}
955955

956-
$this->dropCollection($this->getDatabaseName(), 'inventory');
956+
$this->createCollection($this->getDatabaseName(), 'inventory');
957957
$db = new Database($this->manager, $this->getDatabaseName());
958-
$db->createCollection('inventory');
959958

960959
// Start Changestream Example 1
961960
$changeStream = $db->inventory->watch();
@@ -1189,9 +1188,8 @@ public function testRunCommand_example_1(): void
11891188

11901189
public function testRunCommand_example_2(): void
11911190
{
1192-
$this->dropCollection($this->getDatabaseName(), 'restaurants');
1191+
$this->createCollection($this->getDatabaseName(), 'restaurants');
11931192
$db = new Database($this->manager, $this->getDatabaseName());
1194-
$db->createCollection('restaurants');
11951193

11961194
// Start runCommand Example 2
11971195
$cursor = $db->command(['collStats' => 'restaurants']);
@@ -1290,9 +1288,6 @@ public function testTransactions_intro_example_1(): void
12901288

12911289
$client = static::createTestClient();
12921290

1293-
$this->dropCollection('hr', 'employees');
1294-
$this->dropCollection('reporting', 'events');
1295-
12961291
/* Collections need to be created before a transaction starts */
12971292
$this->createCollection('hr', 'employees');
12981293
$this->createCollection('reporting', 'events');
@@ -1469,9 +1464,6 @@ public function testTransactions_retry_example_3(): void
14691464

14701465
$client = static::createTestClient();
14711466

1472-
$this->dropCollection('hr', 'employees');
1473-
$this->dropCollection('reporting', 'events');
1474-
14751467
/* Collections need to be created before a transaction starts */
14761468
$this->createCollection('hr', 'employees');
14771469
$this->createCollection('reporting', 'events');
@@ -1491,12 +1483,8 @@ public function testCausalConsistency(): void
14911483
$this->assertNotNull('This test intentionally performs no assertions');
14921484

14931485
// Prep
1494-
$this->dropCollection('test', 'items');
14951486
$client = static::createTestClient();
1496-
$items = $client->selectDatabase(
1497-
'test',
1498-
['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)]
1499-
)->items;
1487+
$items = $this->createCollection('test', 'items');
15001488
$items->insertOne(
15011489
['sku' => '111', 'name' => 'Peanuts', 'start' => new UTCDateTime()]
15021490
);
@@ -1865,17 +1853,16 @@ public function testQueryableEncryption(): void
18651853
$this->markTestSkipped('Automatic encryption requires MongoDB Enterprise');
18661854
}
18671855

1856+
// Ensure the collection is dropped by tearDown()
18681857
$this->dropCollection($this->getDatabaseName(), $this->getCollectionName());
18691858

18701859
// Fetch names for the database and collection under test
18711860
$collectionName = $this->getCollectionName();
18721861
$databaseName = $this->getDatabaseName();
18731862
$namespace = $this->getNamespace();
18741863

1875-
/* Create a client without auto encryption. Drop existing data in both
1876-
* the keyvault and database under test. The latter is necessary since
1877-
* setUp() only drops the collection under test, which will leave behind
1878-
* internal collections for queryable encryption. */
1864+
/* Create a client without auto encryption. Drop existing data in both the keyvault and database under test.
1865+
* The latter is necessary to clean up any internal collections for queryable encryption. */
18791866
$client = static::createTestClient();
18801867
$client->selectDatabase('keyvault')->drop(['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)]);
18811868
$client->selectDatabase($databaseName)->drop(['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)]);

tests/FunctionalTestCase.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use InvalidArgumentException;
66
use MongoDB\BSON\ObjectId;
77
use MongoDB\Client;
8+
use MongoDB\Collection;
89
use MongoDB\Driver\Command;
910
use MongoDB\Driver\Exception\CommandException;
1011
use MongoDB\Driver\Manager;
@@ -15,7 +16,6 @@
1516
use MongoDB\Driver\WriteConcern;
1617
use MongoDB\Operation\CreateCollection;
1718
use MongoDB\Operation\DatabaseCommand;
18-
use MongoDB\Operation\DropCollection;
1919
use MongoDB\Operation\ListCollections;
2020
use stdClass;
2121
use UnexpectedValueException;
@@ -54,7 +54,7 @@ abstract class FunctionalTestCase extends TestCase
5454
/** @var array */
5555
private $configuredFailPoints = [];
5656

57-
/** @var array{string, array{string, true}} */
57+
/** @var Collection[] */
5858
private $collectionsToCleanup = [];
5959

6060
public function setUp(): void
@@ -260,18 +260,21 @@ public static function getModuleInfo(string $row): ?string
260260

261261
/**
262262
* Creates the test collection with the specified options and ensures it is
263-
* dropped again during tearDown().
263+
* dropped again during tearDown(). If the collection already exists, it
264+
* is dropped and recreated.
264265
*
265266
* A majority write concern is applied by default to ensure that the
266267
* transaction can acquire the required locks.
267268
* See: https://www.mongodb.com/docs/manual/core/transactions/#transactions-and-operations
268269
*/
269-
protected function createCollection(string $databaseName, string $collectionName, array $options = []): void
270+
protected function createCollection(string $databaseName, string $collectionName, array $options = []): Collection
270271
{
271-
$options += ['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)];
272+
$collection = $this->dropCollection($databaseName, $collectionName, $options);
273+
272274
$operation = new CreateCollection($databaseName, $collectionName, $options);
273275
$operation->execute($this->getPrimaryServer());
274-
$this->collectionsToCleanup[$databaseName][$collectionName] = true;
276+
277+
return $collection;
275278
}
276279

277280
/**
@@ -281,20 +284,20 @@ protected function createCollection(string $databaseName, string $collectionName
281284
* transaction can acquire the required locks.
282285
* See: https://www.mongodb.com/docs/manual/core/transactions/#transactions-and-operations
283286
*/
284-
protected function dropCollection(string $databaseName, string $collectionName): void
287+
protected function dropCollection(string $databaseName, string $collectionName, array $options = []): Collection
285288
{
286-
$options = ['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)];
287-
$operation = new DropCollection($databaseName, $collectionName, $options);
288-
$operation->execute($this->getPrimaryServer());
289-
$this->collectionsToCleanup[$databaseName][$collectionName] = true;
289+
$options += ['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)];
290+
$collection = new Collection($this->manager, $databaseName, $collectionName, $options);
291+
$this->collectionsToCleanup[] = $collection;
292+
$collection->drop();
293+
294+
return $collection;
290295
}
291296

292297
private function cleanupCollections(): void
293298
{
294-
foreach ($this->collectionsToCleanup as $databaseName => $collectionNames) {
295-
foreach ($collectionNames as $collectionName => $unused) {
296-
$this->dropCollection($databaseName, $collectionName);
297-
}
299+
foreach ($this->collectionsToCleanup as $collection) {
300+
$collection->drop();
298301
}
299302

300303
$this->collectionsToCleanup = [];

tests/Model/ChangeStreamIteratorTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ public function setUp(): void
2828
{
2929
parent::setUp();
3030

31-
$this->dropCollection($this->getDatabaseName(), $this->getCollectionName());
32-
$this->createCollection($this->getDatabaseName(), $this->getCollectionName(), ['capped' => true, 'size' => 8192]);
33-
34-
$this->collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName());
31+
// Drop and re-create the collection
32+
$this->collection = $this->createCollection($this->getDatabaseName(), $this->getCollectionName(), ['capped' => true, 'size' => 8192]);
3533
}
3634

3735
/** @dataProvider provideInvalidIntegerValues */

0 commit comments

Comments
 (0)