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 #333 from alcaeus/fix-replaceroot-stage
Browse files Browse the repository at this point in the history
Fix wrong syntax for replaceRoot stage
  • Loading branch information
alcaeus authored Jul 11, 2019
2 parents 859dad9 + 6b081b6 commit 46b6316
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 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.4 (2019-07-10)
------------------

* [#333](https://github.com/doctrine/mongodb/pull/333): Fix wrong syntax for replaceRoot stage

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

Expand Down
7 changes: 6 additions & 1 deletion lib/Doctrine/MongoDB/Aggregation/Stage/ReplaceRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\MongoDB\Aggregation\Builder;
use Doctrine\MongoDB\Aggregation\Expr;
use function is_array;

/**
* Fluent interface for adding a $replaceRoot stage to an aggregation pipeline.
Expand Down Expand Up @@ -35,8 +36,12 @@ public function __construct(Builder $builder, $expression = null)
*/
public function getExpression()
{
$expression = $this->expression !== null ? $this->convertExpression($this->expression) : $this->expr->getExpression();

return [
'$replaceRoot' => $this->expression !== null ? $this->convertExpression($this->expression) : $this->expr->getExpression()
'$replaceRoot' => [
'newRoot' => is_array($expression) ? (object) $expression : $expression,
],
];
}

Expand Down
35 changes: 32 additions & 3 deletions tests/Doctrine/MongoDB/Tests/Aggregation/Stage/ReplaceRootTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ public function testReplaceRootStage()
->field('product')
->multiply('$field', 5);

$this->assertSame(['$replaceRoot' => ['product' => ['$multiply' => ['$field', 5]]]], $replaceRootStage->getExpression());
$this->assertEquals(
[
'$replaceRoot' => [
'newRoot' => (object) [
'product' => ['$multiply' => ['$field', 5]],
],
],
],
$replaceRootStage->getExpression()
);
}

public function testReplaceRootFromBuilder()
Expand All @@ -28,14 +37,34 @@ public function testReplaceRootFromBuilder()
->field('product')
->multiply('$field', 5);

$this->assertSame([['$replaceRoot' => ['product' => ['$multiply' => ['$field', 5]]]]], $builder->getPipeline());
$this->assertEquals(
[
[
'$replaceRoot' => [
'newRoot' => (object) [
'product' => ['$multiply' => ['$field', 5]],
],
],
],
],
$builder->getPipeline()
);
}

public function testReplaceWithEmbeddedDocument()
{
$builder = $this->getTestAggregationBuilder();
$builder->replaceRoot('$some.embedded.document');

$this->assertSame([['$replaceRoot' => '$some.embedded.document']], $builder->getPipeline());
$this->assertEquals(
[
[
'$replaceRoot' => [
'newRoot' => '$some.embedded.document'
]
]
],
$builder->getPipeline()
);
}
}

0 comments on commit 46b6316

Please sign in to comment.