From 9748929888646d682514c6e9df0056c07d57ad71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Tue, 11 Jul 2023 16:32:06 +0200 Subject: [PATCH] PHPORM-44: Throw an exception when Laravel Builder::push() is used incorrectly --- src/Query/Builder.php | 5 ++++- tests/QueryBuilderTest.php | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Query/Builder.php b/src/Query/Builder.php index 06641273..1f707e9b 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -786,9 +786,12 @@ public function push($column, $value = null, $unique = false) $operator = $unique ? '$addToSet' : '$push'; // Check if we are pushing multiple values. - $batch = (is_array($value) && array_keys($value) === range(0, count($value) - 1)); + $batch = is_array($value) && array_is_list($value); if (is_array($column)) { + if ($value !== null) { + throw new \InvalidArgumentException(sprintf('2nd argument of %s() must be "null" when 1st argument is an array. Got "%s" instead.', __METHOD__, get_debug_type($value))); + } $query = [$operator => $column]; } elseif ($batch) { $query = [$operator => [$column => ['$each' => $value]]]; diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 9cb3af40..92c1bbe7 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -345,6 +345,14 @@ public function testPush() $this->assertCount(3, $user['messages']); } + public function testPushRefuses2ndArgumentWhen1stIsAnArray() + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('2nd argument of Jenssegers\Mongodb\Query\Builder::push() must be "null" when 1st argument is an array. Got "string" instead.'); + + DB::collection('users')->push(['tags' => 'tag1'], 'tag2'); + } + public function testPull() { $message1 = ['from' => 'Jane', 'body' => 'Hi John'];