Skip to content

Commit bc364b5

Browse files
authored
PHPORM-44: Throw an exception when Query\Builder::push() is used incorrectly (mongodb#5)
1 parent b6e8a44 commit bc364b5

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/Query/Builder.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -786,9 +786,12 @@ public function push($column, $value = null, $unique = false)
786786
$operator = $unique ? '$addToSet' : '$push';
787787

788788
// Check if we are pushing multiple values.
789-
$batch = (is_array($value) && array_keys($value) === range(0, count($value) - 1));
789+
$batch = is_array($value) && array_is_list($value);
790790

791791
if (is_array($column)) {
792+
if ($value !== null) {
793+
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)));
794+
}
792795
$query = [$operator => $column];
793796
} elseif ($batch) {
794797
$query = [$operator => [$column => ['$each' => $value]]];

tests/QueryBuilderTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,14 @@ public function testPush()
345345
$this->assertCount(3, $user['messages']);
346346
}
347347

348+
public function testPushRefuses2ndArgumentWhen1stIsAnArray()
349+
{
350+
$this->expectException(\InvalidArgumentException::class);
351+
$this->expectExceptionMessage('2nd argument of Jenssegers\Mongodb\Query\Builder::push() must be "null" when 1st argument is an array. Got "string" instead.');
352+
353+
DB::collection('users')->push(['tags' => 'tag1'], 'tag2');
354+
}
355+
348356
public function testPull()
349357
{
350358
$message1 = ['from' => 'Jane', 'body' => 'Hi John'];

0 commit comments

Comments
 (0)