Skip to content

Commit 5f748fc

Browse files
committed
Apply review
1 parent 5f210b9 commit 5f748fc

File tree

4 files changed

+17
-22
lines changed

4 files changed

+17
-22
lines changed

tests/Collection/FunctionalTestCase.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ public function setUp(): void
1717
{
1818
parent::setUp();
1919

20-
$this->collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName());
21-
22-
$this->dropCollection($this->getDatabaseName(), $this->getCollectionName());
20+
$this->collection = $this->dropCollection($this->getDatabaseName(), $this->getCollectionName());
2321
}
2422
}

tests/DocumentationExamplesTest.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use MongoDB\Driver\Exception\CommandException;
1212
use MongoDB\Driver\Exception\Exception;
1313
use MongoDB\Driver\ReadPreference;
14-
use MongoDB\Driver\WriteConcern;
1514
use MongoDB\Tests\SpecTests\ClientSideEncryptionSpecTest;
1615

1716
use function base64_decode;
@@ -1570,19 +1569,13 @@ public function testSnapshotQueries(): void
15701569
$this->markTestSkipped('Snapshot read concern is only supported with replicasets');
15711570
}
15721571

1573-
$this->dropCollection('pets', 'cats');
1574-
$this->dropCollection('pets', 'dogs');
1575-
$this->dropCollection('retail', 'sales');
1576-
1577-
$client = static::createTestClient();
1578-
1579-
$catsCollection = $client->selectCollection('pets', 'cats');
1572+
$catsCollection = $this->dropCollection('pets', 'cats');
15801573
$catsCollection->insertMany([
15811574
['name' => 'Whiskers', 'color' => 'white', 'adoptable' => true],
15821575
['name' => 'Garfield', 'color' => 'orange', 'adoptable' => false],
15831576
]);
15841577

1585-
$dogsCollection = $client->selectCollection('pets', 'dogs');
1578+
$dogsCollection = $this->dropCollection('pets', 'dogs');
15861579
$dogsCollection->insertMany([
15871580
['name' => 'Toto', 'color' => 'black', 'adoptable' => true],
15881581
['name' => 'Milo', 'color' => 'black', 'adoptable' => false],
@@ -1597,6 +1590,8 @@ public function testSnapshotQueries(): void
15971590
$this->waitForSnapshot('pets', 'dogs');
15981591
}
15991592

1593+
$client = static::createTestClient();
1594+
16001595
ob_start();
16011596

16021597
// Start Snapshot Query Example 1
@@ -1628,10 +1623,7 @@ public function testSnapshotQueries(): void
16281623

16291624
$this->assertSame(3, $adoptablePetsCount);
16301625

1631-
$catsCollection->drop();
1632-
$dogsCollection->drop();
1633-
1634-
$salesCollection = $client->selectCollection('retail', 'sales');
1626+
$salesCollection = $this->dropCollection('retail', 'sales');
16351627
$salesCollection->insertMany([
16361628
['shoeType' => 'boot', 'price' => 30, 'saleDate' => new UTCDateTime()],
16371629
]);
@@ -1670,8 +1662,6 @@ public function testSnapshotQueries(): void
16701662
// End Snapshot Query Example 2
16711663

16721664
$this->assertSame(1, $totalDailySales);
1673-
1674-
$salesCollection->drop();
16751665
}
16761666

16771667
/** @doesNotPerformAssertions */

tests/FunctionalTestCase.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use stdClass;
2121
use UnexpectedValueException;
2222

23+
use function array_intersect_key;
2324
use function call_user_func;
2425
use function count;
2526
use function current;
@@ -266,14 +267,19 @@ public static function getModuleInfo(string $row): ?string
266267
* A majority write concern is applied by default to ensure that the
267268
* transaction can acquire the required locks.
268269
* See: https://www.mongodb.com/docs/manual/core/transactions/#transactions-and-operations
270+
*
271+
* @param array $options CreateCollection options
269272
*/
270273
protected function createCollection(string $databaseName, string $collectionName, array $options = []): Collection
271274
{
275+
// @see https://jira.mongodb.org/browse/PHPLIB-1145
272276
if (isset($options['encryptedFields'])) {
273277
throw new InvalidArgumentException('The "encryptedFields" option is not supported by createCollection(). Time to refactor!');
274278
}
275279

276-
$collection = $this->dropCollection($databaseName, $collectionName, $options);
280+
// Pass only relevant options to drop the collection in case it already exists
281+
$dropOptions = array_intersect_key($options, ['writeConcern' => 1, 'encryptedFields' => 1]);
282+
$collection = $this->dropCollection($databaseName, $collectionName, $dropOptions);
277283

278284
$options += ['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)];
279285
$operation = new CreateCollection($databaseName, $collectionName, $options);
@@ -288,10 +294,12 @@ protected function createCollection(string $databaseName, string $collectionName
288294
* A majority write concern is applied by default to ensure that the
289295
* transaction can acquire the required locks.
290296
* See: https://www.mongodb.com/docs/manual/core/transactions/#transactions-and-operations
297+
*
298+
* @param array $options Collection::dropCollection() options
291299
*/
292300
protected function dropCollection(string $databaseName, string $collectionName, array $options = []): Collection
293301
{
294-
$collection = new Collection($this->manager, $databaseName, $collectionName, $options);
302+
$collection = new Collection($this->manager, $databaseName, $collectionName);
295303
$this->collectionsToCleanup[] = [$collection, $options];
296304

297305
$options += ['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)];

tests/Model/IndexInfoFunctionalTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ public function setUp(): void
1414
{
1515
parent::setUp();
1616

17-
$this->dropCollection($this->getDatabaseName(), $this->getCollectionName());
18-
$this->collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName());
17+
$this->collection = $this->dropCollection($this->getDatabaseName(), $this->getCollectionName());
1918
}
2019

2120
public function testIs2dSphere(): void

0 commit comments

Comments
 (0)