11<?php
22
3+ declare (strict_types=1 );
4+
35namespace Doctrine \ODM \MongoDB \Tests \Functional ;
46
5- use Doctrine \ODM \MongoDB \Configuration ;
6- use Doctrine \ODM \MongoDB \Mapping \Annotations as ODM ;
7+ use Doctrine \ODM \MongoDB \DocumentManager ;
78use Doctrine \ODM \MongoDB \Tests \BaseTestCase ;
8- use MongoDB \Driver \ClientEncryption ;
9+ use Documents \Encryption \Patient ;
10+ use Documents \Encryption \PatientBilling ;
11+ use Documents \Encryption \PatientRecord ;
12+ use MongoDB \BSON \Binary ;
13+ use MongoDB \Client ;
14+
15+ use function base64_decode ;
16+ use function iterator_to_array ;
917
1018class QueryableEncryptionTest extends BaseTestCase
1119{
12- public function testBasic (): void
20+ private const LOCAL_MASTERKEY = 'quTJGRzz3TS2yrPUzNf9Ajv+rG2cn0buRsWT6i6BTQihznxZkhYKzyagXZZ05+y/FMEV1kpC79reiJSpysytFyEcXXJChjBsH2iTzBK8uWFN2dN7udzYjWvBJmWKbhhm ' ;
21+
22+ public function testCreateAndQueryEncryptedCollection (): void
1323 {
24+ // @todo skip if not using MongoDB < 7, single node or not enterprise
25+ $ client = new Client (self ::getUri ());
26+ $ database = $ client ->getDatabase (DOCTRINE_MONGODB_DATABASE );
1427
15- }
28+ // Create the encrypted collection
29+ $ this ->dm ->getSchemaManager ()->createDocumentCollection (Patient::class);
1630
31+ // Test created collectionss
32+ $ collectionNames = iterator_to_array ($ database ->listCollectionNames ());
33+ self ::assertContains ('patients ' , $ collectionNames );
34+ self ::assertContains ('datakeys ' , $ collectionNames );
1735
18- protected static function getConfiguration (): Configuration
19- {
20- $ config = parent ::getConfiguration ();
36+ // Insert a document
37+ $ patient = new Patient ();
38+ $ patient ->patientName = 'Jon Doe ' ;
39+ $ patient ->patientId = 12345678 ;
40+
41+ $ patientRecord = new PatientRecord ();
42+ $ patientRecord ->ssn = '987-65-4320 ' ;
43+ $ patientRecord ->billingAmount = 1200 ;
44+
45+ $ billing = new PatientBilling ();
46+ $ billing ->type = 'Visa ' ;
47+ $ billing ->number = '4111111111111111 ' ;
48+
49+ $ patientRecord ->billing = $ billing ;
50+ $ patient ->patientRecord = $ patientRecord ;
2151
22- return $ config ;
52+ $ this ->dm ->persist ($ patient );
53+ $ this ->dm ->flush ();
54+ $ this ->dm ->clear ();
55+
56+ // Queryable with equality
57+ $ result = $ this ->dm ->getRepository (Patient::class)->findOneBy (['patientRecord.ssn ' => '987-65-4320 ' ]);
58+ self ::assertNotNull ($ result );
59+ self ::assertSame ('Jon Doe ' , $ result ->patientName );
60+ self ::assertSame ('987-65-4320 ' , $ result ->patientRecord ->ssn );
61+
62+ // Queryable with range
63+ $ result = $ this ->dm ->getRepository (Patient::class)->findOneBy (['patientRecord.billingAmount ' => ['$gt ' => 1000 , '$lt ' => 2000 ]]);
64+ self ::assertSame ('Jon Doe ' , $ result ->patientName );
65+ self ::assertSame ('987-65-4320 ' , $ result ->patientRecord ->ssn );
66+ self ::assertSame ('4111111111111111 ' , $ result ->patientRecord ->billing ->number );
2367 }
24- }
2568
26- #[ODM \Document]
27- class EncryptedDocument
28- {
29- #[ODM \Id]
30- public string $ id ;
69+ protected static function createTestDocumentManager (): DocumentManager
70+ {
71+ $ config = static ::getConfiguration ();
72+ $ config ->setAutoEncryption ([
73+ 'keyVaultNamespace ' => DOCTRINE_MONGODB_DATABASE . '.datakeys ' ,
74+ 'kmsProviders ' => [
75+ 'local ' => ['key ' => new Binary (base64_decode (self ::LOCAL_MASTERKEY ))],
76+ ],
77+ ]);
78+
79+ $ client = new Client (self ::getUri (), [], ['autoEncryption ' => $ config ->getAutoEncryption ()]);
3180
32- #[ODM \Field]
33- #[ODM \Encrypt(queryType: ClientEncryption::QUERY_TYPE_EQUALITY )]
34- private string $ sensitiveField ;
35- }
81+ return DocumentManager::create ($ client , $ config );
82+ }
83+ }
0 commit comments