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

Commit 7deb802

Browse files
authored
PHPLIB-1347 Add tests on Conditional Expression Operators (#30)
1 parent 118184d commit 7deb802

File tree

8 files changed

+402
-54
lines changed

8 files changed

+402
-54
lines changed

generator/config/expression/cond.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,19 @@ arguments:
1919
name: else
2020
type:
2121
- expression
22+
tests:
23+
-
24+
name: 'Example'
25+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/cond/#example'
26+
pipeline:
27+
-
28+
$project:
29+
item: 1
30+
discount:
31+
$cond:
32+
if:
33+
$gte:
34+
- '$qty'
35+
- 250
36+
then: 30
37+
else: 20

generator/config/expression/ifNull.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,27 @@ arguments:
1212
type:
1313
- expression
1414
variadic: array
15+
tests:
16+
-
17+
name: 'Single Input Expression'
18+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/ifNull/#single-input-expression'
19+
pipeline:
20+
-
21+
$project:
22+
item: 1
23+
description:
24+
$ifNull:
25+
- '$description'
26+
- 'Unspecified'
27+
-
28+
name: 'Multiple Input Expressions'
29+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/ifNull/#multiple-input-expressions'
30+
pipeline:
31+
-
32+
$project:
33+
item: 1
34+
value:
35+
$ifNull:
36+
- '$description'
37+
- '$quantity'
38+
- 'Unspecified'

generator/config/expression/switch.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,47 @@ arguments:
2424
description: |
2525
The path to take if no branch case expression evaluates to true.
2626
Although optional, if default is unspecified and no branch case evaluates to true, $switch returns an error.
27+
tests:
28+
-
29+
name: 'Example'
30+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/switch/#example'
31+
pipeline:
32+
-
33+
$project:
34+
name: 1
35+
summary:
36+
$switch:
37+
branches:
38+
-
39+
case:
40+
$gte:
41+
-
42+
#$avg: '$scores'
43+
$avg: [ '$scores' ]
44+
- 90
45+
then: 'Doing great!'
46+
-
47+
case:
48+
$and:
49+
-
50+
$gte:
51+
-
52+
#$avg: '$scores'
53+
$avg: [ '$scores' ]
54+
- 80
55+
-
56+
$lt:
57+
-
58+
#$avg: '$scores'
59+
$avg: [ '$scores' ]
60+
- 90
61+
then: 'Doing pretty well.'
62+
-
63+
case:
64+
$lt:
65+
-
66+
#$avg: '$scores'
67+
$avg: [ '$scores' ]
68+
- 80
69+
then: 'Needs improvement.'
70+
default: 'No scores found.'
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
/**
13+
* Test $cond expression
14+
*/
15+
class CondOperatorTest extends PipelineTestCase
16+
{
17+
public function testExample(): void
18+
{
19+
$pipeline = new Pipeline(
20+
Stage::project(
21+
item: 1,
22+
discount: Expression::cond(
23+
if: Expression::gte(
24+
Expression::intFieldPath('qty'),
25+
250,
26+
),
27+
then: 30,
28+
else: 20,
29+
),
30+
),
31+
);
32+
33+
$this->assertSamePipeline(Pipelines::CondExample, $pipeline);
34+
}
35+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
/**
13+
* Test $ifNull expression
14+
*/
15+
class IfNullOperatorTest extends PipelineTestCase
16+
{
17+
public function testMultipleInputExpressions(): void
18+
{
19+
$pipeline = new Pipeline(
20+
Stage::project(
21+
item: 1,
22+
value: Expression::ifNull(
23+
Expression::fieldPath('description'),
24+
Expression::fieldPath('quantity'),
25+
'Unspecified',
26+
),
27+
),
28+
);
29+
30+
$this->assertSamePipeline(Pipelines::IfNullMultipleInputExpressions, $pipeline);
31+
}
32+
33+
public function testSingleInputExpression(): void
34+
{
35+
$pipeline = new Pipeline(
36+
Stage::project(
37+
item: 1,
38+
description: Expression::ifNull(
39+
Expression::fieldPath('description'),
40+
'Unspecified',
41+
),
42+
),
43+
);
44+
45+
$this->assertSamePipeline(Pipelines::IfNullSingleInputExpression, $pipeline);
46+
}
47+
}

tests/Builder/Expression/Pipelines.php

Lines changed: 167 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)