Skip to content

Commit 8089d73

Browse files
committed
Add test on adding raw stage
1 parent c33bad0 commit 8089d73

File tree

3 files changed

+51
-23
lines changed

3 files changed

+51
-23
lines changed

src/Query/AggregationBuilder.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
use Illuminate\Support\Collection as LaravelCollection;
88
use MongoDB\Builder\BuilderEncoder;
99
use MongoDB\Builder\Stage\FluentFactoryTrait;
10-
use MongoDB\Laravel\Collection;
10+
use MongoDB\Collection as MongoDBCollection;
11+
use MongoDB\Laravel\Collection as LaravelMongoDBCollection;
1112

1213
use function array_replace;
1314
use function collect;
@@ -17,9 +18,9 @@ final class AggregationBuilder
1718
use FluentFactoryTrait;
1819

1920
public function __construct(
20-
array $pipeline,
21-
private Collection $collection,
22-
private array $options,
21+
private MongoDBCollection|LaravelMongoDBCollection $collection,
22+
array $pipeline = [],
23+
private array $options = [],
2324
) {
2425
$this->pipeline = $pipeline;
2526
}

src/Query/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ private function getAggregationBuilder(): AggregationBuilder
290290
throw new BadMethodCallException('Aggregation builder requires package mongodb/builder 0.2+');
291291
}
292292

293-
$agg = new AggregationBuilder([], $this->collection, $this->options);
293+
$agg = new AggregationBuilder($this->collection, [], $this->options);
294294

295295
$wheres = $this->compileWheres();
296296

tests/Query/AggregationBuilderTest.php

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111
use MongoDB\BSON\UTCDateTime;
1212
use MongoDB\Builder\BuilderEncoder;
1313
use MongoDB\Builder\Expression;
14+
use MongoDB\Builder\Pipeline;
1415
use MongoDB\Builder\Type\Sort;
16+
use MongoDB\Collection as MongoDBCollection;
1517
use MongoDB\Laravel\Query\AggregationBuilder;
1618
use MongoDB\Laravel\Tests\Models\User;
1719
use MongoDB\Laravel\Tests\TestCase;
1820

21+
use function is_array;
22+
1923
class AggregationBuilderTest extends TestCase
2024
{
2125
public function tearDown(): void
@@ -48,28 +52,20 @@ public function testCreateFromQueryBuilder(): void
4852
->sort(year: Sort::Desc, name: Sort::Asc)
4953
->unset('birthday');
5054

51-
// The encoder is used to convert the pipeline to a BSON document
52-
$codec = new BuilderEncoder();
53-
$json = Document::fromPHP([
54-
'pipeline' => $codec->encode($pipeline->getPipeline()),
55-
])->toCanonicalExtendedJSON();
56-
5755
// Compare with the expected pipeline
58-
$expected = Document::fromPHP([
59-
'pipeline' => [
60-
['$match' => ['name' => 'John Doe']],
61-
['$limit' => 10],
62-
[
63-
'$addFields' => [
64-
'year' => ['$year' => ['date' => '$birthday']],
65-
],
56+
$expected = [
57+
['$match' => ['name' => 'John Doe']],
58+
['$limit' => 10],
59+
[
60+
'$addFields' => [
61+
'year' => ['$year' => ['date' => '$birthday']],
6662
],
67-
['$sort' => ['year' => -1, 'name' => 1]],
68-
['$unset' => ['birthday']],
6963
],
70-
])->toCanonicalExtendedJSON();
64+
['$sort' => ['year' => -1, 'name' => 1]],
65+
['$unset' => ['birthday']],
66+
];
7167

72-
$this->assertJsonStringEqualsJsonString($expected, $json);
68+
$this->assertSamePipeline($expected, $pipeline->getPipeline());
7369

7470
// Execute the pipeline and validate the results
7571
$results = $pipeline->get();
@@ -81,4 +77,35 @@ public function testCreateFromQueryBuilder(): void
8177
$this->assertIsInt($results->first()['year']);
8278
$this->assertArrayNotHasKey('birthday', $results->first());
8379
}
80+
81+
public function testAddRawStage(): void
82+
{
83+
$collection = $this->createMock(MongoDBCollection::class);
84+
85+
$pipeline = new AggregationBuilder($collection);
86+
$pipeline
87+
->addRawStage('$match', ['name' => 'John Doe'])
88+
->addRawStage('$limit', 10)
89+
->addRawStage('$replaceRoot', (object) ['newRoot' => '$$ROOT']);
90+
91+
$expected = [
92+
['$match' => ['name' => 'John Doe']],
93+
['$limit' => 10],
94+
['$replaceRoot' => ['$newRoot' => '$$ROOT']],
95+
];
96+
97+
$this->assertSamePipeline($expected, $pipeline->getPipeline());
98+
}
99+
100+
private static function assertSamePipeline(array $expected, Pipeline $pipeline): void
101+
{
102+
$expected = Document::fromPHP(['pipeline' => $expected])->toCanonicalExtendedJSON();
103+
104+
$codec = new BuilderEncoder();
105+
$actual = $codec->encode($pipeline);
106+
// Normalize with BSON round-trip
107+
$actual = Document::fromPHP(['pipeline' => $actual])->toCanonicalExtendedJSON();
108+
109+
self::assertJsonStringEqualsJsonString($expected, $actual);
110+
}
84111
}

0 commit comments

Comments
 (0)