Skip to content

Commit 91d5b99

Browse files
committed
Use the collection instance to drop it with its options
1 parent 1d4f79a commit 91d5b99

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
@@ -954,9 +954,8 @@ public function testChangeStreamExample_1_4(): void
954954
$this->markTestSkipped('Test does not apply on sharded clusters: need more than a single getMore call on the change stream.');
955955
}
956956

957-
$this->dropCollection($this->getDatabaseName(), 'inventory');
957+
$this->createCollection($this->getDatabaseName(), 'inventory');
958958
$db = new Database($this->manager, $this->getDatabaseName());
959-
$db->createCollection('inventory');
960959

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

11911190
public function testRunCommand_example_2(): void
11921191
{
1193-
$this->dropCollection($this->getDatabaseName(), 'restaurants');
1192+
$this->createCollection($this->getDatabaseName(), 'restaurants');
11941193
$db = new Database($this->manager, $this->getDatabaseName());
1195-
$db->createCollection('restaurants');
11961194

11971195
// Start runCommand Example 2
11981196
$cursor = $db->command(['collStats' => 'restaurants']);
@@ -1291,9 +1289,6 @@ public function testTransactions_intro_example_1(): void
12911289

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

1294-
$this->dropCollection('hr', 'employees');
1295-
$this->dropCollection('reporting', 'events');
1296-
12971292
/* Collections need to be created before a transaction starts */
12981293
$this->createCollection('hr', 'employees');
12991294
$this->createCollection('reporting', 'events');
@@ -1470,9 +1465,6 @@ public function testTransactions_retry_example_3(): void
14701465

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

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

14941486
// Prep
1495-
$this->dropCollection('test', 'items');
14961487
$client = static::createTestClient();
1497-
$items = $client->selectDatabase(
1498-
'test',
1499-
['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)]
1500-
)->items;
1488+
$items = $this->createCollection('test', 'items');
15011489
$items->insertOne(
15021490
['sku' => '111', 'name' => 'Peanuts', 'start' => new UTCDateTime()]
15031491
);
@@ -1866,17 +1854,16 @@ public function testQueryableEncryption(): void
18661854
$this->markTestSkipped('Automatic encryption requires MongoDB Enterprise');
18671855
}
18681856

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

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

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