Skip to content
This repository has been archived by the owner on Nov 11, 2020. It is now read-only.

Commit

Permalink
Merge pull request #326 from alcaeus/fix-expr-pop
Browse files Browse the repository at this point in the history
Fix wrong element deletion in popFirst and popLast
  • Loading branch information
alcaeus authored Jul 20, 2018
2 parents 517560a + efeb3b3 commit 859dad9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG-1.6.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ CHANGELOG for 1.6.x
This changelog references the relevant changes (bug and security fixes) done
in 1.6.x patch versions.

1.6.3 (2018-07-20)
------------------

* [#326](https://github.com/doctrine/mongodb/pull/326): Fix wrong element deletion in popFirst and popLast

1.6.2 (2018-04-30)
------------------

Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/MongoDB/Query/Expr.php
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,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 @@ -1013,7 +1013,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
50 changes: 49 additions & 1 deletion tests/Doctrine/MongoDB/Tests/Query/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,57 @@ public function testUpsertQuery()
$this->assertArrayHasKeyValue(['insertValue' => 1], $document);
}

public function testPopFirst()
{
$this->getTestQueryBuilder()
->insert()
->field('list')->set([1, 2, 3])
->getQuery()
->execute();

$this->getTestQueryBuilder()
->updateOne()
->field('list')->popFirst()
->getQuery()
->execute();

$count = $this->getTestQueryBuilder()
->count()
->field('list')
->equals(1)
->getQuery()
->execute();

$this->assertSame(0, $count);
}

public function testPopLast()
{
$this->getTestQueryBuilder()
->insert()
->field('list')->set([1, 2, 3])
->getQuery()
->execute();

$this->getTestQueryBuilder()
->updateOne()
->field('list')->popLast()
->getQuery()
->execute();

$count = $this->getTestQueryBuilder()
->count()
->field('list')
->equals(3)
->getQuery()
->execute();

$this->assertSame(0, $count);
}

private function getTestQueryBuilder()
{
return $this->conn->selectCollection('db', 'users')->createQueryBuilder();
return $this->conn->selectCollection(self::$dbName, 'users')->createQueryBuilder();
}

private function assertArrayHasKeyValue($expected, $array, $message = '')
Expand Down

0 comments on commit 859dad9

Please sign in to comment.