Skip to content

Commit f665eb3

Browse files
authored
PHPLIB-1335 Add tests on Element Query Operators (mongodb#23)
* PHPLIB-1335 Add tests on Element Query Operators * Make $type query operator variadic
1 parent 320cc26 commit f665eb3

File tree

7 files changed

+410
-13
lines changed

7 files changed

+410
-13
lines changed

generator/config/query/exists.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,21 @@ arguments:
1111
name: exists
1212
type:
1313
- bool
14+
tests:
15+
-
16+
name: 'Exists and Not Equal To'
17+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/exists/#exists-and-not-equal-to'
18+
pipeline:
19+
-
20+
$match:
21+
qty:
22+
$exists: true
23+
$nin: [5, 15]
24+
-
25+
name: 'Null Values'
26+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/exists/#null-values'
27+
pipeline:
28+
-
29+
$match:
30+
qty:
31+
$exists: true

generator/config/query/type.yaml

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,77 @@ arguments:
1212
type:
1313
- int
1414
- string
15-
- array # of int or string
15+
variadic: array
16+
tests:
17+
-
18+
name: 'Querying by Data Type'
19+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/type/#querying-by-data-type'
20+
pipeline:
21+
-
22+
$match:
23+
zipCode:
24+
# Example uses the short form, the builder always generated the verbose form
25+
# $type: 2
26+
$type: [2]
27+
-
28+
$match:
29+
zipCode:
30+
# Example uses the short form, the builder always generated the verbose form
31+
# $type: 'string'
32+
$type: ['string']
33+
-
34+
$match:
35+
zipCode:
36+
# Example uses the short form, the builder always generated the verbose form
37+
# $type: 1
38+
$type: [1]
39+
-
40+
$match:
41+
zipCode:
42+
# Example uses the short form, the builder always generated the verbose form
43+
# $type: 'double'
44+
$type: ['double']
45+
-
46+
$match:
47+
zipCode:
48+
# Example uses the short form, the builder always generated the verbose form
49+
# $type: 'number'
50+
$type: ['number']
51+
-
52+
name: 'Querying by Multiple Data Type'
53+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/type/#querying-by-multiple-data-type'
54+
pipeline:
55+
-
56+
$match:
57+
zipCode:
58+
$type: [2, 1]
59+
-
60+
$match:
61+
zipCode:
62+
$type: ['string', 'double']
63+
-
64+
name: 'Querying by MinKey and MaxKey'
65+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/type/#querying-by-minkey-and-maxkey'
66+
pipeline:
67+
-
68+
$match:
69+
zipCode:
70+
# Example uses the short form, the builder always generated the verbose form
71+
# $type: 'minKey'
72+
$type: ['minKey']
73+
-
74+
$match:
75+
zipCode:
76+
# Example uses the short form, the builder always generated the verbose form
77+
# $type: 'maxKey'
78+
$type: ['maxKey']
79+
-
80+
name: 'Querying by Array Type'
81+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/type/#querying-by-array-type'
82+
pipeline:
83+
-
84+
$match:
85+
zipCode:
86+
# Example uses the short form, the builder always generated the verbose form
87+
# $type: 'array'
88+
$type: ['array']

src/Builder/Query/FactoryTrait.php

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

src/Builder/Query/TypeOperator.php

Lines changed: 11 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Tests\Builder\Query;
6+
7+
use MongoDB\Builder\Pipeline;
8+
use MongoDB\Builder\Query;
9+
use MongoDB\Builder\Stage;
10+
use MongoDB\Tests\Builder\PipelineTestCase;
11+
12+
/**
13+
* Test $exists query
14+
*/
15+
class ExistsOperatorTest extends PipelineTestCase
16+
{
17+
public function testExistsAndNotEqualTo(): void
18+
{
19+
$pipeline = new Pipeline(
20+
Stage::match(
21+
qty: [
22+
Query::exists(true),
23+
Query::nin([5, 15]),
24+
],
25+
),
26+
);
27+
28+
$this->assertSamePipeline(Pipelines::ExistsExistsAndNotEqualTo, $pipeline);
29+
}
30+
31+
public function testNullValues(): void
32+
{
33+
$pipeline = new Pipeline(
34+
Stage::match(
35+
qty: [
36+
Query::exists(true),
37+
],
38+
),
39+
);
40+
41+
$this->assertSamePipeline(Pipelines::ExistsNullValues, $pipeline);
42+
}
43+
}

0 commit comments

Comments
 (0)