11
11
use MongoDB \BSON \UTCDateTime ;
12
12
use MongoDB \Builder \BuilderEncoder ;
13
13
use MongoDB \Builder \Expression ;
14
+ use MongoDB \Builder \Pipeline ;
14
15
use MongoDB \Builder \Type \Sort ;
16
+ use MongoDB \Collection as MongoDBCollection ;
15
17
use MongoDB \Laravel \Query \AggregationBuilder ;
16
18
use MongoDB \Laravel \Tests \Models \User ;
17
19
use MongoDB \Laravel \Tests \TestCase ;
18
20
21
+ use function is_array ;
22
+
19
23
class AggregationBuilderTest extends TestCase
20
24
{
21
25
public function tearDown (): void
@@ -48,28 +52,20 @@ public function testCreateFromQueryBuilder(): void
48
52
->sort (year: Sort::Desc, name: Sort::Asc)
49
53
->unset ('birthday ' );
50
54
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
-
57
55
// 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 ' ]],
66
62
],
67
- ['$sort ' => ['year ' => -1 , 'name ' => 1 ]],
68
- ['$unset ' => ['birthday ' ]],
69
63
],
70
- ])->toCanonicalExtendedJSON ();
64
+ ['$sort ' => ['year ' => -1 , 'name ' => 1 ]],
65
+ ['$unset ' => ['birthday ' ]],
66
+ ];
71
67
72
- $ this ->assertJsonStringEqualsJsonString ($ expected , $ json );
68
+ $ this ->assertSamePipeline ($ expected , $ pipeline -> getPipeline () );
73
69
74
70
// Execute the pipeline and validate the results
75
71
$ results = $ pipeline ->get ();
@@ -81,4 +77,35 @@ public function testCreateFromQueryBuilder(): void
81
77
$ this ->assertIsInt ($ results ->first ()['year ' ]);
82
78
$ this ->assertArrayNotHasKey ('birthday ' , $ results ->first ());
83
79
}
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
+ }
84
111
}
0 commit comments