From 96dfb4acd7978c106f4f15f37ca4d03159df3ca3 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Tue, 9 Jul 2019 22:03:07 +0200 Subject: [PATCH 1/2] Fix wrong syntax for replaceRoot stage --- .../MongoDB/Aggregation/Stage/ReplaceRoot.php | 7 +++- .../Aggregation/Stage/ReplaceRootTest.php | 35 +++++++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/lib/Doctrine/MongoDB/Aggregation/Stage/ReplaceRoot.php b/lib/Doctrine/MongoDB/Aggregation/Stage/ReplaceRoot.php index 25451e30..220cee25 100644 --- a/lib/Doctrine/MongoDB/Aggregation/Stage/ReplaceRoot.php +++ b/lib/Doctrine/MongoDB/Aggregation/Stage/ReplaceRoot.php @@ -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. @@ -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, + ], ]; } diff --git a/tests/Doctrine/MongoDB/Tests/Aggregation/Stage/ReplaceRootTest.php b/tests/Doctrine/MongoDB/Tests/Aggregation/Stage/ReplaceRootTest.php index 91f4616a..83f2c65d 100644 --- a/tests/Doctrine/MongoDB/Tests/Aggregation/Stage/ReplaceRootTest.php +++ b/tests/Doctrine/MongoDB/Tests/Aggregation/Stage/ReplaceRootTest.php @@ -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() @@ -28,7 +37,18 @@ 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() @@ -36,6 +56,15 @@ 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() + ); } } From 6b081b61d30d872ac031fb074727ba6994666391 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Tue, 9 Jul 2019 22:05:24 +0200 Subject: [PATCH 2/2] Update changelog for 1.6.4 --- CHANGELOG-1.6.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG-1.6.md b/CHANGELOG-1.6.md index fae13c63..10d1b4d4 100644 --- a/CHANGELOG-1.6.md +++ b/CHANGELOG-1.6.md @@ -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) ------------------