Skip to content

Commit d4396d3

Browse files
committed
Use consistent db and collection name in example tests to ensure cleanup is done in tearDown
1 parent 7dcdf8b commit d4396d3

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

tests/DocumentationExamplesTest.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,11 +1194,10 @@ public function testRunCommand_example_1(): void
11941194
public function testRunCommand_example_2(): void
11951195
{
11961196
$db = new Database($this->manager, $this->getDatabaseName());
1197-
$db->dropCollection('restaurants');
1198-
$db->createCollection('restaurants');
1197+
$db->createCollection('inventory');
11991198

12001199
// Start runCommand Example 2
1201-
$cursor = $db->command(['collStats' => 'restaurants']);
1200+
$cursor = $db->command(['collStats' => 'inventory']);
12021201
$result = $cursor->toArray()[0];
12031202
// End runCommand Example 2
12041203

@@ -1210,7 +1209,7 @@ public function testIndex_example_1(): void
12101209
$db = new Database($this->manager, $this->getDatabaseName());
12111210

12121211
// Start Index Example 1
1213-
$indexName = $db->records->createIndex(['score' => 1]);
1212+
$indexName = $db->inventory->createIndex(['score' => 1]);
12141213
// End Index Example 1
12151214

12161215
$this->assertEquals('score_1', $indexName);
@@ -1221,7 +1220,7 @@ public function testIndex_example_2(): void
12211220
$db = new Database($this->manager, $this->getDatabaseName());
12221221

12231222
// Start Index Example 2
1224-
$indexName = $db->restaurants->createIndex(
1223+
$indexName = $db->inventory->createIndex(
12251224
['cuisine' => 1, 'name' => 1],
12261225
['partialFilterExpression' => ['rating' => ['$gt' => 5]]]
12271226
);
@@ -1496,13 +1495,14 @@ public function testCausalConsistency(): void
14961495

14971496
// Prep
14981497
$client = static::createTestClient();
1499-
$items = $client->selectDatabase(
1500-
'test',
1498+
$databaseName = $this->getDatabaseName();
1499+
$inventory = $client->selectDatabase(
1500+
$databaseName,
15011501
['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)]
1502-
)->items;
1502+
)->inventory;
15031503

1504-
$items->drop();
1505-
$items->insertOne(
1504+
$inventory->drop();
1505+
$inventory->insertOne(
15061506
['sku' => '111', 'name' => 'Peanuts', 'start' => new UTCDateTime()]
15071507
);
15081508

@@ -1511,33 +1511,33 @@ public function testCausalConsistency(): void
15111511
* mode, so using $manager->selectServer does not work here. To work
15121512
* around this, we run a query on a secondary and rely on an
15131513
* exception to let us know that no secondary is available. */
1514-
$items->countDocuments([], ['readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY)]);
1514+
$inventory->countDocuments([], ['readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY)]);
15151515
} catch (Exception $e) {
15161516
$this->markTestSkipped('Secondary is not available');
15171517
}
15181518

15191519
// phpcs:disable SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly
15201520
// Start Causal Consistency Example 1
1521-
$items = $client->selectDatabase(
1522-
'test',
1521+
$inventory = $client->selectDatabase(
1522+
$databaseName,
15231523
[
15241524
'readConcern' => new \MongoDB\Driver\ReadConcern(\MongoDB\Driver\ReadConcern::MAJORITY),
15251525
'writeConcern' => new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 1000),
15261526
]
1527-
)->items;
1527+
)->inventory;
15281528

15291529
$s1 = $client->startSession(
15301530
['causalConsistency' => true]
15311531
);
15321532

15331533
$currentDate = new \MongoDB\BSON\UTCDateTime();
15341534

1535-
$items->updateOne(
1535+
$inventory->updateOne(
15361536
['sku' => '111', 'end' => ['$exists' => false]],
15371537
['$set' => ['end' => $currentDate]],
15381538
['session' => $s1]
15391539
);
1540-
$items->insertOne(
1540+
$inventory->insertOne(
15411541
['sku' => '111-nuts', 'name' => 'Pecans', 'start' => $currentDate],
15421542
['session' => $s1]
15431543
);
@@ -1554,16 +1554,16 @@ public function testCausalConsistency(): void
15541554
$s2->advanceClusterTime($s1->getClusterTime());
15551555
$s2->advanceOperationTime($s1->getOperationTime());
15561556

1557-
$items = $client->selectDatabase(
1558-
'test',
1557+
$inventory = $client->selectDatabase(
1558+
$databaseName,
15591559
[
15601560
'readPreference' => new \MongoDB\Driver\ReadPreference(\MongoDB\Driver\ReadPreference::RP_SECONDARY),
15611561
'readConcern' => new \MongoDB\Driver\ReadConcern(\MongoDB\Driver\ReadConcern::MAJORITY),
15621562
'writeConcern' => new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 1000),
15631563
]
1564-
)->items;
1564+
)->inventory;
15651565

1566-
$result = $items->find(
1566+
$result = $inventory->find(
15671567
['end' => ['$exists' => false]],
15681568
['session' => $s2]
15691569
);

tests/FunctionalTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,9 @@ protected function createCollection(array $options = []): void
273273
* server, a majority write concern will be used. This is helpful for tests
274274
* using transactions or secondary reads.
275275
*/
276-
protected function dropCollection(array $options = []): void
276+
protected function dropCollection(): void
277277
{
278-
$options += ['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)];
278+
$options = ['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)];
279279

280280
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName(), $options);
281281
$operation->execute($this->getPrimaryServer());

0 commit comments

Comments
 (0)