Skip to content

Commit 40b2dfb

Browse files
committed
Refactor validity check for bulk update argument
1 parent 841e7bc commit 40b2dfb

File tree

2 files changed

+3
-8
lines changed

2 files changed

+3
-8
lines changed

src/Operation/BulkWrite.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
use function current;
3333
use function is_array;
3434
use function is_bool;
35-
use function is_object;
3635
use function key;
3736
use function MongoDB\is_document;
3837
use function MongoDB\is_first_key_operator;
@@ -233,11 +232,7 @@ public function __construct(string $databaseName, string $collectionName, array
233232
throw new InvalidArgumentException(sprintf('Missing second argument for $operations[%d]["%s"]', $i, $type));
234233
}
235234

236-
if (! is_array($args[1]) && ! is_object($args[1])) {
237-
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][1]', $i, $type), $args[1], 'array or object');
238-
}
239-
240-
if (! is_first_key_operator($args[1]) && ! is_pipeline($args[1])) {
235+
if ((! is_document($args[1]) || ! is_first_key_operator($args[1])) && ! is_pipeline($args[1])) {
241236
throw new InvalidArgumentException(sprintf('Expected update operator(s) or non-empty pipeline for $operations[%d]["%s"][1]', $i, $type));
242237
}
243238

tests/Operation/BulkWriteTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public function testUpdateManyUpdateArgumentMissing(): void
267267
public function testUpdateManyUpdateArgumentTypeCheck($update): void
268268
{
269269
$this->expectException(InvalidArgumentException::class);
270-
$this->expectExceptionMessageMatches('/(non-empty pipeline for \$operations\[0\]\["updateMany"\]\[1\])|(\$operations\[0\]\["updateMany"\]\[1\] to have type "array or object" but found ".+")/');
270+
$this->expectExceptionMessage('Expected update operator(s) or non-empty pipeline for $operations[0]["updateMany"][1]');
271271
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
272272
[BulkWrite::UPDATE_MANY => [['x' => 1], $update]],
273273
]);
@@ -360,7 +360,7 @@ public function testUpdateOneUpdateArgumentMissing(): void
360360
public function testUpdateOneUpdateArgumentTypeCheck($update): void
361361
{
362362
$this->expectException(InvalidArgumentException::class);
363-
$this->expectExceptionMessageMatches('/(non-empty pipeline for \$operations\[0\]\["updateOne"\]\[1\])|(\$operations\[0\]\["updateOne"\]\[1\] to have type "array or object" but found ".+")/');
363+
$this->expectExceptionMessage('Expected update operator(s) or non-empty pipeline for $operations[0]["updateOne"][1]');
364364
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
365365
[BulkWrite::UPDATE_ONE => [['x' => 1], $update]],
366366
]);

0 commit comments

Comments
 (0)