Skip to content

Commit 0ce96ed

Browse files
kevinkl3kevinl4amartin-helmich
authored
Fix: Array functions fail against BSONArray (#48)
* cast BSONArray to array to prevent is_array from failing * update composer package name * cast val and operand if they are instances of BSONArray * add missing import * Formatting Co-authored-by: kevinkl3 <kevin@loyica.com> Co-authored-by: Martin Helmich <kontakt@martin-helmich.de>
1 parent 1c6c5b5 commit 0ce96ed

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/MockCollection.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use MongoDB\Collection;
1313
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
1414
use MongoDB\Model\BSONDocument;
15+
use MongoDB\Model\BSONArray;
1516
use MongoDB\Model\IndexInfoIteratorIterator;
1617
use MongoDB\Operation\FindOneAndUpdate;
1718
use PHPUnit\Framework\Constraint\Constraint;
@@ -234,7 +235,6 @@ private function updateCore(&$doc, $update)
234235
$doc[$k][] = $v;
235236
}
236237
}
237-
238238
}
239239

240240
public function find($filter = [], array $options = []): MockCursor
@@ -377,7 +377,7 @@ public function deleteOne($filter, array $options = [])
377377
$deletedIds = [];
378378
foreach ($this->documents as $i => $doc) {
379379
if ($matcher($doc)) {
380-
$deletedIds [] = $doc['_id'];
380+
$deletedIds[] = $doc['_id'];
381381
unset($this->documents[$i]);
382382
$this->documents = array_values($this->documents);
383383
$count++;
@@ -613,8 +613,18 @@ private function matcherFromConstraint($constraint): callable
613613

614614
if (is_array($constraint)) {
615615
return $match = function ($val) use (&$constraint, &$match): bool {
616+
//cast $val to array if it is an instance of BSONArray
617+
//this will prevent is_array,array_reduce... from failing
618+
if ($val instanceof BSONArray) {
619+
$val = (array)$val;
620+
}
616621
$result = true;
617622
foreach ($constraint as $type => $operand) {
623+
//cast $operand to array if it is an instance of BSONArray
624+
//this will prevent is_array,array_reduce... from failing
625+
if ($operand instanceof BSONArray) {
626+
$operand = (array)$operand;
627+
}
618628
switch ($type) {
619629
// Mongo operators (subset)
620630
case '$gt':

0 commit comments

Comments
 (0)