Skip to content

Commit f176d59

Browse files
committed
Prose test 22.2
1 parent f758980 commit f176d59

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

tests/SpecTests/ClientSideEncryption/Prose22_RangeExplicitEncryptionTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
namespace MongoDB\Tests\SpecTests\ClientSideEncryption;
44

5+
use ArrayIterator;
56
use Generator;
7+
use Iterator;
68
use MongoDB\BSON\Binary;
79
use MongoDB\BSON\Decimal128;
810
use MongoDB\BSON\Document;
911
use MongoDB\BSON\UTCDateTime;
1012
use MongoDB\Driver\ClientEncryption;
13+
use MultipleIterator;
1114

1215
use function base64_decode;
1316
use function file_get_contents;
@@ -207,6 +210,59 @@ public function testCase1_CanDecryptAPayload(string $type, array $rangeOpts): vo
207210
$this->assertEquals($originalValue, $decryptedValue);
208211
}
209212

213+
/**
214+
* @see https://github.com/mongodb/specifications/blob/master/source/client-side-encryption/tests/README.rst#case-2-can-find-encrypted-range-and-return-the-maximum
215+
* @dataProvider provideTypeAndRangeOpts
216+
*/
217+
public function testCase2_CanFindEncryptedRangeAndReturnTheMaximum(string $type, array $rangeOpts): void
218+
{
219+
$this->setUpWithTypeAndRangeOpts($type, $rangeOpts);
220+
221+
$encryptOpts = [
222+
'keyId' => $this->key1Id,
223+
'algorithm' => ClientEncryption::ALGORITHM_RANGE_PREVIEW,
224+
'queryType' => ClientEncryption::QUERY_TYPE_RANGE_PREVIEW,
225+
'contentionFactor' => 0,
226+
'rangeOpts' => $rangeOpts,
227+
];
228+
229+
$cast = self::getCastCallableForType($type);
230+
$fieldName = 'encrypted' . $type;
231+
232+
$expr = [
233+
'$and' => [
234+
[$fieldName => ['$gte' => $cast(6)]],
235+
[$fieldName => ['$lte' => $cast(200)]],
236+
],
237+
];
238+
239+
$encryptedExpr = $this->clientEncryption->encryptExpression($expr, $encryptOpts);
240+
$cursor = $this->collection->find($encryptedExpr, ['sort' => ['_id' => 1]]);
241+
242+
$expectedDocuments = [
243+
['_id' => 1, $fieldName => $cast(6)],
244+
['_id' => 2, $fieldName => $cast(30)],
245+
['_id' => 3, $fieldName => $cast(200)],
246+
];
247+
248+
$this->assertMultipleDocumentsMatch($expectedDocuments, $cursor);
249+
}
250+
251+
private function assertMultipleDocumentsMatch(array $expectedDocuments, Iterator $actualDocuments): void
252+
{
253+
$mi = new MultipleIterator(MultipleIterator::MIT_NEED_ANY);
254+
$mi->attachIterator(new ArrayIterator($expectedDocuments));
255+
$mi->attachIterator($actualDocuments);
256+
257+
foreach ($mi as $documents) {
258+
[$expectedDocument, $actualDocument] = $documents;
259+
$this->assertNotNull($expectedDocument);
260+
$this->assertNotNull($actualDocument);
261+
262+
$this->assertDocumentsMatch($expectedDocument, $actualDocument);
263+
}
264+
}
265+
210266
private static function getCastCallableForType(string $type): callable
211267
{
212268
switch ($type) {

0 commit comments

Comments
 (0)