Skip to content

Commit bef00b3

Browse files
committed
Explicitly drop collection & databases in tests to clean them on tear down
1 parent 339b8a2 commit bef00b3

File tree

4 files changed

+90
-72
lines changed

4 files changed

+90
-72
lines changed

tests/Collection/FunctionalTestCase.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,4 @@ public function setUp(): void
2121

2222
$this->dropCollection();
2323
}
24-
25-
public function tearDown(): void
26-
{
27-
if (! $this->hasFailed()) {
28-
$this->dropCollection();
29-
}
30-
31-
parent::tearDown();
32-
}
3324
}

tests/DocumentationExamplesTest.php

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,9 @@
3131
*/
3232
class DocumentationExamplesTest extends FunctionalTestCase
3333
{
34-
public function setUp(): void
35-
{
36-
parent::setUp();
37-
38-
$this->dropCollection();
39-
}
40-
41-
public function tearDown(): void
42-
{
43-
if (! $this->hasFailed()) {
44-
$this->dropCollection();
45-
}
46-
47-
parent::tearDown();
48-
}
49-
5034
public function testExample_1_2(): void
5135
{
36+
$this->dropCollection(null, 'inventory');
5237
$db = new Database($this->manager, $this->getDatabaseName());
5338

5439
// Start Example 1
@@ -73,6 +58,7 @@ public function testExample_1_2(): void
7358

7459
public function testExample_3(): void
7560
{
61+
$this->dropCollection(null, 'inventory');
7662
$db = new Database($this->manager, $this->getDatabaseName());
7763

7864
// Start Example 3
@@ -108,6 +94,7 @@ public function testExample_3(): void
10894

10995
public function testExample_6_13(): void
11096
{
97+
$this->dropCollection(null, 'inventory');
11198
$db = new Database($this->manager, $this->getDatabaseName());
11299

113100
// Start Example 6
@@ -212,6 +199,7 @@ public function testExample_6_13(): void
212199

213200
public function testExample_14_19(): void
214201
{
202+
$this->dropCollection(null, 'inventory');
215203
$db = new Database($this->manager, $this->getDatabaseName());
216204

217205
// Start Example 14
@@ -293,6 +281,7 @@ public function testExample_14_19(): void
293281

294282
public function testExample_20_28(): void
295283
{
284+
$this->dropCollection(null, 'inventory');
296285
$db = new Database($this->manager, $this->getDatabaseName());
297286

298287
// Start Example 20
@@ -400,6 +389,7 @@ public function testExample_20_28(): void
400389

401390
public function testExample_29_37(): void
402391
{
392+
$this->dropCollection(null, 'inventory');
403393
$db = new Database($this->manager, $this->getDatabaseName());
404394

405395
// Start Example 29
@@ -499,6 +489,7 @@ public function testExample_29_37(): void
499489

500490
public function testExample_38_41(): void
501491
{
492+
$this->dropCollection(null, 'inventory');
502493
$db = new Database($this->manager, $this->getDatabaseName());
503494

504495
// Start Example 38
@@ -536,6 +527,7 @@ public function testExample_38_41(): void
536527

537528
public function testExample_42_50(): void
538529
{
530+
$this->dropCollection(null, 'inventory');
539531
$db = new Database($this->manager, $this->getDatabaseName());
540532

541533
// Start Example 42
@@ -741,6 +733,7 @@ public function testExample_42_50(): void
741733

742734
public function testExample_51_54(): void
743735
{
736+
$this->dropCollection(null, 'inventory');
744737
$db = new Database($this->manager, $this->getDatabaseName());
745738

746739
// Start Example 51
@@ -882,6 +875,7 @@ public function testExample_51_54(): void
882875

883876
public function testExample_55_58(): void
884877
{
878+
$this->dropCollection(null, 'inventory');
885879
$db = new Database($this->manager, $this->getDatabaseName());
886880

887881
// Start Example 55
@@ -959,8 +953,8 @@ public function testChangeStreamExample_1_4(): void
959953
$this->markTestSkipped('Test does not apply on sharded clusters: need more than a single getMore call on the change stream.');
960954
}
961955

956+
$this->dropCollection(null, 'inventory');
962957
$db = new Database($this->manager, $this->getDatabaseName());
963-
$db->dropCollection('inventory');
964958
$db->createCollection('inventory');
965959

966960
// Start Changestream Example 1
@@ -1059,6 +1053,7 @@ public function testChangeStreamExample_1_4(): void
10591053

10601054
public function testAggregation_example_1(): void
10611055
{
1056+
$this->dropCollection(null, 'sales');
10621057
$db = new Database($this->manager, $this->getDatabaseName());
10631058

10641059
// Start Aggregation Example 1
@@ -1073,6 +1068,7 @@ public function testAggregation_example_1(): void
10731068

10741069
public function testAggregation_example_2(): void
10751070
{
1071+
$this->dropCollection(null, 'sales');
10761072
$db = new Database($this->manager, $this->getDatabaseName());
10771073

10781074
// Start Aggregation Example 2
@@ -1101,6 +1097,7 @@ public function testAggregation_example_2(): void
11011097

11021098
public function testAggregation_example_3(): void
11031099
{
1100+
$this->dropCollection(null, 'sales');
11041101
$db = new Database($this->manager, $this->getDatabaseName());
11051102

11061103
// Start Aggregation Example 3
@@ -1139,6 +1136,7 @@ public function testAggregation_example_3(): void
11391136

11401137
public function testAggregation_example_4(): void
11411138
{
1139+
$this->dropCollection(null, 'air_airlines');
11421140
$db = new Database($this->manager, $this->getDatabaseName());
11431141

11441142
// phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
@@ -1191,8 +1189,8 @@ public function testRunCommand_example_1(): void
11911189

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

11981196
// Start runCommand Example 2
@@ -1205,6 +1203,7 @@ public function testRunCommand_example_2(): void
12051203

12061204
public function testIndex_example_1(): void
12071205
{
1206+
$this->dropCollection(null, 'records');
12081207
$db = new Database($this->manager, $this->getDatabaseName());
12091208

12101209
// Start Index Example 1
@@ -1216,6 +1215,7 @@ public function testIndex_example_1(): void
12161215

12171216
public function testIndex_example_2(): void
12181217
{
1218+
$this->dropCollection(null, 'restaurants');
12191219
$db = new Database($this->manager, $this->getDatabaseName());
12201220

12211221
// Start Index Example 2
@@ -1234,6 +1234,8 @@ 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');
12371239
$session->startTransaction([
12381240
'readConcern' => new \MongoDB\Driver\ReadConcern('snapshot'),
12391241
'writeConcern' => new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY),
@@ -1291,8 +1293,8 @@ public function testTransactions_intro_example_1(): void
12911293
$client = static::createTestClient();
12921294

12931295
/* The WC is required: https://mongodb.com/docs/manual/core/transactions/#transactions-and-locks */
1294-
$client->hr->dropCollection('employees', ['writeConcern' => new WriteConcern('majority')]);
1295-
$client->reporting->dropCollection('events', ['writeConcern' => new WriteConcern('majority')]);
1296+
$this->dropCollection('hr', 'employees');
1297+
$this->dropCollection('reporting', 'events');
12961298

12971299
/* Collections need to be created before a transaction starts */
12981300
$client->hr->createCollection('employees', ['writeConcern' => new WriteConcern('majority')]);
@@ -1471,12 +1473,12 @@ public function testTransactions_retry_example_3(): void
14711473
$client = static::createTestClient();
14721474

14731475
/* The WC is required: https://mongodb.com/docs/manual/core/transactions/#transactions-and-locks */
1474-
$client->hr->dropCollection('employees', ['writeConcern' => new WriteConcern('majority')]);
1475-
$client->reporting->dropCollection('events', ['writeConcern' => new WriteConcern('majority')]);
1476+
$this->dropCollection('hr', 'employees');
1477+
$this->dropCollection('reporting', 'events');
14761478

14771479
/* Collections need to be created before a transaction starts */
1478-
$client->hr->createCollection('employees', ['writeConcern' => new WriteConcern('majority')]);
1479-
$client->reporting->createCollection('events', ['writeConcern' => new WriteConcern('majority')]);
1480+
$this->createCollection('hr', 'employees');
1481+
$this->createCollection('reporting', 'events');
14801482

14811483
ob_start();
14821484
try {
@@ -1493,13 +1495,12 @@ public function testCausalConsistency(): void
14931495
$this->assertNotNull('This test intentionally performs no assertions');
14941496

14951497
// Prep
1498+
$this->dropCollection('test', 'items');
14961499
$client = static::createTestClient();
14971500
$items = $client->selectDatabase(
14981501
'test',
14991502
['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)]
15001503
)->items;
1501-
1502-
$items->drop();
15031504
$items->insertOne(
15041505
['sku' => '111', 'name' => 'Peanuts', 'start' => new UTCDateTime()]
15051506
);
@@ -1585,17 +1586,19 @@ public function testSnapshotQueries(): void
15851586
$this->markTestSkipped('Snapshot read concern is only supported with replicasets');
15861587
}
15871588

1589+
$this->dropCollection('pets', 'cats');
1590+
$this->dropCollection('pets', 'dogs');
1591+
$this->dropCollection('retail', 'sales');
1592+
15881593
$client = static::createTestClient();
15891594

15901595
$catsCollection = $client->selectCollection('pets', 'cats');
1591-
$catsCollection->drop();
15921596
$catsCollection->insertMany([
15931597
['name' => 'Whiskers', 'color' => 'white', 'adoptable' => true],
15941598
['name' => 'Garfield', 'color' => 'orange', 'adoptable' => false],
15951599
]);
15961600

15971601
$dogsCollection = $client->selectCollection('pets', 'dogs');
1598-
$dogsCollection->drop();
15991602
$dogsCollection->insertMany([
16001603
['name' => 'Toto', 'color' => 'black', 'adoptable' => true],
16011604
['name' => 'Milo', 'color' => 'black', 'adoptable' => false],
@@ -1645,7 +1648,6 @@ public function testSnapshotQueries(): void
16451648
$dogsCollection->drop();
16461649

16471650
$salesCollection = $client->selectCollection('retail', 'sales');
1648-
$salesCollection->drop();
16491651
$salesCollection->insertMany([
16501652
['shoeType' => 'boot', 'price' => 30, 'saleDate' => new UTCDateTime()],
16511653
]);
@@ -1726,13 +1728,13 @@ public function testVersionedApiMigration(): void
17261728
$this->markTestSkipped('The count command was added to API version 1 (SERVER-63850)');
17271729
}
17281730

1731+
$this->dropCollection(null, 'sales');
17291732
$uriString = static::getUri(true);
17301733

17311734
// phpcs:disable SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly
17321735
$serverApi = new \MongoDB\Driver\ServerApi('1', true);
17331736
$client = new \MongoDB\Client($uriString, [], ['serverApi' => $serverApi]);
17341737
$db = $client->selectDatabase($this->getDatabaseName());
1735-
$db->dropCollection('sales');
17361738

17371739
// Start Versioned API Example 5
17381740
$strtoutc = function (string $datetime) {
@@ -1786,6 +1788,8 @@ public function testWithTransactionExample(): void
17861788
$this->skipIfTransactionsAreNotSupported();
17871789

17881790
$uriString = static::getUri(true);
1791+
$this->dropCollection('mydb1', 'foo');
1792+
$this->dropCollection('mydb2', 'bar');
17891793

17901794
// phpcs:disable SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly
17911795
// Start Transactions withTxn API Example 1
@@ -1865,6 +1869,8 @@ public function testQueryableEncryption(): void
18651869
$this->markTestSkipped('Automatic encryption requires MongoDB Enterprise');
18661870
}
18671871

1872+
$this->dropCollection();
1873+
18681874
// Fetch names for the database and collection under test
18691875
$collectionName = $this->getCollectionName();
18701876
$databaseName = $this->getDatabaseName();
@@ -1950,22 +1956,14 @@ public function testQueryableEncryption(): void
19501956
$this->assertInstanceOf(Binary::class, $result['encryptedUnindexed']);
19511957
}
19521958

1953-
/**
1954-
* Return the test collection name.
1955-
*/
1956-
protected function getCollectionName(): string
1957-
{
1958-
return 'inventory';
1959-
}
1960-
19611959
private function assertCursorCount($count, Cursor $cursor): void
19621960
{
19631961
$this->assertCount($count, $cursor->toArray());
19641962
}
19651963

19661964
private function assertInventoryCount($count): void
19671965
{
1968-
$this->assertCollectionCount($this->getDatabaseName() . '.' . $this->getCollectionName(), $count);
1966+
$this->assertCollectionCount($this->getDatabaseName() . '.inventory', $count);
19691967
}
19701968

19711969
private function waitForSnapshot(string $databaseName, string $collectionName): void

0 commit comments

Comments
 (0)