Skip to content

Commit

Permalink
Fix wrong syntax for replaceRoot stage
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Jul 11, 2019
1 parent a50624c commit b678b7d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
6 changes: 5 additions & 1 deletion lib/Doctrine/ODM/MongoDB/Aggregation/Stage/ReplaceRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ public function __construct(Builder $builder, DocumentManager $documentManager,
*/
public function getExpression() : array
{
$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
8 changes: 6 additions & 2 deletions tests/Doctrine/ODM/MongoDB/Tests/Aggregation/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,11 @@ public function testPipelineConvertsTypes()
],
[
'$replaceRoot' => [
'isToday' => ['$eq' => ['$createdAt', new UTCDateTime((int) $dateTime->format('Uv'))]],
'newRoot' => (object) [
'isToday' => [
'$eq' => ['$createdAt', new UTCDateTime((int) $dateTime->format('Uv'))],
],
],
],
],
];
Expand Down Expand Up @@ -265,7 +269,7 @@ public function testFieldNameConversion()
[
'$sort' => ['ip' => 1],
],
['$replaceRoot' => '$ip'],
['$replaceRoot' => ['newRoot' => '$ip']],
];

$this->assertEquals($expectedPipeline, $builder->getPipeline());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public function testTypeConversion()
$this->assertEquals(
[
'$replaceRoot' => [
'isToday' => ['$eq' => ['$createdAt', $mongoDate]],
'newRoot' => (object) [
'isToday' => ['$eq' => ['$createdAt', $mongoDate]],
],
],
],
$stage->getExpression()
Expand All @@ -49,7 +51,9 @@ public function testTypeConversionWithDirectExpression()
$this->assertEquals(
[
'$replaceRoot' => [
'isToday' => ['$eq' => ['$createdAt', $mongoDate]],
'newRoot' => (object) [
'isToday' => ['$eq' => ['$createdAt', $mongoDate]],
],
],
],
$stage->getExpression()
Expand All @@ -68,7 +72,9 @@ public function testFieldNameConversion()
$this->assertEquals(
[
'$replaceRoot' => [
'someField' => ['$concat' => ['$ip', 'foo']],
'newRoot' => (object) [
'someField' => ['$concat' => ['$ip', 'foo']],
],
],
],
$stage->getExpression()
Expand All @@ -83,7 +89,9 @@ public function testFieldNameConversionWithDirectExpression()
->replaceRoot('$authorIp');

$this->assertEquals(
['$replaceRoot' => '$ip'],
[
'$replaceRoot' => ['newRoot' => '$ip']
],
$stage->getExpression()
);
}
Expand Down

0 comments on commit b678b7d

Please sign in to comment.