31
31
*/
32
32
class DocumentationExamplesTest extends FunctionalTestCase
33
33
{
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
-
50
34
public function testExample_1_2 (): void
51
35
{
36
+ $ this ->dropCollection (null , 'inventory ' );
52
37
$ db = new Database ($ this ->manager , $ this ->getDatabaseName ());
53
38
54
39
// Start Example 1
@@ -73,6 +58,7 @@ public function testExample_1_2(): void
73
58
74
59
public function testExample_3 (): void
75
60
{
61
+ $ this ->dropCollection (null , 'inventory ' );
76
62
$ db = new Database ($ this ->manager , $ this ->getDatabaseName ());
77
63
78
64
// Start Example 3
@@ -108,6 +94,7 @@ public function testExample_3(): void
108
94
109
95
public function testExample_6_13 (): void
110
96
{
97
+ $ this ->dropCollection (null , 'inventory ' );
111
98
$ db = new Database ($ this ->manager , $ this ->getDatabaseName ());
112
99
113
100
// Start Example 6
@@ -212,6 +199,7 @@ public function testExample_6_13(): void
212
199
213
200
public function testExample_14_19 (): void
214
201
{
202
+ $ this ->dropCollection (null , 'inventory ' );
215
203
$ db = new Database ($ this ->manager , $ this ->getDatabaseName ());
216
204
217
205
// Start Example 14
@@ -293,6 +281,7 @@ public function testExample_14_19(): void
293
281
294
282
public function testExample_20_28 (): void
295
283
{
284
+ $ this ->dropCollection (null , 'inventory ' );
296
285
$ db = new Database ($ this ->manager , $ this ->getDatabaseName ());
297
286
298
287
// Start Example 20
@@ -400,6 +389,7 @@ public function testExample_20_28(): void
400
389
401
390
public function testExample_29_37 (): void
402
391
{
392
+ $ this ->dropCollection (null , 'inventory ' );
403
393
$ db = new Database ($ this ->manager , $ this ->getDatabaseName ());
404
394
405
395
// Start Example 29
@@ -499,6 +489,7 @@ public function testExample_29_37(): void
499
489
500
490
public function testExample_38_41 (): void
501
491
{
492
+ $ this ->dropCollection (null , 'inventory ' );
502
493
$ db = new Database ($ this ->manager , $ this ->getDatabaseName ());
503
494
504
495
// Start Example 38
@@ -536,6 +527,7 @@ public function testExample_38_41(): void
536
527
537
528
public function testExample_42_50 (): void
538
529
{
530
+ $ this ->dropCollection (null , 'inventory ' );
539
531
$ db = new Database ($ this ->manager , $ this ->getDatabaseName ());
540
532
541
533
// Start Example 42
@@ -741,6 +733,7 @@ public function testExample_42_50(): void
741
733
742
734
public function testExample_51_54 (): void
743
735
{
736
+ $ this ->dropCollection (null , 'inventory ' );
744
737
$ db = new Database ($ this ->manager , $ this ->getDatabaseName ());
745
738
746
739
// Start Example 51
@@ -882,6 +875,7 @@ public function testExample_51_54(): void
882
875
883
876
public function testExample_55_58 (): void
884
877
{
878
+ $ this ->dropCollection (null , 'inventory ' );
885
879
$ db = new Database ($ this ->manager , $ this ->getDatabaseName ());
886
880
887
881
// Start Example 55
@@ -959,8 +953,8 @@ public function testChangeStreamExample_1_4(): void
959
953
$ this ->markTestSkipped ('Test does not apply on sharded clusters: need more than a single getMore call on the change stream. ' );
960
954
}
961
955
956
+ $ this ->dropCollection (null , 'inventory ' );
962
957
$ db = new Database ($ this ->manager , $ this ->getDatabaseName ());
963
- $ db ->dropCollection ('inventory ' );
964
958
$ db ->createCollection ('inventory ' );
965
959
966
960
// Start Changestream Example 1
@@ -1059,6 +1053,7 @@ public function testChangeStreamExample_1_4(): void
1059
1053
1060
1054
public function testAggregation_example_1 (): void
1061
1055
{
1056
+ $ this ->dropCollection (null , 'sales ' );
1062
1057
$ db = new Database ($ this ->manager , $ this ->getDatabaseName ());
1063
1058
1064
1059
// Start Aggregation Example 1
@@ -1073,6 +1068,7 @@ public function testAggregation_example_1(): void
1073
1068
1074
1069
public function testAggregation_example_2 (): void
1075
1070
{
1071
+ $ this ->dropCollection (null , 'sales ' );
1076
1072
$ db = new Database ($ this ->manager , $ this ->getDatabaseName ());
1077
1073
1078
1074
// Start Aggregation Example 2
@@ -1101,6 +1097,7 @@ public function testAggregation_example_2(): void
1101
1097
1102
1098
public function testAggregation_example_3 (): void
1103
1099
{
1100
+ $ this ->dropCollection (null , 'sales ' );
1104
1101
$ db = new Database ($ this ->manager , $ this ->getDatabaseName ());
1105
1102
1106
1103
// Start Aggregation Example 3
@@ -1139,6 +1136,7 @@ public function testAggregation_example_3(): void
1139
1136
1140
1137
public function testAggregation_example_4 (): void
1141
1138
{
1139
+ $ this ->dropCollection (null , 'air_airlines ' );
1142
1140
$ db = new Database ($ this ->manager , $ this ->getDatabaseName ());
1143
1141
1144
1142
// phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
@@ -1191,8 +1189,8 @@ public function testRunCommand_example_1(): void
1191
1189
1192
1190
public function testRunCommand_example_2 (): void
1193
1191
{
1192
+ $ this ->dropCollection (null , 'restaurants ' );
1194
1193
$ db = new Database ($ this ->manager , $ this ->getDatabaseName ());
1195
- $ db ->dropCollection ('restaurants ' );
1196
1194
$ db ->createCollection ('restaurants ' );
1197
1195
1198
1196
// Start runCommand Example 2
@@ -1205,6 +1203,7 @@ public function testRunCommand_example_2(): void
1205
1203
1206
1204
public function testIndex_example_1 (): void
1207
1205
{
1206
+ $ this ->dropCollection (null , 'records ' );
1208
1207
$ db = new Database ($ this ->manager , $ this ->getDatabaseName ());
1209
1208
1210
1209
// Start Index Example 1
@@ -1216,6 +1215,7 @@ public function testIndex_example_1(): void
1216
1215
1217
1216
public function testIndex_example_2 (): void
1218
1217
{
1218
+ $ this ->dropCollection (null , 'restaurants ' );
1219
1219
$ db = new Database ($ this ->manager , $ this ->getDatabaseName ());
1220
1220
1221
1221
// Start Index Example 2
@@ -1234,6 +1234,8 @@ public function testIndex_example_2(): void
1234
1234
// Start Transactions Intro Example 1
1235
1235
private function updateEmployeeInfo1 (\MongoDB \Client $ client , \MongoDB \Driver \Session $ session ): void
1236
1236
{
1237
+ $ this ->dropCollection ('hr ' , 'employees ' );
1238
+ $ this ->dropCollection ('reporting ' , 'events ' );
1237
1239
$ session ->startTransaction ([
1238
1240
'readConcern ' => new \MongoDB \Driver \ReadConcern ('snapshot ' ),
1239
1241
'writeConcern ' => new \MongoDB \Driver \WriteConcern (\MongoDB \Driver \WriteConcern::MAJORITY ),
@@ -1291,8 +1293,8 @@ public function testTransactions_intro_example_1(): void
1291
1293
$ client = static ::createTestClient ();
1292
1294
1293
1295
/* 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 ' );
1296
1298
1297
1299
/* Collections need to be created before a transaction starts */
1298
1300
$ client ->hr ->createCollection ('employees ' , ['writeConcern ' => new WriteConcern ('majority ' )]);
@@ -1471,12 +1473,12 @@ public function testTransactions_retry_example_3(): void
1471
1473
$ client = static ::createTestClient ();
1472
1474
1473
1475
/* 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 ' );
1476
1478
1477
1479
/* 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 ' );
1480
1482
1481
1483
ob_start ();
1482
1484
try {
@@ -1493,13 +1495,12 @@ public function testCausalConsistency(): void
1493
1495
$ this ->assertNotNull ('This test intentionally performs no assertions ' );
1494
1496
1495
1497
// Prep
1498
+ $ this ->dropCollection ('test ' , 'items ' );
1496
1499
$ client = static ::createTestClient ();
1497
1500
$ items = $ client ->selectDatabase (
1498
1501
'test ' ,
1499
1502
['writeConcern ' => new WriteConcern (WriteConcern::MAJORITY )]
1500
1503
)->items ;
1501
-
1502
- $ items ->drop ();
1503
1504
$ items ->insertOne (
1504
1505
['sku ' => '111 ' , 'name ' => 'Peanuts ' , 'start ' => new UTCDateTime ()]
1505
1506
);
@@ -1585,17 +1586,19 @@ public function testSnapshotQueries(): void
1585
1586
$ this ->markTestSkipped ('Snapshot read concern is only supported with replicasets ' );
1586
1587
}
1587
1588
1589
+ $ this ->dropCollection ('pets ' , 'cats ' );
1590
+ $ this ->dropCollection ('pets ' , 'dogs ' );
1591
+ $ this ->dropCollection ('retail ' , 'sales ' );
1592
+
1588
1593
$ client = static ::createTestClient ();
1589
1594
1590
1595
$ catsCollection = $ client ->selectCollection ('pets ' , 'cats ' );
1591
- $ catsCollection ->drop ();
1592
1596
$ catsCollection ->insertMany ([
1593
1597
['name ' => 'Whiskers ' , 'color ' => 'white ' , 'adoptable ' => true ],
1594
1598
['name ' => 'Garfield ' , 'color ' => 'orange ' , 'adoptable ' => false ],
1595
1599
]);
1596
1600
1597
1601
$ dogsCollection = $ client ->selectCollection ('pets ' , 'dogs ' );
1598
- $ dogsCollection ->drop ();
1599
1602
$ dogsCollection ->insertMany ([
1600
1603
['name ' => 'Toto ' , 'color ' => 'black ' , 'adoptable ' => true ],
1601
1604
['name ' => 'Milo ' , 'color ' => 'black ' , 'adoptable ' => false ],
@@ -1645,7 +1648,6 @@ public function testSnapshotQueries(): void
1645
1648
$ dogsCollection ->drop ();
1646
1649
1647
1650
$ salesCollection = $ client ->selectCollection ('retail ' , 'sales ' );
1648
- $ salesCollection ->drop ();
1649
1651
$ salesCollection ->insertMany ([
1650
1652
['shoeType ' => 'boot ' , 'price ' => 30 , 'saleDate ' => new UTCDateTime ()],
1651
1653
]);
@@ -1726,13 +1728,13 @@ public function testVersionedApiMigration(): void
1726
1728
$ this ->markTestSkipped ('The count command was added to API version 1 (SERVER-63850) ' );
1727
1729
}
1728
1730
1731
+ $ this ->dropCollection (null , 'sales ' );
1729
1732
$ uriString = static ::getUri (true );
1730
1733
1731
1734
// phpcs:disable SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly
1732
1735
$ serverApi = new \MongoDB \Driver \ServerApi ('1 ' , true );
1733
1736
$ client = new \MongoDB \Client ($ uriString , [], ['serverApi ' => $ serverApi ]);
1734
1737
$ db = $ client ->selectDatabase ($ this ->getDatabaseName ());
1735
- $ db ->dropCollection ('sales ' );
1736
1738
1737
1739
// Start Versioned API Example 5
1738
1740
$ strtoutc = function (string $ datetime ) {
@@ -1786,6 +1788,8 @@ public function testWithTransactionExample(): void
1786
1788
$ this ->skipIfTransactionsAreNotSupported ();
1787
1789
1788
1790
$ uriString = static ::getUri (true );
1791
+ $ this ->dropCollection ('mydb1 ' , 'foo ' );
1792
+ $ this ->dropCollection ('mydb2 ' , 'bar ' );
1789
1793
1790
1794
// phpcs:disable SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly
1791
1795
// Start Transactions withTxn API Example 1
@@ -1865,6 +1869,8 @@ public function testQueryableEncryption(): void
1865
1869
$ this ->markTestSkipped ('Automatic encryption requires MongoDB Enterprise ' );
1866
1870
}
1867
1871
1872
+ $ this ->dropCollection ();
1873
+
1868
1874
// Fetch names for the database and collection under test
1869
1875
$ collectionName = $ this ->getCollectionName ();
1870
1876
$ databaseName = $ this ->getDatabaseName ();
@@ -1950,22 +1956,14 @@ public function testQueryableEncryption(): void
1950
1956
$ this ->assertInstanceOf (Binary::class, $ result ['encryptedUnindexed ' ]);
1951
1957
}
1952
1958
1953
- /**
1954
- * Return the test collection name.
1955
- */
1956
- protected function getCollectionName (): string
1957
- {
1958
- return 'inventory ' ;
1959
- }
1960
-
1961
1959
private function assertCursorCount ($ count , Cursor $ cursor ): void
1962
1960
{
1963
1961
$ this ->assertCount ($ count , $ cursor ->toArray ());
1964
1962
}
1965
1963
1966
1964
private function assertInventoryCount ($ count ): void
1967
1965
{
1968
- $ this ->assertCollectionCount ($ this ->getDatabaseName () . '. ' . $ this -> getCollectionName () , $ count );
1966
+ $ this ->assertCollectionCount ($ this ->getDatabaseName () . '.inventory ' , $ count );
1969
1967
}
1970
1968
1971
1969
private function waitForSnapshot (string $ databaseName , string $ collectionName ): void
0 commit comments