Skip to content

Commit

Permalink
Merge pull request #1831 from alcaeus/fix-expr-pop
Browse files Browse the repository at this point in the history
[2.0] Fix wrong element deletion in popFirst and popLast
  • Loading branch information
malarzm authored Jul 20, 2018
2 parents 3c06525 + 96c6df0 commit 1b40f78
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/Doctrine/ODM/MongoDB/Query/Expr.php
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ public function operator($operator, $value)
public function popFirst()
{
$this->requiresCurrentField();
$this->newObj['$pop'][$this->currentField] = 1;
$this->newObj['$pop'][$this->currentField] = -1;
return $this;
}

Expand All @@ -981,7 +981,7 @@ public function popFirst()
public function popLast()
{
$this->requiresCurrentField();
$this->newObj['$pop'][$this->currentField] = -1;
$this->newObj['$pop'][$this->currentField] = 1;
return $this;
}

Expand Down
52 changes: 52 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,4 +441,56 @@ public function testQueryWhereAllValuesOfCollection()
$this->assertSame($expected, $qb->getQueryArray());
$this->assertSame($expected, $qb->getQuery()->debug('query'));
}

public function testPopFirst(): void
{
$article = new Article();
$article->setTitle('test');
$article->setBody('test');
$article->setCreatedAt('1985-09-01 00:00:00');
$article->addTag(1);
$article->addTag(2);
$article->addTag(3);

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

$this->dm->createQueryBuilder(Article::class)
->updateOne()
->field('id')
->equals($article->getId())
->field('tags')
->popFirst()
->getQuery()
->execute();

$this->dm->refresh($article);
$this->assertSame([2, 3], $article->getTags());
}

public function testPopLast(): void
{
$article = new Article();
$article->setTitle('test');
$article->setBody('test');
$article->setCreatedAt('1985-09-01 00:00:00');
$article->addTag(1);
$article->addTag(2);
$article->addTag(3);

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

$this->dm->createQueryBuilder(Article::class)
->updateOne()
->field('id')
->equals($article->getId())
->field('tags')
->popLast()
->getQuery()
->execute();

$this->dm->refresh($article);
$this->assertSame([1, 2], $article->getTags());
}
}
2 changes: 1 addition & 1 deletion tests/Doctrine/ODM/MongoDB/Tests/Query/ExprTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public function testNewObjectIsPrepared()
->field('address.subAddress.subAddress.subAddress.test')->popFirst();
$query = $qb->getQuery();
$query = $query->getQuery();
$this->assertEquals(['$pop' => ['address.subAddress.subAddress.subAddress.testFieldName' => 1]], $query['newObj']);
$this->assertEquals(['$pop' => ['address.subAddress.subAddress.subAddress.testFieldName' => -1]], $query['newObj']);
}

public function testReferencesUsesMinimalKeys()
Expand Down

0 comments on commit 1b40f78

Please sign in to comment.