From 028bd7e7b7e0300befe68958b840aa2b5d22294e Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Tue, 27 Jun 2023 10:24:57 +0200 Subject: [PATCH] Introduce new exception factory for expected document type --- src/Command/ListCollections.php | 2 +- src/Command/ListDatabases.php | 2 +- src/Exception/InvalidArgumentException.php | 11 +++++++++ src/GridFS/WritableStream.php | 2 +- src/Model/ChangeStreamIterator.php | 4 ++-- src/Model/IndexInput.php | 2 +- src/Operation/Aggregate.php | 4 ++-- src/Operation/BulkWrite.php | 12 +++++----- src/Operation/Count.php | 4 ++-- src/Operation/CountDocuments.php | 2 +- src/Operation/CreateCollection.php | 16 ++++++------- src/Operation/CreateEncryptedCollection.php | 2 +- src/Operation/DatabaseCommand.php | 2 +- src/Operation/Delete.php | 6 ++--- src/Operation/Distinct.php | 4 ++-- src/Operation/DropEncryptedCollection.php | 2 +- src/Operation/Find.php | 16 ++++++------- src/Operation/FindAndModify.php | 10 ++++---- src/Operation/FindOneAndDelete.php | 4 ++-- src/Operation/FindOneAndReplace.php | 6 ++--- src/Operation/FindOneAndUpdate.php | 4 ++-- src/Operation/InsertMany.php | 2 +- src/Operation/InsertOne.php | 2 +- src/Operation/MapReduce.php | 8 +++---- src/Operation/ReplaceOne.php | 2 +- src/Operation/Update.php | 6 ++--- src/Operation/Watch.php | 4 ++-- src/functions.php | 4 ++-- tests/FunctionsTest.php | 2 +- tests/Operation/BulkWriteTest.php | 26 ++++++++++----------- tests/Operation/CreateIndexesTest.php | 2 +- tests/Operation/FindOneAndReplaceTest.php | 2 +- tests/Operation/ReplaceOneTest.php | 2 +- tests/Operation/UpdateTest.php | 2 +- 34 files changed, 96 insertions(+), 85 deletions(-) diff --git a/src/Command/ListCollections.php b/src/Command/ListCollections.php index 6e6c869d7..7b6e84489 100644 --- a/src/Command/ListCollections.php +++ b/src/Command/ListCollections.php @@ -79,7 +79,7 @@ public function __construct(string $databaseName, array $options = []) } if (isset($options['filter']) && ! is_document($options['filter'])) { - throw InvalidArgumentException::invalidType('"filter" option', $options['filter'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"filter" option', $options['filter']); } if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) { diff --git a/src/Command/ListDatabases.php b/src/Command/ListDatabases.php index 14cb31791..94e1f8750 100644 --- a/src/Command/ListDatabases.php +++ b/src/Command/ListDatabases.php @@ -77,7 +77,7 @@ public function __construct(array $options = []) } if (isset($options['filter']) && ! is_document($options['filter'])) { - throw InvalidArgumentException::invalidType('"filter" option', $options['filter'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"filter" option', $options['filter']); } if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) { diff --git a/src/Exception/InvalidArgumentException.php b/src/Exception/InvalidArgumentException.php index 890eb6c16..7752aaf37 100644 --- a/src/Exception/InvalidArgumentException.php +++ b/src/Exception/InvalidArgumentException.php @@ -46,6 +46,17 @@ public static function invalidType(string $name, $value, $expectedType) return new static(sprintf('Expected %s to have type "%s" but found "%s"', $name, $expectedType, get_debug_type($value))); } + /** + * Thrown when an argument or option is expected to be a document. + * + * @param string $name Name of the argument or option + * @param mixed $value Actual value (used to derive the type) + */ + public static function expectedDocumentType(string $name, $value): self + { + return new self(sprintf('Expected %s to have type "document" (array or object) but found "%s"', $name, get_debug_type($value))); + } + /** @param list $types */ private static function expectedTypesToString(array $types): string { diff --git a/src/GridFS/WritableStream.php b/src/GridFS/WritableStream.php index 5919ee129..981ce5312 100644 --- a/src/GridFS/WritableStream.php +++ b/src/GridFS/WritableStream.php @@ -130,7 +130,7 @@ public function __construct(CollectionWrapper $collectionWrapper, string $filena } if (isset($options['metadata']) && ! is_document($options['metadata'])) { - throw InvalidArgumentException::invalidType('"metadata" option', $options['metadata'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"metadata" option', $options['metadata']); } $this->chunkSize = $options['chunkSizeBytes']; diff --git a/src/Model/ChangeStreamIterator.php b/src/Model/ChangeStreamIterator.php index 6b2fa61a2..c34e07d4e 100644 --- a/src/Model/ChangeStreamIterator.php +++ b/src/Model/ChangeStreamIterator.php @@ -77,7 +77,7 @@ class ChangeStreamIterator extends IteratorIterator implements CommandSubscriber public function __construct(Cursor $cursor, int $firstBatchSize, $initialResumeToken, ?object $postBatchResumeToken) { if (isset($initialResumeToken) && ! is_document($initialResumeToken)) { - throw InvalidArgumentException::invalidType('$initialResumeToken', $initialResumeToken, 'document'); + throw InvalidArgumentException::expectedDocumentType('$initialResumeToken', $initialResumeToken); } parent::__construct($cursor); @@ -236,7 +236,7 @@ public function valid(): bool private function extractResumeToken($document) { if (! is_document($document)) { - throw InvalidArgumentException::invalidType('$document', $document, 'document'); + throw InvalidArgumentException::expectedDocumentType('$document', $document); } if ($document instanceof Serializable) { diff --git a/src/Model/IndexInput.php b/src/Model/IndexInput.php index c8a769b53..b95fbfc1f 100644 --- a/src/Model/IndexInput.php +++ b/src/Model/IndexInput.php @@ -53,7 +53,7 @@ public function __construct(array $index) } if (! is_document($index['key'])) { - throw InvalidArgumentException::invalidType('"key" option', $index['key'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"key" option', $index['key']); } foreach ($index['key'] as $fieldName => $order) { diff --git a/src/Operation/Aggregate.php b/src/Operation/Aggregate.php index acba69169..1916f8141 100644 --- a/src/Operation/Aggregate.php +++ b/src/Operation/Aggregate.php @@ -157,7 +157,7 @@ public function __construct(string $databaseName, ?string $collectionName, array } if (isset($options['collation']) && ! is_document($options['collation'])) { - throw InvalidArgumentException::invalidType('"collation" option', $options['collation'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"collation" option', $options['collation']); } if (isset($options['explain']) && ! is_bool($options['explain'])) { @@ -169,7 +169,7 @@ public function __construct(string $databaseName, ?string $collectionName, array } if (isset($options['let']) && ! is_document($options['let'])) { - throw InvalidArgumentException::invalidType('"let" option', $options['let'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"let" option', $options['let']); } if (isset($options['maxAwaitTimeMS']) && ! is_integer($options['maxAwaitTimeMS'])) { diff --git a/src/Operation/BulkWrite.php b/src/Operation/BulkWrite.php index 83217ac6d..0f5153489 100644 --- a/src/Operation/BulkWrite.php +++ b/src/Operation/BulkWrite.php @@ -154,7 +154,7 @@ public function __construct(string $databaseName, string $collectionName, array } if (! is_document($args[0])) { - throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][0]', $i, $type), $args[0], 'document'); + throw InvalidArgumentException::expectedDocumentType(sprintf('$operations[%d]["%s"][0]', $i, $type), $args[0]); } switch ($type) { @@ -174,7 +174,7 @@ public function __construct(string $databaseName, string $collectionName, array $args[1]['limit'] = ($type === self::DELETE_ONE ? 1 : 0); if (isset($args[1]['collation']) && ! is_document($args[1]['collation'])) { - throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][1]["collation"]', $i, $type), $args[1]['collation'], 'document'); + throw InvalidArgumentException::expectedDocumentType(sprintf('$operations[%d]["%s"][1]["collation"]', $i, $type), $args[1]['collation']); } $operations[$i][$type][1] = $args[1]; @@ -187,7 +187,7 @@ public function __construct(string $databaseName, string $collectionName, array } if (! is_document($args[1])) { - throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][1]', $i, $type), $args[1], 'document'); + throw InvalidArgumentException::expectedDocumentType(sprintf('$operations[%d]["%s"][1]', $i, $type), $args[1]); } // Treat empty arrays as replacement documents for BC @@ -215,7 +215,7 @@ public function __construct(string $databaseName, string $collectionName, array $args[2] += ['upsert' => false]; if (isset($args[2]['collation']) && ! is_document($args[2]['collation'])) { - throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][2]["collation"]', $i, $type), $args[2]['collation'], 'document'); + throw InvalidArgumentException::expectedDocumentType(sprintf('$operations[%d]["%s"][2]["collation"]', $i, $type), $args[2]['collation']); } if (! is_bool($args[2]['upsert'])) { @@ -252,7 +252,7 @@ public function __construct(string $databaseName, string $collectionName, array } if (isset($args[2]['collation']) && ! is_document($args[2]['collation'])) { - throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][2]["collation"]', $i, $type), $args[2]['collation'], 'document'); + throw InvalidArgumentException::expectedDocumentType(sprintf('$operations[%d]["%s"][2]["collation"]', $i, $type), $args[2]['collation']); } if (! is_bool($args[2]['upsert'])) { @@ -287,7 +287,7 @@ public function __construct(string $databaseName, string $collectionName, array } if (isset($options['let']) && ! is_document($options['let'])) { - throw InvalidArgumentException::invalidType('"let" option', $options['let'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"let" option', $options['let']); } if (isset($options['bypassDocumentValidation']) && ! $options['bypassDocumentValidation']) { diff --git a/src/Operation/Count.php b/src/Operation/Count.php index 6437fa516..b42050ae3 100644 --- a/src/Operation/Count.php +++ b/src/Operation/Count.php @@ -93,11 +93,11 @@ class Count implements Executable, Explainable public function __construct(string $databaseName, string $collectionName, $filter = [], array $options = []) { if (! is_document($filter)) { - throw InvalidArgumentException::invalidType('$filter', $filter, 'document'); + throw InvalidArgumentException::expectedDocumentType('$filter', $filter); } if (isset($options['collation']) && ! is_document($options['collation'])) { - throw InvalidArgumentException::invalidType('"collation" option', $options['collation'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"collation" option', $options['collation']); } if (isset($options['hint']) && ! is_string($options['hint']) && ! is_array($options['hint']) && ! is_object($options['hint'])) { diff --git a/src/Operation/CountDocuments.php b/src/Operation/CountDocuments.php index bb827ac01..ecb0e135d 100644 --- a/src/Operation/CountDocuments.php +++ b/src/Operation/CountDocuments.php @@ -97,7 +97,7 @@ class CountDocuments implements Executable public function __construct(string $databaseName, string $collectionName, $filter, array $options = []) { if (! is_document($filter)) { - throw InvalidArgumentException::invalidType('$filter', $filter, 'document'); + throw InvalidArgumentException::expectedDocumentType('$filter', $filter); } if (isset($options['limit']) && ! is_integer($options['limit'])) { diff --git a/src/Operation/CreateCollection.php b/src/Operation/CreateCollection.php index 26cc1295d..795542fea 100644 --- a/src/Operation/CreateCollection.php +++ b/src/Operation/CreateCollection.php @@ -153,19 +153,19 @@ public function __construct(string $databaseName, string $collectionName, array } if (isset($options['changeStreamPreAndPostImages']) && ! is_document($options['changeStreamPreAndPostImages'])) { - throw InvalidArgumentException::invalidType('"changeStreamPreAndPostImages" option', $options['changeStreamPreAndPostImages'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"changeStreamPreAndPostImages" option', $options['changeStreamPreAndPostImages']); } if (isset($options['clusteredIndex']) && ! is_document($options['clusteredIndex'])) { - throw InvalidArgumentException::invalidType('"clusteredIndex" option', $options['clusteredIndex'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"clusteredIndex" option', $options['clusteredIndex']); } if (isset($options['collation']) && ! is_document($options['collation'])) { - throw InvalidArgumentException::invalidType('"collation" option', $options['collation'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"collation" option', $options['collation']); } if (isset($options['encryptedFields']) && ! is_document($options['encryptedFields'])) { - throw InvalidArgumentException::invalidType('"encryptedFields" option', $options['encryptedFields'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"encryptedFields" option', $options['encryptedFields']); } if (isset($options['expireAfterSeconds']) && ! is_integer($options['expireAfterSeconds'])) { @@ -177,7 +177,7 @@ public function __construct(string $databaseName, string $collectionName, array } if (isset($options['indexOptionDefaults']) && ! is_document($options['indexOptionDefaults'])) { - throw InvalidArgumentException::invalidType('"indexOptionDefaults" option', $options['indexOptionDefaults'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"indexOptionDefaults" option', $options['indexOptionDefaults']); } if (isset($options['max']) && ! is_integer($options['max'])) { @@ -201,11 +201,11 @@ public function __construct(string $databaseName, string $collectionName, array } if (isset($options['storageEngine']) && ! is_document($options['storageEngine'])) { - throw InvalidArgumentException::invalidType('"storageEngine" option', $options['storageEngine'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"storageEngine" option', $options['storageEngine']); } if (isset($options['timeseries']) && ! is_document($options['timeseries'])) { - throw InvalidArgumentException::invalidType('"timeseries" option', $options['timeseries'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"timeseries" option', $options['timeseries']); } if (isset($options['typeMap']) && ! is_array($options['typeMap'])) { @@ -221,7 +221,7 @@ public function __construct(string $databaseName, string $collectionName, array } if (isset($options['validator']) && ! is_document($options['validator'])) { - throw InvalidArgumentException::invalidType('"validator" option', $options['validator'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"validator" option', $options['validator']); } if (isset($options['viewOn']) && ! is_string($options['viewOn'])) { diff --git a/src/Operation/CreateEncryptedCollection.php b/src/Operation/CreateEncryptedCollection.php index 72b6e1925..aced1321f 100644 --- a/src/Operation/CreateEncryptedCollection.php +++ b/src/Operation/CreateEncryptedCollection.php @@ -84,7 +84,7 @@ public function __construct(string $databaseName, string $collectionName, array } if (! is_document($options['encryptedFields'])) { - throw InvalidArgumentException::invalidType('"encryptedFields" option', $options['encryptedFields'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"encryptedFields" option', $options['encryptedFields']); } $this->createCollection = new CreateCollection($databaseName, $collectionName, $options); diff --git a/src/Operation/DatabaseCommand.php b/src/Operation/DatabaseCommand.php index 5de77489d..39e5a9928 100644 --- a/src/Operation/DatabaseCommand.php +++ b/src/Operation/DatabaseCommand.php @@ -67,7 +67,7 @@ class DatabaseCommand implements Executable public function __construct(string $databaseName, $command, array $options = []) { if (! is_document($command)) { - throw InvalidArgumentException::invalidType('$command', $command, 'document'); + throw InvalidArgumentException::expectedDocumentType('$command', $command); } if (isset($options['readPreference']) && ! $options['readPreference'] instanceof ReadPreference) { diff --git a/src/Operation/Delete.php b/src/Operation/Delete.php index 240ddfd2f..99bad4ebf 100644 --- a/src/Operation/Delete.php +++ b/src/Operation/Delete.php @@ -100,7 +100,7 @@ class Delete implements Executable, Explainable public function __construct(string $databaseName, string $collectionName, $filter, int $limit, array $options = []) { if (! is_document($filter)) { - throw InvalidArgumentException::invalidType('$filter', $filter, 'document'); + throw InvalidArgumentException::expectedDocumentType('$filter', $filter); } if ($limit !== 0 && $limit !== 1) { @@ -108,7 +108,7 @@ public function __construct(string $databaseName, string $collectionName, $filte } if (isset($options['collation']) && ! is_document($options['collation'])) { - throw InvalidArgumentException::invalidType('"collation" option', $options['collation'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"collation" option', $options['collation']); } if (isset($options['hint']) && ! is_string($options['hint']) && ! is_array($options['hint']) && ! is_object($options['hint'])) { @@ -124,7 +124,7 @@ public function __construct(string $databaseName, string $collectionName, $filte } if (isset($options['let']) && ! is_document($options['let'])) { - throw InvalidArgumentException::invalidType('"let" option', $options['let'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"let" option', $options['let']); } if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) { diff --git a/src/Operation/Distinct.php b/src/Operation/Distinct.php index 3a50ab5cf..5c8863fbc 100644 --- a/src/Operation/Distinct.php +++ b/src/Operation/Distinct.php @@ -89,11 +89,11 @@ class Distinct implements Executable, Explainable public function __construct(string $databaseName, string $collectionName, string $fieldName, $filter = [], array $options = []) { if (! is_document($filter)) { - throw InvalidArgumentException::invalidType('$filter', $filter, 'document'); + throw InvalidArgumentException::expectedDocumentType('$filter', $filter); } if (isset($options['collation']) && ! is_document($options['collation'])) { - throw InvalidArgumentException::invalidType('"collation" option', $options['collation'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"collation" option', $options['collation']); } if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) { diff --git a/src/Operation/DropEncryptedCollection.php b/src/Operation/DropEncryptedCollection.php index 75245c066..6bcdf06d5 100644 --- a/src/Operation/DropEncryptedCollection.php +++ b/src/Operation/DropEncryptedCollection.php @@ -68,7 +68,7 @@ public function __construct(string $databaseName, string $collectionName, array } if (! is_document($options['encryptedFields'])) { - throw InvalidArgumentException::invalidType('"encryptedFields" option', $options['encryptedFields'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"encryptedFields" option', $options['encryptedFields']); } /** @psalm-var array{ecocCollection?: ?string, escCollection?: ?string} */ diff --git a/src/Operation/Find.php b/src/Operation/Find.php index ac7064650..d3ec85f1a 100644 --- a/src/Operation/Find.php +++ b/src/Operation/Find.php @@ -164,7 +164,7 @@ class Find implements Executable, Explainable public function __construct(string $databaseName, string $collectionName, $filter, array $options = []) { if (! is_document($filter)) { - throw InvalidArgumentException::invalidType('$filter', $filter, 'document'); + throw InvalidArgumentException::expectedDocumentType('$filter', $filter); } if (isset($options['allowDiskUse']) && ! is_bool($options['allowDiskUse'])) { @@ -180,7 +180,7 @@ public function __construct(string $databaseName, string $collectionName, $filte } if (isset($options['collation']) && ! is_document($options['collation'])) { - throw InvalidArgumentException::invalidType('"collation" option', $options['collation'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"collation" option', $options['collation']); } if (isset($options['cursorType'])) { @@ -206,7 +206,7 @@ public function __construct(string $databaseName, string $collectionName, $filte } if (isset($options['max']) && ! is_document($options['max'])) { - throw InvalidArgumentException::invalidType('"max" option', $options['max'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"max" option', $options['max']); } if (isset($options['maxAwaitTimeMS']) && ! is_integer($options['maxAwaitTimeMS'])) { @@ -222,11 +222,11 @@ public function __construct(string $databaseName, string $collectionName, $filte } if (isset($options['min']) && ! is_document($options['min'])) { - throw InvalidArgumentException::invalidType('"min" option', $options['min'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"min" option', $options['min']); } if (isset($options['modifiers']) && ! is_document($options['modifiers'])) { - throw InvalidArgumentException::invalidType('"modifiers" option', $options['modifiers'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"modifiers" option', $options['modifiers']); } if (isset($options['noCursorTimeout']) && ! is_bool($options['noCursorTimeout'])) { @@ -238,7 +238,7 @@ public function __construct(string $databaseName, string $collectionName, $filte } if (isset($options['projection']) && ! is_document($options['projection'])) { - throw InvalidArgumentException::invalidType('"projection" option', $options['projection'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"projection" option', $options['projection']); } if (isset($options['readConcern']) && ! $options['readConcern'] instanceof ReadConcern) { @@ -270,7 +270,7 @@ public function __construct(string $databaseName, string $collectionName, $filte } if (isset($options['sort']) && ! is_document($options['sort'])) { - throw InvalidArgumentException::invalidType('"sort" option', $options['sort'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"sort" option', $options['sort']); } if (isset($options['typeMap']) && ! is_array($options['typeMap'])) { @@ -278,7 +278,7 @@ public function __construct(string $databaseName, string $collectionName, $filte } if (isset($options['let']) && ! is_document($options['let'])) { - throw InvalidArgumentException::invalidType('"let" option', $options['let'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"let" option', $options['let']); } if (isset($options['readConcern']) && $options['readConcern']->isDefault()) { diff --git a/src/Operation/FindAndModify.php b/src/Operation/FindAndModify.php index 801c685a5..8a08e9b4d 100644 --- a/src/Operation/FindAndModify.php +++ b/src/Operation/FindAndModify.php @@ -141,11 +141,11 @@ public function __construct(string $databaseName, string $collectionName, array } if (isset($options['collation']) && ! is_document($options['collation'])) { - throw InvalidArgumentException::invalidType('"collation" option', $options['collation'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"collation" option', $options['collation']); } if (isset($options['fields']) && ! is_document($options['fields'])) { - throw InvalidArgumentException::invalidType('"fields" option', $options['fields'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"fields" option', $options['fields']); } if (isset($options['hint']) && ! is_string($options['hint']) && ! is_array($options['hint']) && ! is_object($options['hint'])) { @@ -161,7 +161,7 @@ public function __construct(string $databaseName, string $collectionName, array } if (isset($options['query']) && ! is_document($options['query'])) { - throw InvalidArgumentException::invalidType('"query" option', $options['query'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"query" option', $options['query']); } if (! is_bool($options['remove'])) { @@ -173,7 +173,7 @@ public function __construct(string $databaseName, string $collectionName, array } if (isset($options['sort']) && ! is_document($options['sort'])) { - throw InvalidArgumentException::invalidType('"sort" option', $options['sort'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"sort" option', $options['sort']); } if (isset($options['typeMap']) && ! is_array($options['typeMap'])) { @@ -193,7 +193,7 @@ public function __construct(string $databaseName, string $collectionName, array } if (isset($options['let']) && ! is_document($options['let'])) { - throw InvalidArgumentException::invalidType('"let" option', $options['let'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"let" option', $options['let']); } if (isset($options['bypassDocumentValidation']) && ! $options['bypassDocumentValidation']) { diff --git a/src/Operation/FindOneAndDelete.php b/src/Operation/FindOneAndDelete.php index 322aa2ca5..d37523003 100644 --- a/src/Operation/FindOneAndDelete.php +++ b/src/Operation/FindOneAndDelete.php @@ -82,11 +82,11 @@ class FindOneAndDelete implements Executable, Explainable public function __construct(string $databaseName, string $collectionName, $filter, array $options = []) { if (! is_document($filter)) { - throw InvalidArgumentException::invalidType('$filter', $filter, 'document'); + throw InvalidArgumentException::expectedDocumentType('$filter', $filter); } if (isset($options['projection']) && ! is_document($options['projection'])) { - throw InvalidArgumentException::invalidType('"projection" option', $options['projection'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"projection" option', $options['projection']); } if (isset($options['projection'])) { diff --git a/src/Operation/FindOneAndReplace.php b/src/Operation/FindOneAndReplace.php index 257c7ac69..ca1c96c79 100644 --- a/src/Operation/FindOneAndReplace.php +++ b/src/Operation/FindOneAndReplace.php @@ -102,11 +102,11 @@ class FindOneAndReplace implements Executable, Explainable public function __construct(string $databaseName, string $collectionName, $filter, $replacement, array $options = []) { if (! is_document($filter)) { - throw InvalidArgumentException::invalidType('$filter', $filter, 'document'); + throw InvalidArgumentException::expectedDocumentType('$filter', $filter); } if (! is_document($replacement)) { - throw InvalidArgumentException::invalidType('$replacement', $replacement, 'document'); + throw InvalidArgumentException::expectedDocumentType('$replacement', $replacement); } // Treat empty arrays as replacement documents for BC @@ -123,7 +123,7 @@ public function __construct(string $databaseName, string $collectionName, $filte } if (isset($options['projection']) && ! is_document($options['projection'])) { - throw InvalidArgumentException::invalidType('"projection" option', $options['projection'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"projection" option', $options['projection']); } if (array_key_exists('returnDocument', $options) && ! is_integer($options['returnDocument'])) { diff --git a/src/Operation/FindOneAndUpdate.php b/src/Operation/FindOneAndUpdate.php index 1b285e310..711d7389b 100644 --- a/src/Operation/FindOneAndUpdate.php +++ b/src/Operation/FindOneAndUpdate.php @@ -107,7 +107,7 @@ class FindOneAndUpdate implements Executable, Explainable public function __construct(string $databaseName, string $collectionName, $filter, $update, array $options = []) { if (! is_document($filter)) { - throw InvalidArgumentException::invalidType('$filter', $filter, 'document'); + throw InvalidArgumentException::expectedDocumentType('$filter', $filter); } if (! is_array($update) && ! is_object($update)) { @@ -119,7 +119,7 @@ public function __construct(string $databaseName, string $collectionName, $filte } if (isset($options['projection']) && ! is_document($options['projection'])) { - throw InvalidArgumentException::invalidType('"projection" option', $options['projection'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"projection" option', $options['projection']); } if (array_key_exists('returnDocument', $options) && ! is_integer($options['returnDocument'])) { diff --git a/src/Operation/InsertMany.php b/src/Operation/InsertMany.php index 36c4bb1bd..8fe782c29 100644 --- a/src/Operation/InsertMany.php +++ b/src/Operation/InsertMany.php @@ -90,7 +90,7 @@ public function __construct(string $databaseName, string $collectionName, array foreach ($documents as $i => $document) { if (! is_document($document)) { - throw InvalidArgumentException::invalidType(sprintf('$documents[%d]', $i), $document, 'document'); + throw InvalidArgumentException::expectedDocumentType(sprintf('$documents[%d]', $i), $document); } } diff --git a/src/Operation/InsertOne.php b/src/Operation/InsertOne.php index 3fd610cc4..727bb503c 100644 --- a/src/Operation/InsertOne.php +++ b/src/Operation/InsertOne.php @@ -74,7 +74,7 @@ class InsertOne implements Executable public function __construct(string $databaseName, string $collectionName, $document, array $options = []) { if (! is_document($document)) { - throw InvalidArgumentException::invalidType('$document', $document, 'document'); + throw InvalidArgumentException::expectedDocumentType('$document', $document); } if (isset($options['bypassDocumentValidation']) && ! is_bool($options['bypassDocumentValidation'])) { diff --git a/src/Operation/MapReduce.php b/src/Operation/MapReduce.php index 1c7335c70..b08b2ebb9 100644 --- a/src/Operation/MapReduce.php +++ b/src/Operation/MapReduce.php @@ -171,7 +171,7 @@ public function __construct(string $databaseName, string $collectionName, Javasc } if (isset($options['collation']) && ! is_document($options['collation'])) { - throw InvalidArgumentException::invalidType('"collation" option', $options['collation'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"collation" option', $options['collation']); } if (isset($options['finalize']) && ! $options['finalize'] instanceof JavascriptInterface) { @@ -191,7 +191,7 @@ public function __construct(string $databaseName, string $collectionName, Javasc } if (isset($options['query']) && ! is_document($options['query'])) { - throw InvalidArgumentException::invalidType('"query" option', $options['query'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"query" option', $options['query']); } if (isset($options['readConcern']) && ! $options['readConcern'] instanceof ReadConcern) { @@ -203,7 +203,7 @@ public function __construct(string $databaseName, string $collectionName, Javasc } if (isset($options['scope']) && ! is_document($options['scope'])) { - throw InvalidArgumentException::invalidType('"scope" option', $options['scope'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"scope" option', $options['scope']); } if (isset($options['session']) && ! $options['session'] instanceof Session) { @@ -211,7 +211,7 @@ public function __construct(string $databaseName, string $collectionName, Javasc } if (isset($options['sort']) && ! is_document($options['sort'])) { - throw InvalidArgumentException::invalidType('"sort" option', $options['sort'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"sort" option', $options['sort']); } if (isset($options['typeMap']) && ! is_array($options['typeMap'])) { diff --git a/src/Operation/ReplaceOne.php b/src/Operation/ReplaceOne.php index 11867d52f..7b09f7985 100644 --- a/src/Operation/ReplaceOne.php +++ b/src/Operation/ReplaceOne.php @@ -81,7 +81,7 @@ class ReplaceOne implements Executable public function __construct(string $databaseName, string $collectionName, $filter, $replacement, array $options = []) { if (! is_document($replacement)) { - throw InvalidArgumentException::invalidType('$replacement', $replacement, 'document'); + throw InvalidArgumentException::expectedDocumentType('$replacement', $replacement); } // Treat empty arrays as replacement documents for BC diff --git a/src/Operation/Update.php b/src/Operation/Update.php index 6ee200514..84e8a45c9 100644 --- a/src/Operation/Update.php +++ b/src/Operation/Update.php @@ -115,7 +115,7 @@ class Update implements Executable, Explainable public function __construct(string $databaseName, string $collectionName, $filter, $update, array $options = []) { if (! is_document($filter)) { - throw InvalidArgumentException::invalidType('$filter', $filter, 'document'); + throw InvalidArgumentException::expectedDocumentType('$filter', $filter); } if (! is_array($update) && ! is_object($update)) { @@ -136,7 +136,7 @@ public function __construct(string $databaseName, string $collectionName, $filte } if (isset($options['collation']) && ! is_document($options['collation'])) { - throw InvalidArgumentException::invalidType('"collation" option', $options['collation'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"collation" option', $options['collation']); } if (isset($options['hint']) && ! is_string($options['hint']) && ! is_array($options['hint']) && ! is_object($options['hint'])) { @@ -164,7 +164,7 @@ public function __construct(string $databaseName, string $collectionName, $filte } if (isset($options['let']) && ! is_document($options['let'])) { - throw InvalidArgumentException::invalidType('"let" option', $options['let'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"let" option', $options['let']); } if (isset($options['bypassDocumentValidation']) && ! $options['bypassDocumentValidation']) { diff --git a/src/Operation/Watch.php b/src/Operation/Watch.php index 44af0eaa7..9eaa7f246 100644 --- a/src/Operation/Watch.php +++ b/src/Operation/Watch.php @@ -224,11 +224,11 @@ public function __construct(Manager $manager, ?string $databaseName, ?string $co } if (isset($options['resumeAfter']) && ! is_document($options['resumeAfter'])) { - throw InvalidArgumentException::invalidType('"resumeAfter" option', $options['resumeAfter'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"resumeAfter" option', $options['resumeAfter']); } if (isset($options['startAfter']) && ! is_document($options['startAfter'])) { - throw InvalidArgumentException::invalidType('"startAfter" option', $options['startAfter'], 'document'); + throw InvalidArgumentException::expectedDocumentType('"startAfter" option', $options['startAfter']); } if (isset($options['startAtOperationTime']) && ! $options['startAtOperationTime'] instanceof TimestampInterface) { diff --git a/src/functions.php b/src/functions.php index bc845db31..4acec021c 100644 --- a/src/functions.php +++ b/src/functions.php @@ -88,7 +88,7 @@ function all_servers_support_write_stage_on_secondary(array $servers): bool function apply_type_map_to_document($document, array $typeMap) { if (! is_document($document)) { - throw InvalidArgumentException::invalidType('$document', $document, 'document'); + throw InvalidArgumentException::expectedDocumentType('$document', $document); } return toPHP(fromPHP($document), $typeMap); @@ -133,7 +133,7 @@ function document_to_array($document): array } if (! is_array($document)) { - throw InvalidArgumentException::invalidType('$document', $document, 'document'); + throw InvalidArgumentException::expectedDocumentType('$document', $document); } return $document; diff --git a/tests/FunctionsTest.php b/tests/FunctionsTest.php index 96e0c417d..c07f81535 100644 --- a/tests/FunctionsTest.php +++ b/tests/FunctionsTest.php @@ -116,7 +116,7 @@ public function provideDocumentsAndExpectedArrays(): array public function testDocumentToArrayArgumentTypeCheck($document): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Expected $document to have type "document"'); + $this->expectExceptionMessage('Expected $document to have type "document" (array or object)'); document_to_array($document); } diff --git a/tests/Operation/BulkWriteTest.php b/tests/Operation/BulkWriteTest.php index e1a38828c..18941b455 100644 --- a/tests/Operation/BulkWriteTest.php +++ b/tests/Operation/BulkWriteTest.php @@ -57,7 +57,7 @@ public function testInsertOneDocumentArgumentMissing(): void public function testInsertOneDocumentArgumentTypeCheck($document): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageMatches('/Expected \$operations\[0\]\["insertOne"\]\[0\] to have type "document" but found ".+"/'); + $this->expectExceptionMessageMatches('/Expected \$operations\[0\]\["insertOne"\]\[0\] to have type "document" \(array or object\) but found ".+"/'); new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [ [BulkWrite::INSERT_ONE => [$document]], ]); @@ -76,7 +76,7 @@ public function testDeleteManyFilterArgumentMissing(): void public function testDeleteManyFilterArgumentTypeCheck($document): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageMatches('/Expected \$operations\[0\]\["deleteMany"\]\[0\] to have type "document" but found ".+"/'); + $this->expectExceptionMessageMatches('/Expected \$operations\[0\]\["deleteMany"\]\[0\] to have type "document" \(array or object\) but found ".+"/'); new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [ [BulkWrite::DELETE_MANY => [$document]], ]); @@ -86,7 +86,7 @@ public function testDeleteManyFilterArgumentTypeCheck($document): void public function testDeleteManyCollationOptionTypeCheck($collation): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageMatches('/Expected \$operations\[0\]\["deleteMany"\]\[1\]\["collation"\] to have type "document" but found ".+"/'); + $this->expectExceptionMessageMatches('/Expected \$operations\[0\]\["deleteMany"\]\[1\]\["collation"\] to have type "document" \(array or object\) but found ".+"/'); new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [ [BulkWrite::DELETE_MANY => [['x' => 1], ['collation' => $collation]]], ]); @@ -110,7 +110,7 @@ public function testDeleteOneFilterArgumentMissing(): void public function testDeleteOneFilterArgumentTypeCheck($document): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageMatches('/Expected \$operations\[0\]\["deleteOne"\]\[0\] to have type "document" but found ".+"/'); + $this->expectExceptionMessageMatches('/Expected \$operations\[0\]\["deleteOne"\]\[0\] to have type "document" \(array or object\) but found ".+"/'); new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [ [BulkWrite::DELETE_ONE => [$document]], ]); @@ -120,7 +120,7 @@ public function testDeleteOneFilterArgumentTypeCheck($document): void public function testDeleteOneCollationOptionTypeCheck($collation): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageMatches('/Expected \$operations\[0\]\["deleteOne"\]\[1\]\["collation"\] to have type "document" but found ".+"/'); + $this->expectExceptionMessageMatches('/Expected \$operations\[0\]\["deleteOne"\]\[1\]\["collation"\] to have type "document" \(array or object\) but found ".+"/'); new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [ [BulkWrite::DELETE_ONE => [['x' => 1], ['collation' => $collation]]], ]); @@ -139,7 +139,7 @@ public function testReplaceOneFilterArgumentMissing(): void public function testReplaceOneFilterArgumentTypeCheck($filter): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageMatches('/Expected \$operations\[0\]\["replaceOne"\]\[0\] to have type "document" but found ".+"/'); + $this->expectExceptionMessageMatches('/Expected \$operations\[0\]\["replaceOne"\]\[0\] to have type "document" \(array or object\) but found ".+"/'); new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [ [BulkWrite::REPLACE_ONE => [$filter, ['y' => 1]]], ]); @@ -169,7 +169,7 @@ public function testReplaceOneReplacementArgumentMissing(): void public function testReplaceOneReplacementArgumentTypeCheck($replacement): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageMatches('/Expected \$operations\[0\]\["replaceOne"\]\[1\] to have type "document" but found ".+"/'); + $this->expectExceptionMessageMatches('/Expected \$operations\[0\]\["replaceOne"\]\[1\] to have type "document" \(array or object\) but found ".+"/'); new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [ [BulkWrite::REPLACE_ONE => [['x' => 1], $replacement]], ]); @@ -192,7 +192,7 @@ public function testReplaceOneReplacementArgumentProhibitsUpdateDocument($replac public function testReplaceOneReplacementArgumentProhibitsUpdatePipeline($replacement): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageMatches('#(\$operations\[0\]\["replaceOne"\]\[1\] is an update pipeline)|(\$operations\[0\]\["replaceOne"\]\[1\] to have type "document")#'); + $this->expectExceptionMessageMatches('#(\$operations\[0\]\["replaceOne"\]\[1\] is an update pipeline)|(\$operations\[0\]\["replaceOne"\]\[1\] to have type "document" \(array or object\))#'); new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [ [BulkWrite::REPLACE_ONE => [['x' => 1], $replacement]], ]); @@ -202,7 +202,7 @@ public function testReplaceOneReplacementArgumentProhibitsUpdatePipeline($replac public function testReplaceOneCollationOptionTypeCheck($collation): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageMatches('/Expected \$operations\[0\]\["replaceOne"\]\[2\]\["collation"\] to have type "document" but found ".+"/'); + $this->expectExceptionMessageMatches('/Expected \$operations\[0\]\["replaceOne"\]\[2\]\["collation"\] to have type "document" \(array or object\) but found ".+"/'); new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [ [BulkWrite::REPLACE_ONE => [['x' => 1], ['y' => 1], ['collation' => $collation]]], ]); @@ -236,7 +236,7 @@ public function testUpdateManyFilterArgumentMissing(): void public function testUpdateManyFilterArgumentTypeCheck($filter): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageMatches('/Expected \$operations\[0\]\["updateMany"\]\[0\] to have type "document" but found ".+"/'); + $this->expectExceptionMessageMatches('/Expected \$operations\[0\]\["updateMany"\]\[0\] to have type "document" \(array or object\) but found ".+"/'); new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [ [BulkWrite::UPDATE_MANY => [$filter, ['$set' => ['x' => 1]]]], ]); @@ -300,7 +300,7 @@ public function testUpdateManyArrayFiltersOptionTypeCheck($arrayFilters): void public function testUpdateManyCollationOptionTypeCheck($collation): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageMatches('/Expected \$operations\[0\]\["updateMany"\]\[2\]\["collation"\] to have type "document" but found ".+"/'); + $this->expectExceptionMessageMatches('/Expected \$operations\[0\]\["updateMany"\]\[2\]\["collation"\] to have type "document" \(array or object\) but found ".+"/'); new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [ [BulkWrite::UPDATE_MANY => [['x' => 1], ['$set' => ['x' => 1]], ['collation' => $collation]]], ]); @@ -341,7 +341,7 @@ public function testUpdateOneFilterArgumentMissing(): void public function testUpdateOneFilterArgumentTypeCheck($filter): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageMatches('/Expected \$operations\[0\]\["updateOne"\]\[0\] to have type "document" but found ".+"/'); + $this->expectExceptionMessageMatches('/Expected \$operations\[0\]\["updateOne"\]\[0\] to have type "document" \(array or object\) but found ".+"/'); new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [ [BulkWrite::UPDATE_ONE => [$filter, ['$set' => ['x' => 1]]]], ]); @@ -393,7 +393,7 @@ public function testUpdateOneArrayFiltersOptionTypeCheck($arrayFilters): void public function testUpdateOneCollationOptionTypeCheck($collation): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageMatches('/Expected \$operations\[0\]\["updateOne"\]\[2\]\["collation"\] to have type "document" but found ".+"/'); + $this->expectExceptionMessageMatches('/Expected \$operations\[0\]\["updateOne"\]\[2\]\["collation"\] to have type "document" \(array or object\) but found ".+"/'); new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [ [BulkWrite::UPDATE_ONE => [['x' => 1], ['$set' => ['x' => 1]], ['collation' => $collation]]], ]); diff --git a/tests/Operation/CreateIndexesTest.php b/tests/Operation/CreateIndexesTest.php index 5ecef9b24..0ece84f43 100644 --- a/tests/Operation/CreateIndexesTest.php +++ b/tests/Operation/CreateIndexesTest.php @@ -67,7 +67,7 @@ public function testConstructorRequiresIndexSpecificationKey(): void public function testConstructorRequiresIndexSpecificationKeyToBeADocument($key): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Expected "key" option to have type "document"'); + $this->expectExceptionMessage('Expected "key" option to have type "document" (array or object)'); new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), [['key' => $key]]); } diff --git a/tests/Operation/FindOneAndReplaceTest.php b/tests/Operation/FindOneAndReplaceTest.php index eacd6b123..73951596d 100644 --- a/tests/Operation/FindOneAndReplaceTest.php +++ b/tests/Operation/FindOneAndReplaceTest.php @@ -46,7 +46,7 @@ public function testConstructorReplacementArgumentProhibitsUpdateDocument($repla public function testConstructorReplacementArgumentProhibitsUpdatePipeline($replacement): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageMatches('#(\$replacement is an update pipeline)|(Expected \$replacement to have type "document")#'); + $this->expectExceptionMessageMatches('#(\$replacement is an update pipeline)|(Expected \$replacement to have type "document" \(array or object\))#'); new FindOneAndReplace($this->getDatabaseName(), $this->getCollectionName(), [], $replacement); } diff --git a/tests/Operation/ReplaceOneTest.php b/tests/Operation/ReplaceOneTest.php index 10f840137..1290f90df 100644 --- a/tests/Operation/ReplaceOneTest.php +++ b/tests/Operation/ReplaceOneTest.php @@ -45,7 +45,7 @@ public function testConstructorReplacementArgumentProhibitsUpdateDocument($repla public function testConstructorReplacementArgumentProhibitsUpdatePipeline($replacement): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageMatches('#(\$replacement is an update pipeline)|(Expected \$replacement to have type "document")#'); + $this->expectExceptionMessageMatches('#(\$replacement is an update pipeline)|(Expected \$replacement to have type "document" \(array or object\))#'); new ReplaceOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], $replacement); } } diff --git a/tests/Operation/UpdateTest.php b/tests/Operation/UpdateTest.php index b033c0768..85d00ef88 100644 --- a/tests/Operation/UpdateTest.php +++ b/tests/Operation/UpdateTest.php @@ -12,7 +12,7 @@ class UpdateTest extends TestCase public function testConstructorFilterArgumentTypeCheck($filter): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageMatches('/Expected \$filter to have type "document" but found ".+"/'); + $this->expectExceptionMessageMatches('/Expected \$filter to have type "document" \(array or object\) but found ".+"/'); new Update($this->getDatabaseName(), $this->getCollectionName(), $filter, ['$set' => ['x' => 1]]); }