1010use Documents \Encryption \PatientBilling ;
1111use Documents \Encryption \PatientRecord ;
1212use MongoDB \BSON \Binary ;
13+ use MongoDB \BSON \Regex ;
1314use MongoDB \Client ;
1415use MongoDB \Model \BSONDocument ;
1516
16- use function count ;
1717use function getenv ;
1818use function iterator_to_array ;
1919use function random_bytes ;
@@ -27,6 +27,13 @@ public function setUp(): void
2727 $ this ->skipTestIfQueryableEncryptionNotSupported ();
2828 }
2929
30+ public function tearDown (): void
31+ {
32+ $ this ->dm ?->getDocumentCollection(Patient::class)?->drop(['encryptedFields ' => []]);
33+
34+ parent ::tearDown ();
35+ }
36+
3037 public function testCreateAndQueryEncryptedCollection (): void
3138 {
3239 $ nonEncryptedClient = new Client (self ::getUri ());
@@ -64,8 +71,11 @@ public function testCreateAndQueryEncryptedCollection(): void
6471 self ::assertSame ('Jon Doe ' , $ document ->patientName );
6572 self ::assertSame (12345678 , $ document ->patientId );
6673 self ::assertInstanceOf (Binary::class, $ document ->patientRecord ->ssn );
74+ self ::assertSame (Binary::TYPE_ENCRYPTED , $ document ->patientRecord ->ssn ->getType ());
6775 self ::assertInstanceOf (Binary::class, $ document ->patientRecord ->billing );
76+ self ::assertSame (Binary::TYPE_ENCRYPTED , $ document ->patientRecord ->billing ->getType ());
6877 self ::assertInstanceOf (Binary::class, $ document ->patientRecord ->billingAmount );
78+ self ::assertSame (Binary::TYPE_ENCRYPTED , $ document ->patientRecord ->billingAmount ->getType ());
6979
7080 // Queryable with equality
7181 $ result = $ this ->dm ->getRepository (Patient::class)->findOneBy (['patientRecord.ssn ' => '987-65-4320 ' ]);
@@ -84,11 +94,9 @@ public function testCreateAndQueryEncryptedCollection(): void
8494 self ::assertSame ('4111111111111111 ' , $ result ->patientRecord ->billing ->number );
8595
8696 // Drop the encrypted collection
87- $ collectionCount = count ($ nonEncryptedDatabase ->listCollectionNames ());
8897 $ this ->dm ->getSchemaManager ()->dropDocumentCollection (Patient::class);
89- $ collectionNames = iterator_to_array ($ nonEncryptedDatabase ->listCollectionNames ());
90- self ::assertNotContains ('patients ' , $ collectionNames );
91- self ::assertSame ($ collectionCount - 3 , count ($ collectionNames ), 'The 2 metadata collections should also be dropped ' );
98+ $ collectionNames = iterator_to_array ($ nonEncryptedDatabase ->listCollectionNames (['filter ' => ['name ' => new Regex ('patients ' )]]));
99+ self ::assertSame ([], $ collectionNames , 'The 2 metadata collections should also be dropped ' );
92100 }
93101
94102 protected static function createTestDocumentManager (): DocumentManager
0 commit comments