Skip to content

Commit ab0da3c

Browse files
committed
Keep options to drop encrypted collection on tearDown
1 parent 8f62c6f commit ab0da3c

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

tests/DocumentationExamplesTest.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1853,19 +1853,18 @@ public function testQueryableEncryption(): void
18531853
$this->markTestSkipped('Automatic encryption requires MongoDB Enterprise');
18541854
}
18551855

1856-
// Ensure the collection is dropped by tearDown()
1857-
$this->dropCollection($this->getDatabaseName(), $this->getCollectionName());
1858-
18591856
// Fetch names for the database and collection under test
18601857
$collectionName = $this->getCollectionName();
18611858
$databaseName = $this->getDatabaseName();
18621859
$namespace = $this->getNamespace();
18631860

1864-
/* Create a client without auto encryption. Drop existing data in both the keyvault and database under test.
1865-
* The latter is necessary to clean up any internal collections for queryable encryption. */
1861+
/* Create a client without auto encryption. Drop existing data in both the
1862+
* keyvault and the collection under test, the "encryptedFields" option
1863+
* is necessary to clean up any internal collections for queryable
1864+
* encryption, assuming they use their default names */
18661865
$client = static::createTestClient();
1867-
$client->selectDatabase('keyvault')->drop(['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)]);
1868-
$client->selectDatabase($databaseName)->drop(['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)]);
1866+
$this->dropCollection('keyvault', 'datakeys');
1867+
$this->dropCollection($databaseName, $collectionName, ['encryptedFields' => []]);
18691868

18701869
/* Although ClientEncryption can be constructed directly, the library
18711870
* provides a helper to do so. With this method, the keyVaultClient will

tests/FunctionalTestCase.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ abstract class FunctionalTestCase extends TestCase
5454
/** @var array */
5555
private $configuredFailPoints = [];
5656

57-
/** @var Collection[] */
57+
/** @var array{int,{Collection,array}} */
5858
private $collectionsToCleanup = [];
5959

6060
public function setUp(): void
@@ -271,6 +271,10 @@ protected function createCollection(string $databaseName, string $collectionName
271271
{
272272
$collection = $this->dropCollection($databaseName, $collectionName, $options);
273273

274+
if (isset($options['encryptedFields'])) {
275+
throw new InvalidArgumentException('The "encryptedFields" option is not supported by createCollection(). Time to refactor!');
276+
}
277+
274278
$operation = new CreateCollection($databaseName, $collectionName, $options);
275279
$operation->execute($this->getPrimaryServer());
276280

@@ -288,16 +292,17 @@ protected function dropCollection(string $databaseName, string $collectionName,
288292
{
289293
$options += ['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)];
290294
$collection = new Collection($this->manager, $databaseName, $collectionName, $options);
291-
$this->collectionsToCleanup[] = $collection;
292295
$collection->drop();
293296

297+
$this->collectionsToCleanup[] = [$collection, $options];
298+
294299
return $collection;
295300
}
296301

297302
private function cleanupCollections(): void
298303
{
299-
foreach ($this->collectionsToCleanup as $collection) {
300-
$collection->drop();
304+
foreach ($this->collectionsToCleanup as [$collection, $options]) {
305+
$collection->drop($options);
301306
}
302307

303308
$this->collectionsToCleanup = [];

0 commit comments

Comments
 (0)