Skip to content

Commit 18e590e

Browse files
committed
Apply review
1 parent 368100d commit 18e590e

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;
@@ -1571,19 +1570,13 @@ public function testSnapshotQueries(): void
15711570
$this->markTestSkipped('Snapshot read concern is only supported with replicasets');
15721571
}
15731572

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

1586-
$dogsCollection = $client->selectCollection('pets', 'dogs');
1579+
$dogsCollection = $this->dropCollection('pets', 'dogs');
15871580
$dogsCollection->insertMany([
15881581
['name' => 'Toto', 'color' => 'black', 'adoptable' => true],
15891582
['name' => 'Milo', 'color' => 'black', 'adoptable' => false],
@@ -1598,6 +1591,8 @@ public function testSnapshotQueries(): void
15981591
$this->waitForSnapshot('pets', 'dogs');
15991592
}
16001593

1594+
$client = static::createTestClient();
1595+
16011596
ob_start();
16021597

16031598
// Start Snapshot Query Example 1
@@ -1629,10 +1624,7 @@ public function testSnapshotQueries(): void
16291624

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

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

16731665
$this->assertSame(1, $totalDailySales);
1674-
1675-
$salesCollection->drop();
16761666
}
16771667

16781668
/** @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)