5
5
use ArrayIterator ;
6
6
use Generator ;
7
7
use Iterator ;
8
+ use LogicException ;
8
9
use MongoDB \BSON \Binary ;
9
10
use MongoDB \BSON \Decimal128 ;
10
11
use MongoDB \BSON \Document ;
@@ -96,14 +97,13 @@ public function setUpWithTypeAndRangeOpts(string $type, array $rangeOpts): void
96
97
'rangeOpts ' => $ rangeOpts ,
97
98
];
98
99
99
- $ cast = self ::getCastCallableForType ($ type );
100
100
$ fieldName = 'encrypted ' . $ type ;
101
101
102
102
$ this ->collection ->insertMany ([
103
- ['_id ' => 0 , $ fieldName => $ this ->clientEncryption ->encrypt ($ cast (0 ), $ encryptOpts )],
104
- ['_id ' => 1 , $ fieldName => $ this ->clientEncryption ->encrypt ($ cast (6 ), $ encryptOpts )],
105
- ['_id ' => 2 , $ fieldName => $ this ->clientEncryption ->encrypt ($ cast (30 ), $ encryptOpts )],
106
- ['_id ' => 3 , $ fieldName => $ this ->clientEncryption ->encrypt ($ cast (200 ), $ encryptOpts )],
103
+ ['_id ' => 0 , $ fieldName => $ this ->clientEncryption ->encrypt (self :: cast ($ type , 0 ), $ encryptOpts )],
104
+ ['_id ' => 1 , $ fieldName => $ this ->clientEncryption ->encrypt (self :: cast ($ type , 6 ), $ encryptOpts )],
105
+ ['_id ' => 2 , $ fieldName => $ this ->clientEncryption ->encrypt (self :: cast ($ type , 30 ), $ encryptOpts )],
106
+ ['_id ' => 3 , $ fieldName => $ this ->clientEncryption ->encrypt (self :: cast ($ type , 200 ), $ encryptOpts )],
107
107
]);
108
108
}
109
109
@@ -193,16 +193,15 @@ public function testCase1_CanDecryptAPayload(string $type, array $rangeOpts): vo
193
193
'rangeOpts ' => $ rangeOpts ,
194
194
];
195
195
196
- $ cast = self ::getCastCallableForType ($ type );
197
- $ originalValue = $ cast (6 );
196
+ $ originalValue = self ::cast ($ type , 6 );
198
197
199
198
$ insertPayload = $ this ->clientEncryption ->encrypt ($ originalValue , $ encryptOpts );
200
199
$ decryptedValue = $ this ->clientEncryption ->decrypt ($ insertPayload );
201
200
202
201
/* Decryption of a 64-bit integer will likely result in a scalar int, so
203
202
* cast it back to an Int64 before comparing to the original value. */
204
203
if ($ type === 'Long ' && is_int ($ decryptedValue )) {
205
- $ decryptedValue = $ cast ($ decryptedValue );
204
+ $ decryptedValue = self :: cast ($ type , $ decryptedValue );
206
205
}
207
206
208
207
/* Use separate assertions for type and equality as assertSame isn't
@@ -228,23 +227,22 @@ public function testCase2_CanFindEncryptedRangeAndReturnTheMaximum(string $type,
228
227
'rangeOpts ' => $ rangeOpts ,
229
228
];
230
229
231
- $ cast = self ::getCastCallableForType ($ type );
232
230
$ fieldName = 'encrypted ' . $ type ;
233
231
234
232
$ expr = [
235
233
'$and ' => [
236
- [$ fieldName => ['$gte ' => $ cast (6 )]],
237
- [$ fieldName => ['$lte ' => $ cast (200 )]],
234
+ [$ fieldName => ['$gte ' => self :: cast ($ type , 6 )]],
235
+ [$ fieldName => ['$lte ' => self :: cast ($ type , 200 )]],
238
236
],
239
237
];
240
238
241
239
$ encryptedExpr = $ this ->clientEncryption ->encryptExpression ($ expr , $ encryptOpts );
242
240
$ cursor = $ this ->collection ->find ($ encryptedExpr , ['sort ' => ['_id ' => 1 ]]);
243
241
244
242
$ expectedDocuments = [
245
- ['_id ' => 1 , $ fieldName => $ cast (6 )],
246
- ['_id ' => 2 , $ fieldName => $ cast (30 )],
247
- ['_id ' => 3 , $ fieldName => $ cast (200 )],
243
+ ['_id ' => 1 , $ fieldName => self :: cast ($ type , 6 )],
244
+ ['_id ' => 2 , $ fieldName => self :: cast ($ type , 30 )],
245
+ ['_id ' => 3 , $ fieldName => self :: cast ($ type , 200 )],
248
246
];
249
247
250
248
$ this ->assertMultipleDocumentsMatch ($ expectedDocuments , $ cursor );
@@ -266,22 +264,21 @@ public function testCase3_CanFindEncryptedRangeAndReturnTheMinimum(string $type,
266
264
'rangeOpts ' => $ rangeOpts ,
267
265
];
268
266
269
- $ cast = self ::getCastCallableForType ($ type );
270
267
$ fieldName = 'encrypted ' . $ type ;
271
268
272
269
$ expr = [
273
270
'$and ' => [
274
- [$ fieldName => ['$gte ' => $ cast (0 )]],
275
- [$ fieldName => ['$lte ' => $ cast (6 )]],
271
+ [$ fieldName => ['$gte ' => self :: cast ($ type , 0 )]],
272
+ [$ fieldName => ['$lte ' => self :: cast ($ type , 6 )]],
276
273
],
277
274
];
278
275
279
276
$ encryptedExpr = $ this ->clientEncryption ->encryptExpression ($ expr , $ encryptOpts );
280
277
$ cursor = $ this ->collection ->find ($ encryptedExpr , ['sort ' => ['_id ' => 1 ]]);
281
278
282
279
$ expectedDocuments = [
283
- ['_id ' => 0 , $ fieldName => $ cast (0 )],
284
- ['_id ' => 1 , $ fieldName => $ cast (6 )],
280
+ ['_id ' => 0 , $ fieldName => self :: cast ($ type , 0 )],
281
+ ['_id ' => 1 , $ fieldName => self :: cast ($ type , 6 )],
285
282
];
286
283
287
284
$ this ->assertMultipleDocumentsMatch ($ expectedDocuments , $ cursor );
@@ -303,14 +300,13 @@ public function testCase4_CanFindEncryptedRangeWithAnOpenRangeQuery(string $type
303
300
'rangeOpts ' => $ rangeOpts ,
304
301
];
305
302
306
- $ cast = self ::getCastCallableForType ($ type );
307
303
$ fieldName = 'encrypted ' . $ type ;
308
304
309
- $ expr = ['$and ' => [[$ fieldName => ['$gt ' => $ cast (30 )]]]];
305
+ $ expr = ['$and ' => [[$ fieldName => ['$gt ' => self :: cast ($ type , 30 )]]]];
310
306
311
307
$ encryptedExpr = $ this ->clientEncryption ->encryptExpression ($ expr , $ encryptOpts );
312
308
$ cursor = $ this ->collection ->find ($ encryptedExpr , ['sort ' => ['_id ' => 1 ]]);
313
- $ expectedDocuments = [['_id ' => 3 , $ fieldName => $ cast (200 )]];
309
+ $ expectedDocuments = [['_id ' => 3 , $ fieldName => self :: cast ($ type , 200 )]];
314
310
315
311
$ this ->assertMultipleDocumentsMatch ($ expectedDocuments , $ cursor );
316
312
}
@@ -331,18 +327,17 @@ public function testCase5_CanRunAnAggregationExpressionInsideExpr(string $type,
331
327
'rangeOpts ' => $ rangeOpts ,
332
328
];
333
329
334
- $ cast = self ::getCastCallableForType ($ type );
335
330
$ fieldName = 'encrypted ' . $ type ;
336
331
$ fieldPath = '$ ' . $ fieldName ;
337
332
338
- $ expr = ['$and ' => [['$lt ' => [$ fieldPath , $ cast (30 )]]]];
333
+ $ expr = ['$and ' => [['$lt ' => [$ fieldPath , self :: cast ($ type , 30 )]]]];
339
334
340
335
$ encryptedExpr = $ this ->clientEncryption ->encryptExpression ($ expr , $ encryptOpts );
341
336
$ cursor = $ this ->collection ->find (['$expr ' => $ encryptedExpr ], ['sort ' => ['_id ' => 1 ]]);
342
337
343
338
$ expectedDocuments = [
344
- ['_id ' => 0 , $ fieldName => $ cast (0 )],
345
- ['_id ' => 1 , $ fieldName => $ cast (6 )],
339
+ ['_id ' => 0 , $ fieldName => self :: cast ($ type , 0 )],
340
+ ['_id ' => 1 , $ fieldName => self :: cast ($ type , 6 )],
346
341
];
347
342
348
343
$ this ->assertMultipleDocumentsMatch ($ expectedDocuments , $ cursor );
@@ -367,11 +362,9 @@ public function testCase6_EncryptingADocumentGreaterThanTheMaximumErrors(string
367
362
'rangeOpts ' => $ rangeOpts ,
368
363
];
369
364
370
- $ cast = self ::getCastCallableForType ($ type );
371
-
372
365
$ this ->expectException (EncryptionException::class);
373
366
$ this ->expectExceptionMessage ('Value must be greater than or equal to the minimum value and less than or equal to the maximum value ' );
374
- $ this ->clientEncryption ->encrypt ($ cast (201 ), $ encryptOpts );
367
+ $ this ->clientEncryption ->encrypt (self :: cast ($ type , 201 ), $ encryptOpts );
375
368
}
376
369
377
370
/**
@@ -421,11 +414,9 @@ public function testCase8_SettingPrecisionErrorsIfTheTypeIsNotDoubleOrDecimal128
421
414
'rangeOpts ' => $ rangeOpts + ['precision ' => 2 ],
422
415
];
423
416
424
- $ cast = self ::getCastCallableForType ($ type );
425
-
426
417
$ this ->expectException (EncryptionException::class);
427
418
$ this ->expectExceptionMessage ('expected \'precision \' to be set with double or decimal128 index ' );
428
- $ this ->clientEncryption ->encrypt ($ cast (6 ), $ encryptOpts );
419
+ $ this ->clientEncryption ->encrypt (self :: cast ($ type , 6 ), $ encryptOpts );
429
420
}
430
421
431
422
private function assertMultipleDocumentsMatch (array $ expectedDocuments , Iterator $ actualDocuments ): void
@@ -443,14 +434,14 @@ private function assertMultipleDocumentsMatch(array $expectedDocuments, Iterator
443
434
}
444
435
}
445
436
446
- private static function getCastCallableForType (string $ type ): callable
437
+ private static function cast (string $ type, int $ value ): mixed
447
438
{
448
439
return match ($ type ) {
449
- 'DecimalNoPrecision ' , 'DecimalPrecision ' => fn ( int $ value ) => new Decimal128 ((string ) $ value ),
450
- 'DoubleNoPrecision ' , 'DoublePrecision ' => fn ( int $ value ) => (double ) $ value ,
451
- 'Date ' => fn ( int $ value ) => new UTCDateTime ($ value ),
452
- 'Int ' => fn ( int $ value ) => $ value ,
453
- 'Long ' => fn ( int $ value ) => new Int64 ($ value ),
440
+ 'DecimalNoPrecision ' , 'DecimalPrecision ' => new Decimal128 ((string ) $ value ),
441
+ 'DoubleNoPrecision ' , 'DoublePrecision ' => (double ) $ value ,
442
+ 'Date ' => new UTCDateTime ($ value ),
443
+ 'Int ' => $ value ,
444
+ 'Long ' => new Int64 ($ value ),
454
445
default => throw new LogicException ('Unsupported type: ' . $ type ),
455
446
};
456
447
}
0 commit comments