File tree Expand file tree Collapse file tree 3 files changed +111
-0
lines changed
generator/config/expression Expand file tree Collapse file tree 3 files changed +111
-0
lines changed Original file line number Diff line number Diff line change @@ -21,3 +21,26 @@ arguments:
21
21
- expression
22
22
description : |
23
23
The expression to evaluate.
24
+ tests :
25
+ -
26
+ name : ' Example'
27
+ link : ' https://www.mongodb.com/docs/manual/reference/operator/aggregation/let/#example'
28
+ pipeline :
29
+ -
30
+ $project :
31
+ finalTotal :
32
+ $let :
33
+ vars :
34
+ total :
35
+ $add :
36
+ - ' $price'
37
+ - ' $tax'
38
+ discounted :
39
+ $cond :
40
+ if : ' $applyDiscount'
41
+ then : 0.9
42
+ else : 1
43
+ in :
44
+ $multiply :
45
+ - ' $$total'
46
+ - ' $$discounted'
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace MongoDB \Tests \Builder \Expression ;
6
+
7
+ use MongoDB \Builder \Expression ;
8
+ use MongoDB \Builder \Pipeline ;
9
+ use MongoDB \Builder \Stage ;
10
+ use MongoDB \Tests \Builder \PipelineTestCase ;
11
+
12
+ use function MongoDB \object ;
13
+
14
+ /**
15
+ * Test $let expression
16
+ */
17
+ class LetOperatorTest extends PipelineTestCase
18
+ {
19
+ public function testExample (): void
20
+ {
21
+ $ pipeline = new Pipeline (
22
+ Stage::project (
23
+ finalTotal: Expression::let (
24
+ vars: object (
25
+ total: Expression::add (
26
+ Expression::numberFieldPath ('price ' ),
27
+ Expression::numberFieldPath ('tax ' ),
28
+ ),
29
+ discounted: Expression::cond (
30
+ if: Expression::boolFieldPath ('applyDiscount ' ),
31
+ then: 0.9 ,
32
+ else: 1 ,
33
+ ),
34
+ ),
35
+ in: Expression::multiply (
36
+ Expression::variable ('total ' ),
37
+ Expression::variable ('discounted ' ),
38
+ ),
39
+ ),
40
+ ),
41
+ );
42
+
43
+ $ this ->assertSamePipeline (Pipelines::LetExample, $ pipeline );
44
+ }
45
+ }
You can’t perform that action at this time.
0 commit comments