Skip to content

Commit 87db4d0

Browse files
committed
Replace getCastCallableForType with a cast function
1 parent 7852790 commit 87db4d0

File tree

1 file changed

+29
-38
lines changed

1 file changed

+29
-38
lines changed

tests/SpecTests/ClientSideEncryption/Prose22_RangeExplicitEncryptionTest.php

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use ArrayIterator;
66
use Generator;
77
use Iterator;
8+
use LogicException;
89
use MongoDB\BSON\Binary;
910
use MongoDB\BSON\Decimal128;
1011
use MongoDB\BSON\Document;
@@ -96,14 +97,13 @@ public function setUpWithTypeAndRangeOpts(string $type, array $rangeOpts): void
9697
'rangeOpts' => $rangeOpts,
9798
];
9899

99-
$cast = self::getCastCallableForType($type);
100100
$fieldName = 'encrypted' . $type;
101101

102102
$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)],
107107
]);
108108
}
109109

@@ -193,16 +193,15 @@ public function testCase1_CanDecryptAPayload(string $type, array $rangeOpts): vo
193193
'rangeOpts' => $rangeOpts,
194194
];
195195

196-
$cast = self::getCastCallableForType($type);
197-
$originalValue = $cast(6);
196+
$originalValue = self::cast($type, 6);
198197

199198
$insertPayload = $this->clientEncryption->encrypt($originalValue, $encryptOpts);
200199
$decryptedValue = $this->clientEncryption->decrypt($insertPayload);
201200

202201
/* Decryption of a 64-bit integer will likely result in a scalar int, so
203202
* cast it back to an Int64 before comparing to the original value. */
204203
if ($type === 'Long' && is_int($decryptedValue)) {
205-
$decryptedValue = $cast($decryptedValue);
204+
$decryptedValue = self::cast($type, $decryptedValue);
206205
}
207206

208207
/* Use separate assertions for type and equality as assertSame isn't
@@ -228,23 +227,22 @@ public function testCase2_CanFindEncryptedRangeAndReturnTheMaximum(string $type,
228227
'rangeOpts' => $rangeOpts,
229228
];
230229

231-
$cast = self::getCastCallableForType($type);
232230
$fieldName = 'encrypted' . $type;
233231

234232
$expr = [
235233
'$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)]],
238236
],
239237
];
240238

241239
$encryptedExpr = $this->clientEncryption->encryptExpression($expr, $encryptOpts);
242240
$cursor = $this->collection->find($encryptedExpr, ['sort' => ['_id' => 1]]);
243241

244242
$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)],
248246
];
249247

250248
$this->assertMultipleDocumentsMatch($expectedDocuments, $cursor);
@@ -266,22 +264,21 @@ public function testCase3_CanFindEncryptedRangeAndReturnTheMinimum(string $type,
266264
'rangeOpts' => $rangeOpts,
267265
];
268266

269-
$cast = self::getCastCallableForType($type);
270267
$fieldName = 'encrypted' . $type;
271268

272269
$expr = [
273270
'$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)]],
276273
],
277274
];
278275

279276
$encryptedExpr = $this->clientEncryption->encryptExpression($expr, $encryptOpts);
280277
$cursor = $this->collection->find($encryptedExpr, ['sort' => ['_id' => 1]]);
281278

282279
$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)],
285282
];
286283

287284
$this->assertMultipleDocumentsMatch($expectedDocuments, $cursor);
@@ -303,14 +300,13 @@ public function testCase4_CanFindEncryptedRangeWithAnOpenRangeQuery(string $type
303300
'rangeOpts' => $rangeOpts,
304301
];
305302

306-
$cast = self::getCastCallableForType($type);
307303
$fieldName = 'encrypted' . $type;
308304

309-
$expr = ['$and' => [[$fieldName => ['$gt' => $cast(30)]]]];
305+
$expr = ['$and' => [[$fieldName => ['$gt' => self::cast($type, 30)]]]];
310306

311307
$encryptedExpr = $this->clientEncryption->encryptExpression($expr, $encryptOpts);
312308
$cursor = $this->collection->find($encryptedExpr, ['sort' => ['_id' => 1]]);
313-
$expectedDocuments = [['_id' => 3, $fieldName => $cast(200)]];
309+
$expectedDocuments = [['_id' => 3, $fieldName => self::cast($type, 200)]];
314310

315311
$this->assertMultipleDocumentsMatch($expectedDocuments, $cursor);
316312
}
@@ -331,18 +327,17 @@ public function testCase5_CanRunAnAggregationExpressionInsideExpr(string $type,
331327
'rangeOpts' => $rangeOpts,
332328
];
333329

334-
$cast = self::getCastCallableForType($type);
335330
$fieldName = 'encrypted' . $type;
336331
$fieldPath = '$' . $fieldName;
337332

338-
$expr = ['$and' => [['$lt' => [$fieldPath, $cast(30)]]]];
333+
$expr = ['$and' => [['$lt' => [$fieldPath, self::cast($type, 30)]]]];
339334

340335
$encryptedExpr = $this->clientEncryption->encryptExpression($expr, $encryptOpts);
341336
$cursor = $this->collection->find(['$expr' => $encryptedExpr], ['sort' => ['_id' => 1]]);
342337

343338
$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)],
346341
];
347342

348343
$this->assertMultipleDocumentsMatch($expectedDocuments, $cursor);
@@ -367,11 +362,9 @@ public function testCase6_EncryptingADocumentGreaterThanTheMaximumErrors(string
367362
'rangeOpts' => $rangeOpts,
368363
];
369364

370-
$cast = self::getCastCallableForType($type);
371-
372365
$this->expectException(EncryptionException::class);
373366
$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);
375368
}
376369

377370
/**
@@ -421,11 +414,9 @@ public function testCase8_SettingPrecisionErrorsIfTheTypeIsNotDoubleOrDecimal128
421414
'rangeOpts' => $rangeOpts + ['precision' => 2],
422415
];
423416

424-
$cast = self::getCastCallableForType($type);
425-
426417
$this->expectException(EncryptionException::class);
427418
$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);
429420
}
430421

431422
private function assertMultipleDocumentsMatch(array $expectedDocuments, Iterator $actualDocuments): void
@@ -443,14 +434,14 @@ private function assertMultipleDocumentsMatch(array $expectedDocuments, Iterator
443434
}
444435
}
445436

446-
private static function getCastCallableForType(string $type): callable
437+
private static function cast(string $type, int $value): mixed
447438
{
448439
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),
454445
default => throw new LogicException('Unsupported type: ' . $type),
455446
};
456447
}

0 commit comments

Comments
 (0)