Skip to content

Commit 728c5eb

Browse files
committed
Fix exception handling and assertions in invalid keyId test
1 parent a9b6ab4 commit 728c5eb

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src/Database.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use MongoDB\Driver\ReadConcern;
2626
use MongoDB\Driver\ReadPreference;
2727
use MongoDB\Driver\WriteConcern;
28+
use MongoDB\Exception\CreateEncryptedCollectionException;
2829
use MongoDB\Exception\InvalidArgumentException;
2930
use MongoDB\Exception\UnexpectedValueException;
3031
use MongoDB\Exception\UnsupportedException;
@@ -44,6 +45,7 @@
4445
use MongoDB\Operation\RenameCollection;
4546
use MongoDB\Operation\Watch;
4647
use Traversable;
48+
use Exception;
4749

4850
use function array_key_exists;
4951
use function is_array;

tests/SpecTests/ClientSideEncryption/Prose21_AutomaticDataEncryptionKeysTest.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use MongoDB\Driver\ClientEncryption;
77
use MongoDB\Driver\Exception\BulkWriteException;
88
use MongoDB\Driver\Exception\CommandException;
9+
use MongoDB\Exception\CreateEncryptedCollectionException;
910
use MongoDB\Exception\InvalidArgumentException;
1011

1112
use function base64_decode;
@@ -121,17 +122,23 @@ public function testCase2_MissingEncryptedFields(string $kmsProvider, ?array $ma
121122
*/
122123
public function testCase3_InvalidKeyId(string $kmsProvider, ?array $masterKey): void
123124
{
124-
$this->expectException(CommandException::class);
125-
$this->expectExceptionCode(self::SERVER_ERROR_TYPEMISMATCH);
126-
$this->expectExceptionMessage('keyId');
127-
128-
$this->database->createEncryptedCollection(
129-
$this->getCollectionName(),
130-
$this->clientEncryption,
131-
$kmsProvider,
132-
$masterKey,
133-
['encryptedFields' => ['fields' => [['path' => 'ssn', 'bsonType' => 'string', 'keyId' => false]]]]
134-
);
125+
try {
126+
$this->database->createEncryptedCollection(
127+
$this->getCollectionName(),
128+
$this->clientEncryption,
129+
$kmsProvider,
130+
$masterKey,
131+
['encryptedFields' => ['fields' => [['path' => 'ssn', 'bsonType' => 'string', 'keyId' => false]]]]
132+
);
133+
$this->fail('CreateEncryptedCollectionException was not thrown');
134+
} catch (CreateEncryptedCollectionException $e) {
135+
$this->assertFalse($e->getEncryptedFields()['fields'][0]['keyId'], 'Invalid keyId should not be modified');
136+
137+
$previous = $e->getPrevious();
138+
$this->assertInstanceOf(CommandException::class, $previous);
139+
$this->assertSame(self::SERVER_ERROR_TYPEMISMATCH, $previous->getCode());
140+
$this->assertStringContainsString('keyId', $previous->getMessage());
141+
}
135142
}
136143

137144
/**

0 commit comments

Comments
 (0)