33namespace MongoDB \Tests \Functions ;
44
55use MongoDB \BSON \Binary ;
6+ use MongoDB \BSON \Regex ;
67use MongoDB \Collection ;
78use MongoDB \Database ;
89use MongoDB \Driver \ClientEncryption ;
910use MongoDB \Driver \WriteConcern ;
1011use MongoDB \Tests \FunctionalTestCase ;
1112
12- use function iterator_count ;
13+ use function preg_quote ;
1314use function str_repeat ;
1415
1516class GetEncryptedFieldsFromServerFunctionalTest extends FunctionalTestCase
@@ -30,30 +31,33 @@ public function setUp(): void
3031
3132 $ this ->skipIfServerVersion ('< ' , '7.0.0 ' , 'Queryable encryption requires MongoDB 7.0 or later ' );
3233
33- $ client = static ::createTestClient ();
34+ $ encryptionOptions = [
35+ 'keyVaultNamespace ' => 'keyvault.datakeys ' ,
36+ 'kmsProviders ' => [
37+ 'local ' => [
38+ 'key ' => new Binary (str_repeat ("\0" , 96 )), // 96-byte local master key
39+ ],
40+ ],
41+ ];
42+ $ client = static ::createTestClient (driverOptions: ['autoEncryption ' => $ encryptionOptions ]);
3443
3544 // Ensure the key vault collection is dropped before each test
3645 $ this ->keyVaultCollection = $ client ->getCollection ('keyvault ' , 'datakeys ' , ['writeConcern ' => new WriteConcern (WriteConcern::MAJORITY )]);
3746 $ this ->keyVaultCollection ->drop ();
3847
39- $ this ->clientEncryption = $ client ->createClientEncryption ([
40- 'keyVaultNamespace ' => $ this ->keyVaultCollection ->getNamespace (),
41- 'kmsProviders ' => ['local ' => ['key ' => new Binary (str_repeat ("\0" , 96 )) ]],
42- ]);
48+ $ this ->clientEncryption = $ client ->createClientEncryption ($ encryptionOptions );
4349
4450 $ this ->database = $ client ->getDatabase ($ this ->getDatabaseName ());
4551 }
4652
4753 public function tearDown (): void
4854 {
49- $ this ->keyVaultCollection ->drop ();
55+ $ this ->keyVaultCollection ? ->drop();
5056 }
5157
5258 /** @see https://jira.mongodb.org/browse/PHPLIB-1702 */
5359 public function testDatabaseDropCollectionConsultsEncryptedFieldsFromServer (): void
5460 {
55- $ originalNumCollections = iterator_count ($ this ->database ->listCollectionNames ());
56-
5761 $ this ->database ->createEncryptedCollection (
5862 $ this ->getCollectionName (),
5963 $ this ->clientEncryption ,
@@ -62,19 +66,16 @@ public function testDatabaseDropCollectionConsultsEncryptedFieldsFromServer(): v
6266 ['encryptedFields ' => ['fields ' => []]],
6367 );
6468
65- // createEncryptedCollection should create three collections
66- $ this ->assertCount ($ originalNumCollections + 3 , $ this ->database ->listCollectionNames ());
69+ $ this ->assertCountCollections (3 , $ this ->getCollectionName (), 'createEncryptedCollection should create three collections ' );
6770
6871 $ this ->database ->dropCollection ($ this ->getCollectionName ());
6972
70- $ this ->assertCount ( $ originalNumCollections , $ this ->database -> listCollectionNames ());
73+ $ this ->assertCountCollections ( 0 , $ this ->getCollectionName ());
7174 }
7275
7376 /** @see https://jira.mongodb.org/browse/PHPLIB-1702 */
7477 public function testCollectionDropConsultsEncryptedFieldsFromServer (): void
7578 {
76- $ originalNumCollections = iterator_count ($ this ->database ->listCollectionNames ());
77-
7879 $ this ->database ->createEncryptedCollection (
7980 $ this ->getCollectionName (),
8081 $ this ->clientEncryption ,
@@ -83,11 +84,18 @@ public function testCollectionDropConsultsEncryptedFieldsFromServer(): void
8384 ['encryptedFields ' => ['fields ' => []]],
8485 );
8586
86- // createEncryptedCollection should create three collections
87- $ this ->assertCount ($ originalNumCollections + 3 , $ this ->database ->listCollectionNames ());
87+ $ this ->assertCountCollections (3 , $ this ->getCollectionName (), 'createEncryptedCollection should create three collections ' );
8888
8989 $ this ->database ->getCollection ($ this ->getCollectionName ())->drop ();
9090
91- $ this ->assertCount ($ originalNumCollections , $ this ->database ->listCollectionNames ());
91+ $ this ->assertCountCollections (0 , $ this ->getCollectionName ());
92+ }
93+
94+ private function assertCountCollections (int $ expected , $ collectionName , string $ message = '' ): void
95+ {
96+ $ collectionNames = $ this ->database ->listCollectionNames ([
97+ 'filter ' => ['name ' => new Regex (preg_quote ($ collectionName ))],
98+ ]);
99+ $ this ->assertCount ($ expected , $ collectionNames , $ message );
92100 }
93101}
0 commit comments