Skip to content
This repository was archived by the owner on Feb 28, 2025. It is now read-only.

Commit b4e2b13

Browse files
authored
PHPLIB-1361 Add test on $let expression operator (#55)
1 parent ff4fc79 commit b4e2b13

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

generator/config/expression/let.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,26 @@ arguments:
2121
- expression
2222
description: |
2323
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'
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}

tests/Builder/Expression/Pipelines.php

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)