12
12
use MongoDB \Driver \Exception \Exception ;
13
13
use MongoDB \Driver \ReadPreference ;
14
14
use MongoDB \Driver \WriteConcern ;
15
+ use MongoDB \Operation \DropDatabase ;
15
16
use MongoDB \Tests \SpecTests \ClientSideEncryptionSpecTest ;
16
17
18
+ use function array_unique ;
17
19
use function base64_decode ;
18
20
use function in_array ;
19
21
use function microtime ;
28
30
* @see https://jira.mongodb.org/browse/DRIVERS-356
29
31
* @see https://jira.mongodb.org/browse/DRIVERS-488
30
32
* @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.
31
35
*/
32
36
class DocumentationExamplesTest extends FunctionalTestCase
33
37
{
38
+ /** @var string[] Name of the databases created during the test that will be dropped in tearDown */
39
+ private $ dbNames = [];
40
+
34
41
public function setUp (): void
35
42
{
36
43
parent ::setUp ();
@@ -46,6 +53,13 @@ public function tearDown(): void
46
53
47
54
$ this ->dropCollection ();
48
55
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
+
49
63
parent ::tearDown ();
50
64
}
51
65
@@ -1235,6 +1249,7 @@ public function testIndex_example_2(): void
1235
1249
// Start Transactions Intro Example 1
1236
1250
private function updateEmployeeInfo1 (\MongoDB \Client $ client , \MongoDB \Driver \Session $ session ): void
1237
1251
{
1252
+ $ this ->dbNames = ['hr ' , 'reporting ' ];
1238
1253
$ session ->startTransaction ([
1239
1254
'readConcern ' => new \MongoDB \Driver \ReadConcern ('snapshot ' ),
1240
1255
'writeConcern ' => new \MongoDB \Driver \WriteConcern (\MongoDB \Driver \WriteConcern::MAJORITY ),
@@ -1291,6 +1306,8 @@ public function testTransactions_intro_example_1(): void
1291
1306
1292
1307
$ client = static ::createTestClient ();
1293
1308
1309
+ $ this ->dbNames = ['hr ' , 'reporting ' ];
1310
+
1294
1311
/* The WC is required: https://mongodb.com/docs/manual/core/transactions/#transactions-and-locks */
1295
1312
$ client ->hr ->dropCollection ('employees ' , ['writeConcern ' => new WriteConcern ('majority ' )]);
1296
1313
$ client ->reporting ->dropCollection ('events ' , ['writeConcern ' => new WriteConcern ('majority ' )]);
@@ -1451,6 +1468,8 @@ private function updateEmployeeInfo3(\MongoDB\Client $client, \MongoDB\Driver\Se
1451
1468
1452
1469
private function doUpdateEmployeeInfo (\MongoDB \Client $ client ): void
1453
1470
{
1471
+ $ this ->dbNames = ['hr ' , 'reporting ' ];
1472
+
1454
1473
// Start a session.
1455
1474
$ session = $ client ->startSession ();
1456
1475
@@ -1471,6 +1490,8 @@ public function testTransactions_retry_example_3(): void
1471
1490
1472
1491
$ client = static ::createTestClient ();
1473
1492
1493
+ $ this ->dbNames = ['hr ' , 'reporting ' ];
1494
+
1474
1495
/* The WC is required: https://mongodb.com/docs/manual/core/transactions/#transactions-and-locks */
1475
1496
$ client ->hr ->dropCollection ('employees ' , ['writeConcern ' => new WriteConcern ('majority ' )]);
1476
1497
$ client ->reporting ->dropCollection ('events ' , ['writeConcern ' => new WriteConcern ('majority ' )]);
@@ -1788,6 +1809,7 @@ public function testWithTransactionExample(): void
1788
1809
$ this ->skipIfTransactionsAreNotSupported ();
1789
1810
1790
1811
$ uriString = static ::getUri (true );
1812
+ $ this ->dbNames = ['mydb1 ' , 'mydb2 ' ];
1791
1813
1792
1814
// phpcs:disable SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly
1793
1815
// Start Transactions withTxn API Example 1
0 commit comments