Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-helmich committed Mar 18, 2022
1 parent 5523028 commit 1c6c5b5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
24 changes: 11 additions & 13 deletions src/MockCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
use MongoDB\BSON\ObjectID;
use MongoDB\BSON\Regex;
use MongoDB\Collection;
use MongoDB\Model\BSONArray;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Model\BSONDocument;
use MongoDB\Model\IndexInfoIteratorIterator;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Operation\FindOneAndUpdate;
use PHPUnit\Framework\Constraint\Constraint;

Expand Down Expand Up @@ -378,7 +377,7 @@ public function deleteOne($filter, array $options = [])
$deletedIds = [];
foreach ($this->documents as $i => $doc) {
if ($matcher($doc)) {
$deletedIds []= $doc['_id'];
$deletedIds [] = $doc['_id'];
unset($this->documents[$i]);
$this->documents = array_values($this->documents);
$count++;
Expand All @@ -393,7 +392,7 @@ public function distinct($fieldName, $filter = [], array $options = [])
$values = [];

$matcher = $this->matcherFromQuery($filter);
foreach ($this->documents as $document){
foreach ($this->documents as $document) {
if ($matcher($document) && isset($document[$fieldName])) {
$values[] = $document[$fieldName];
}
Expand Down Expand Up @@ -678,20 +677,19 @@ function ($acc, $op) use ($val) {
$result = !$operand;
}
break;
case '$regex':{
if($operand instanceof \MongoDB\BSON\Regex){
$regex = "/". $operand->getPattern() . "/". $operand->getFlags();
$result = preg_match($regex,$val) === 1;
}else if(is_string($operand)){
if(@preg_match($operand, '') === false){
case '$regex':
if ($operand instanceof Regex) {
$regex = "/" . $operand->getPattern() . "/" . $operand->getFlags();
$result = preg_match($regex, $val) === 1;
} else if (is_string($operand)) {
if (@preg_match($operand, '') === false) {
throw new Exception("Invalid constraint for operator '" . $type . "'");
}
$result = preg_match($operand,$val) === 1;
}else{
$result = preg_match($operand, $val) === 1;
} else {
throw new Exception("Invalid constraint for operator '" . $type . "'");
}
break;
}
// Custom operators
case '$instanceOf':
$result = is_a($val, $operand);
Expand Down
8 changes: 4 additions & 4 deletions tests/MockCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,9 @@ public function testFindWithRegexFilter()
$result = $this->col->count(['foo' => ['$regex' => "/(bar|BAZ)/i"]]);
self::assertThat($result, self::equalTo(3));

$result = $this->col->count(['foo' => ['$regex' => new \MongoDB\BSON\Regex("FOOBAR","i")]]);
$result = $this->col->count(['foo' => ['$regex' => new \MongoDB\BSON\Regex("FOOBAR", "i")]]);
self::assertThat($result, self::equalTo(1));

$this->expectException(Exception::class);

$result = $this->col->count(['foo' => ['$regex' => "[[[[foobar{"]]);
Expand Down Expand Up @@ -311,15 +311,15 @@ public function testDeleteOneDeletesJustOneObject()
['foo' => 'bar', 'bar' => 1],
['foo' => 'baz', 'bar' => 2],
]);

$deleteResult = $this->col->deleteOne(['bar' => 1]);

self::assertThat($this->col->count(['bar' => 1]), self::equalTo(1));
self::assertThat($this->col->count(['bar' => 2]), self::equalTo(1));

self::assertInstanceOf(\Helmich\MongoMock\MockDeleteResult::class, $deleteResult);

self::assertEquals(1,$deleteResult->getDeletedCount());
self::assertEquals(1, $deleteResult->getDeletedCount());
}

/**
Expand Down

0 comments on commit 1c6c5b5

Please sign in to comment.