Skip to content

Commit 25bd1b0

Browse files
committed
Cleanup extra databases used by transaction examples tests
1 parent d4396d3 commit 25bd1b0

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

tests/DocumentationExamplesTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
use MongoDB\Driver\Exception\Exception;
1313
use MongoDB\Driver\ReadPreference;
1414
use MongoDB\Driver\WriteConcern;
15+
use MongoDB\Operation\DropDatabase;
1516
use MongoDB\Tests\SpecTests\ClientSideEncryptionSpecTest;
1617

18+
use function array_unique;
1719
use function base64_decode;
1820
use function in_array;
1921
use function microtime;
@@ -28,9 +30,14 @@
2830
* @see https://jira.mongodb.org/browse/DRIVERS-356
2931
* @see https://jira.mongodb.org/browse/DRIVERS-488
3032
* @see https://jira.mongodb.org/browse/DRIVERS-547
33+
*
34+
* Note: Example should use the "inventory" collection in the default database unless they need multiple databases.
3135
*/
3236
class DocumentationExamplesTest extends FunctionalTestCase
3337
{
38+
/** @var string[] Name of the databases created during the test that will be dropped in tearDown */
39+
private $dbNames = [];
40+
3441
public function setUp(): void
3542
{
3643
parent::setUp();
@@ -46,6 +53,13 @@ public function tearDown(): void
4653

4754
$this->dropCollection();
4855

56+
foreach (array_unique($this->dbNames) as $dbName) {
57+
$options = ['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)];
58+
59+
$operation = new DropDatabase($dbName, $options);
60+
$operation->execute($this->getPrimaryServer());
61+
}
62+
4963
parent::tearDown();
5064
}
5165

@@ -1235,6 +1249,7 @@ public function testIndex_example_2(): void
12351249
// Start Transactions Intro Example 1
12361250
private function updateEmployeeInfo1(\MongoDB\Client $client, \MongoDB\Driver\Session $session): void
12371251
{
1252+
$this->dbNames = ['hr', 'reporting'];
12381253
$session->startTransaction([
12391254
'readConcern' => new \MongoDB\Driver\ReadConcern('snapshot'),
12401255
'writeConcern' => new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY),
@@ -1291,6 +1306,8 @@ public function testTransactions_intro_example_1(): void
12911306

12921307
$client = static::createTestClient();
12931308

1309+
$this->dbNames = ['hr', 'reporting'];
1310+
12941311
/* The WC is required: https://mongodb.com/docs/manual/core/transactions/#transactions-and-locks */
12951312
$client->hr->dropCollection('employees', ['writeConcern' => new WriteConcern('majority')]);
12961313
$client->reporting->dropCollection('events', ['writeConcern' => new WriteConcern('majority')]);
@@ -1451,6 +1468,8 @@ private function updateEmployeeInfo3(\MongoDB\Client $client, \MongoDB\Driver\Se
14511468

14521469
private function doUpdateEmployeeInfo(\MongoDB\Client $client): void
14531470
{
1471+
$this->dbNames = ['hr', 'reporting'];
1472+
14541473
// Start a session.
14551474
$session = $client->startSession();
14561475

@@ -1471,6 +1490,8 @@ public function testTransactions_retry_example_3(): void
14711490

14721491
$client = static::createTestClient();
14731492

1493+
$this->dbNames = ['hr', 'reporting'];
1494+
14741495
/* The WC is required: https://mongodb.com/docs/manual/core/transactions/#transactions-and-locks */
14751496
$client->hr->dropCollection('employees', ['writeConcern' => new WriteConcern('majority')]);
14761497
$client->reporting->dropCollection('events', ['writeConcern' => new WriteConcern('majority')]);
@@ -1788,6 +1809,7 @@ public function testWithTransactionExample(): void
17881809
$this->skipIfTransactionsAreNotSupported();
17891810

17901811
$uriString = static::getUri(true);
1812+
$this->dbNames = ['mydb1', 'mydb2'];
17911813

17921814
// phpcs:disable SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly
17931815
// Start Transactions withTxn API Example 1

0 commit comments

Comments
 (0)