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

Commit b1300df

Browse files
committed
PHPLIB-1340 Add tests on Projection Operators
1 parent aa7cffc commit b1300df

File tree

9 files changed

+333
-67
lines changed

9 files changed

+333
-67
lines changed

generator/config/projection/elemMatch.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,30 @@ arguments:
1111
name: query
1212
type:
1313
- query
14+
tests:
15+
-
16+
name: 'Zipcode Search'
17+
link: 'https://www.mongodb.com/docs/manual/reference/operator/projection/elemMatch/#zipcode-search'
18+
pipeline:
19+
-
20+
$match:
21+
zipcode: '63109'
22+
-
23+
$project:
24+
students:
25+
$elemMatch:
26+
school: 102
27+
-
28+
name: 'with Multiple Fields'
29+
link: 'https://www.mongodb.com/docs/manual/reference/operator/projection/elemMatch/#with-multiple-fields'
30+
pipeline:
31+
-
32+
$match:
33+
zipcode: '63109'
34+
-
35+
$project:
36+
students:
37+
$elemMatch:
38+
school: 102
39+
age:
40+
$gt: 10

generator/config/projection/filter.yaml

Lines changed: 0 additions & 34 deletions
This file was deleted.

generator/config/projection/slice.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,41 @@ arguments:
1515
name: skip
1616
type:
1717
- int
18+
optional: true
19+
tests:
20+
-
21+
name: 'Return an Array with Its First 3 Elements'
22+
link: 'https://www.mongodb.com/docs/manual/reference/operator/projection/slice/#return-an-array-with-its-first-3-elements'
23+
pipeline:
24+
-
25+
$project:
26+
comments:
27+
# Example uses the short form, the builder always generated the verbose form
28+
# $slice: 3
29+
$slice: [3]
30+
-
31+
name: 'Return an Array with Its Last 3 Elements'
32+
link: 'https://www.mongodb.com/docs/manual/reference/operator/projection/slice/#return-an-array-with-its-last-3-elements'
33+
pipeline:
34+
-
35+
$project:
36+
comments:
37+
# Example uses the short form, the builder always generated the verbose form
38+
# $slice: -3
39+
$slice: [-3]
40+
-
41+
name: 'Return an Array with 3 Elements After Skipping the First Element'
42+
link: 'https://www.mongodb.com/docs/manual/reference/operator/projection/slice/#return-an-array-with-3-elements-after-skipping-the-first-element'
43+
pipeline:
44+
-
45+
$project:
46+
comments:
47+
$slice: [1, 3]
48+
-
49+
name: 'Return an Array with 3 Elements After Skipping the Last Element'
50+
link: 'https://www.mongodb.com/docs/manual/reference/operator/projection/slice/#return-an-array-with-3-elements-after-skipping-the-last-element'
51+
pipeline:
52+
-
53+
$project:
54+
comments:
55+
$slice: [-1, 3]

src/Builder/Projection/FactoryTrait.php

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

src/Builder/Projection/SliceOperator.php

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

tests/Builder/BuilderEncoderTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use MongoDB\Builder\BuilderEncoder;
1111
use MongoDB\Builder\Expression;
1212
use MongoDB\Builder\Pipeline;
13-
use MongoDB\Builder\Projection;
1413
use MongoDB\Builder\Query;
1514
use MongoDB\Builder\Stage;
1615
use MongoDB\Builder\Variable;
@@ -157,7 +156,7 @@ public function testExpressionFilter(array $limit, array $expectedLimit): void
157156
{
158157
$pipeline = new Pipeline(
159158
Stage::project(
160-
items: Projection::filter(
159+
items: Expression::filter(
161160
...$limit,
162161
input: Expression::arrayFieldPath('items'),
163162
cond: Expression::gte(Expression::variable('item.price'), 100),
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Tests\Builder\Projection;
6+
7+
use MongoDB\Builder\Pipeline;
8+
use MongoDB\Builder\Projection;
9+
use MongoDB\Builder\Query;
10+
use MongoDB\Builder\Stage;
11+
use MongoDB\Tests\Builder\PipelineTestCase;
12+
13+
/**
14+
* Test $elemMatch projection
15+
*/
16+
class ElemMatchOperatorTest extends PipelineTestCase
17+
{
18+
public function testWithMultipleFields(): void
19+
{
20+
$pipeline = new Pipeline(
21+
Stage::match(
22+
zipcode: '63109',
23+
),
24+
Stage::project(
25+
students: Projection::elemMatch(
26+
Query::query(
27+
school: 102,
28+
age: Query::gt(10),
29+
),
30+
),
31+
),
32+
);
33+
34+
$this->assertSamePipeline(Pipelines::ElemMatchWithMultipleFields, $pipeline);
35+
}
36+
37+
public function testZipcodeSearch(): void
38+
{
39+
$pipeline = new Pipeline(
40+
Stage::match(
41+
zipcode: '63109',
42+
),
43+
Stage::project(
44+
students: Projection::elemMatch(
45+
Query::query(
46+
school: 102,
47+
),
48+
),
49+
),
50+
);
51+
52+
$this->assertSamePipeline(Pipelines::ElemMatchZipcodeSearch, $pipeline);
53+
}
54+
}

0 commit comments

Comments
 (0)