Skip to content

Commit

Permalink
Allow mixed value in $not operator (#2307)
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu authored May 20, 2021
1 parent 5ca00af commit f225a0c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
6 changes: 3 additions & 3 deletions lib/Doctrine/ODM/MongoDB/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1028,11 +1028,11 @@ public function nearSphere($x, $y = null): self
* @see Expr::not()
* @see https://docs.mongodb.com/manual/reference/operator/not/
*
* @param array|Expr $expression
* @param array|Expr|mixed $valueOrExpression
*/
public function not($expression): self
public function not($valueOrExpression): self
{
$this->expr->not($expression);
$this->expr->not($valueOrExpression);

return $this;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Doctrine/ODM/MongoDB/Query/Expr.php
Original file line number Diff line number Diff line change
Expand Up @@ -861,11 +861,11 @@ public function nearSphere($x, $y = null): self
* @see Builder::not()
* @see https://docs.mongodb.com/manual/reference/operator/not/
*
* @param array|Expr $expression
* @param array|Expr|mixed $valueOrExpression
*/
public function not($expression): self
public function not($valueOrExpression): self
{
return $this->operator('$not', $expression);
return $this->operator('$not', $valueOrExpression);
}

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use InvalidArgumentException;
use IteratorAggregate;
use MongoDB\BSON\ObjectId;
use MongoDB\BSON\Regex;
use MongoDB\BSON\UTCDateTime;

use function array_values;
Expand Down Expand Up @@ -97,6 +98,27 @@ public function testAddNot()
$this->assertNotNull($user);
}

public function testNotAllowsRegex()
{
$user = new User();
$user->setUsername('boo');

$this->dm->persist($user);
$this->dm->flush();

$qb = $this->dm->createQueryBuilder(User::class);
$qb->field('username')->not(new Regex('Boo', 'i'));
$query = $qb->getQuery();
$user = $query->getSingleResult();
$this->assertNull($user);

$qb = $this->dm->createQueryBuilder(User::class);
$qb->field('username')->not(new Regex('Boo'));
$query = $qb->getQuery();
$user = $query->getSingleResult();
$this->assertNotNull($user);
}

public function testDistinct()
{
$user = new User();
Expand Down
14 changes: 14 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use InvalidArgumentException;
use IteratorAggregate;
use MongoDB\BSON\ObjectId;
use MongoDB\BSON\Regex;
use MongoDB\Driver\ReadPreference;
use ReflectionProperty;

Expand Down Expand Up @@ -299,6 +300,19 @@ public function testAddNot()
$this->assertEquals($expected, $qb->getQueryArray());
}

public function testNotAllowsRegex()
{
$qb = $this->getTestQueryBuilder();
$qb->field('username')->not(new Regex('Boo', 'i'));

$expected = [
'username' => [
'$not' => new Regex('Boo', 'i'),
],
];
$this->assertEquals($expected, $qb->getQueryArray());
}

public function testFindQuery()
{
$qb = $this->getTestQueryBuilder()
Expand Down

0 comments on commit f225a0c

Please sign in to comment.