3
3
namespace MongoDB \Tests \Functions ;
4
4
5
5
use MongoDB \BSON \Binary ;
6
+ use MongoDB \BSON \Regex ;
6
7
use MongoDB \Collection ;
7
8
use MongoDB \Database ;
8
9
use MongoDB \Driver \ClientEncryption ;
9
10
use MongoDB \Driver \WriteConcern ;
10
11
use MongoDB \Tests \FunctionalTestCase ;
11
12
12
- use function iterator_count ;
13
+ use function preg_quote ;
13
14
use function str_repeat ;
14
15
15
16
class GetEncryptedFieldsFromServerFunctionalTest extends FunctionalTestCase
@@ -30,30 +31,33 @@ public function setUp(): void
30
31
31
32
$ this ->skipIfServerVersion ('< ' , '7.0.0 ' , 'Queryable encryption requires MongoDB 7.0 or later ' );
32
33
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 ]);
34
43
35
44
// Ensure the key vault collection is dropped before each test
36
45
$ this ->keyVaultCollection = $ client ->getCollection ('keyvault ' , 'datakeys ' , ['writeConcern ' => new WriteConcern (WriteConcern::MAJORITY )]);
37
46
$ this ->keyVaultCollection ->drop ();
38
47
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 );
43
49
44
50
$ this ->database = $ client ->getDatabase ($ this ->getDatabaseName ());
45
51
}
46
52
47
53
public function tearDown (): void
48
54
{
49
- $ this ->keyVaultCollection ->drop ();
55
+ $ this ->keyVaultCollection ? ->drop();
50
56
}
51
57
52
58
/** @see https://jira.mongodb.org/browse/PHPLIB-1702 */
53
59
public function testDatabaseDropCollectionConsultsEncryptedFieldsFromServer (): void
54
60
{
55
- $ originalNumCollections = iterator_count ($ this ->database ->listCollectionNames ());
56
-
57
61
$ this ->database ->createEncryptedCollection (
58
62
$ this ->getCollectionName (),
59
63
$ this ->clientEncryption ,
@@ -62,19 +66,16 @@ public function testDatabaseDropCollectionConsultsEncryptedFieldsFromServer(): v
62
66
['encryptedFields ' => ['fields ' => []]],
63
67
);
64
68
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 ' );
67
70
68
71
$ this ->database ->dropCollection ($ this ->getCollectionName ());
69
72
70
- $ this ->assertCount ( $ originalNumCollections , $ this ->database -> listCollectionNames ());
73
+ $ this ->assertCountCollections ( 0 , $ this ->getCollectionName ());
71
74
}
72
75
73
76
/** @see https://jira.mongodb.org/browse/PHPLIB-1702 */
74
77
public function testCollectionDropConsultsEncryptedFieldsFromServer (): void
75
78
{
76
- $ originalNumCollections = iterator_count ($ this ->database ->listCollectionNames ());
77
-
78
79
$ this ->database ->createEncryptedCollection (
79
80
$ this ->getCollectionName (),
80
81
$ this ->clientEncryption ,
@@ -83,11 +84,18 @@ public function testCollectionDropConsultsEncryptedFieldsFromServer(): void
83
84
['encryptedFields ' => ['fields ' => []]],
84
85
);
85
86
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 ' );
88
88
89
89
$ this ->database ->getCollection ($ this ->getCollectionName ())->drop ();
90
90
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 );
92
100
}
93
101
}
0 commit comments