From 96c6df0bfe912fd601f996fe8a08c1be0c99fe52 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Fri, 20 Jul 2018 07:07:44 +0200 Subject: [PATCH] Fix wrong element deletion in popFirst and popLast --- lib/Doctrine/ODM/MongoDB/Query/Expr.php | 4 +- .../MongoDB/Tests/Functional/QueryTest.php | 52 +++++++++++++++++++ .../ODM/MongoDB/Tests/Query/ExprTest.php | 2 +- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/ODM/MongoDB/Query/Expr.php b/lib/Doctrine/ODM/MongoDB/Query/Expr.php index 51af6ca312..c0425691be 100644 --- a/lib/Doctrine/ODM/MongoDB/Query/Expr.php +++ b/lib/Doctrine/ODM/MongoDB/Query/Expr.php @@ -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; } @@ -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; } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/QueryTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/QueryTest.php index eec4d0b85e..b5d152b90d 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/QueryTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/QueryTest.php @@ -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()); + } } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Query/ExprTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Query/ExprTest.php index a07ab5f3ea..1dcb3ed931 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Query/ExprTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Query/ExprTest.php @@ -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()