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

Commit 919c4e6

Browse files
authored
PHPLIB-1333 Add tests on Comparison Query Operators (#20)
$eq $gt $gte $in $lt $lte $ne $nin
1 parent b0886f8 commit 919c4e6

17 files changed

+709
-0
lines changed

generator/config/query/eq.yaml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,55 @@ arguments:
1111
name: value
1212
type:
1313
- any
14+
tests:
15+
-
16+
name: 'Equals a Specified Value'
17+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/eq/#equals-a-specified-value'
18+
pipeline:
19+
-
20+
$match:
21+
qty:
22+
$eq: 20
23+
24+
-
25+
name: 'Field in Embedded Document Equals a Value'
26+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/eq/#field-in-embedded-document-equals-a-value'
27+
pipeline:
28+
-
29+
$match:
30+
'item.name':
31+
$eq: 'ab'
32+
33+
-
34+
name: 'Equals an Array Value'
35+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/eq/#equals-an-array-value'
36+
pipeline:
37+
-
38+
$match:
39+
tags:
40+
$eq: ['A', 'B']
41+
42+
-
43+
name: 'Regex Match Behaviour'
44+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/eq/#regex-match-behaviour'
45+
pipeline:
46+
-
47+
$match:
48+
company: 'MongoDB'
49+
-
50+
$match:
51+
company:
52+
$eq: 'MongoDB'
53+
-
54+
$match:
55+
company:
56+
$regularExpression:
57+
options: ''
58+
pattern: '^MongoDB'
59+
-
60+
$match:
61+
company:
62+
$eq:
63+
$regularExpression:
64+
options: ''
65+
pattern: '^MongoDB'

generator/config/query/gt.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,12 @@ arguments:
1111
name: value
1212
type:
1313
- any
14+
tests:
15+
-
16+
name: 'Match Document Fields'
17+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/gt/#match-document-fields'
18+
pipeline:
19+
-
20+
$match:
21+
qty:
22+
$gt: 20

generator/config/query/gte.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,12 @@ arguments:
1111
name: value
1212
type:
1313
- any
14+
tests:
15+
-
16+
name: 'Match Document Fields'
17+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/gte/#match-document-fields'
18+
pipeline:
19+
-
20+
$match:
21+
qty:
22+
$gte: 20

generator/config/query/in.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,28 @@ arguments:
1111
name: value
1212
type:
1313
- array # of expression
14+
tests:
15+
-
16+
name: 'Use the $in Operator to Match Values in an Array'
17+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/in/#use-the--in-operator-to-match-values'
18+
pipeline:
19+
-
20+
$match:
21+
tags:
22+
$in: ['home', 'school']
23+
-
24+
name: 'Use the $in Operator with a Regular Expression'
25+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/in/#use-the--in-operator-with-a-regular-expression'
26+
pipeline:
27+
-
28+
$match:
29+
tags:
30+
$in:
31+
-
32+
$regularExpression:
33+
options: ''
34+
pattern: '^be'
35+
-
36+
$regularExpression:
37+
options: ''
38+
pattern: '^st'

generator/config/query/lt.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,12 @@ arguments:
1111
name: value
1212
type:
1313
- any
14+
tests:
15+
-
16+
name: 'Match Document Fields'
17+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/lt/#match-document-fields'
18+
pipeline:
19+
-
20+
$match:
21+
qty:
22+
$lt: 20

generator/config/query/lte.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,12 @@ arguments:
1111
name: value
1212
type:
1313
- any
14+
tests:
15+
-
16+
name: 'Match Document Fields'
17+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/lte/#match-document-fields'
18+
pipeline:
19+
-
20+
$match:
21+
qty:
22+
$lte: 20

generator/config/query/ne.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,12 @@ arguments:
1111
name: value
1212
type:
1313
- any
14+
tests:
15+
-
16+
name: 'Match Document Fields'
17+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/ne/#match-document-fields'
18+
pipeline:
19+
-
20+
$match:
21+
quantity:
22+
$ne: 20

generator/config/query/nin.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,20 @@ arguments:
1111
name: value
1212
type:
1313
- array # of expression
14+
tests:
15+
-
16+
name: 'Select on Unmatching Documents'
17+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/nin/#select-on-unmatching-documents'
18+
pipeline:
19+
-
20+
$match:
21+
quantity:
22+
$nin: [5, 15]
23+
-
24+
name: 'Select on Elements Not in an Array'
25+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/nin/#select-on-elements-not-in-an-array'
26+
pipeline:
27+
-
28+
$match:
29+
tags:
30+
$nin: ['school']
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Tests\Builder\Query;
6+
7+
use MongoDB\BSON\Regex;
8+
use MongoDB\Builder\Pipeline;
9+
use MongoDB\Builder\Query;
10+
use MongoDB\Builder\Stage;
11+
use MongoDB\Tests\Builder\PipelineTestCase;
12+
13+
/**
14+
* Test $eq query
15+
*/
16+
class EqOperatorTest extends PipelineTestCase
17+
{
18+
public function testEqualsASpecifiedValue(): void
19+
{
20+
$pipeline = new Pipeline(
21+
Stage::match(
22+
qty: Query::eq(20),
23+
),
24+
);
25+
26+
$this->assertSamePipeline(Pipelines::EqEqualsASpecifiedValue, $pipeline);
27+
}
28+
29+
public function testEqualsAnArrayValue(): void
30+
{
31+
$pipeline = new Pipeline(
32+
Stage::match(
33+
tags: Query::eq(['A', 'B']),
34+
),
35+
);
36+
37+
$this->assertSamePipeline(Pipelines::EqEqualsAnArrayValue, $pipeline);
38+
}
39+
40+
public function testFieldInEmbeddedDocumentEqualsAValue(): void
41+
{
42+
$pipeline = new Pipeline(
43+
Stage::match(
44+
...['item.name' => Query::eq('ab')],
45+
),
46+
);
47+
48+
$this->assertSamePipeline(Pipelines::EqFieldInEmbeddedDocumentEqualsAValue, $pipeline);
49+
}
50+
51+
public function testRegexMatchBehaviour(): void
52+
{
53+
$pipeline = new Pipeline(
54+
Stage::match(
55+
company: 'MongoDB',
56+
),
57+
Stage::match(
58+
company: Query::eq('MongoDB'),
59+
),
60+
Stage::match(
61+
company: new Regex('^MongoDB'),
62+
),
63+
Stage::match(
64+
company: Query::eq(new Regex('^MongoDB')),
65+
),
66+
);
67+
68+
$this->assertSamePipeline(Pipelines::EqRegexMatchBehaviour, $pipeline);
69+
}
70+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 $gt query
14+
*/
15+
class GtOperatorTest extends PipelineTestCase
16+
{
17+
public function testMatchDocumentFields(): void
18+
{
19+
$pipeline = new Pipeline(
20+
Stage::match(
21+
qty: Query::gt(20),
22+
),
23+
);
24+
25+
$this->assertSamePipeline(Pipelines::GtMatchDocumentFields, $pipeline);
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 $gte query
14+
*/
15+
class GteOperatorTest extends PipelineTestCase
16+
{
17+
public function testMatchDocumentFields(): void
18+
{
19+
$pipeline = new Pipeline(
20+
Stage::match(
21+
qty: Query::gte(20),
22+
),
23+
);
24+
25+
$this->assertSamePipeline(Pipelines::GteMatchDocumentFields, $pipeline);
26+
}
27+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Tests\Builder\Query;
6+
7+
use MongoDB\BSON\Regex;
8+
use MongoDB\Builder\Pipeline;
9+
use MongoDB\Builder\Query;
10+
use MongoDB\Builder\Stage;
11+
use MongoDB\Tests\Builder\PipelineTestCase;
12+
13+
/**
14+
* Test $in query
15+
*/
16+
class InOperatorTest extends PipelineTestCase
17+
{
18+
public function testUseTheInOperatorToMatchValuesInAnArray(): void
19+
{
20+
$pipeline = new Pipeline(
21+
Stage::match(
22+
tags: Query::in(['home', 'school']),
23+
),
24+
);
25+
26+
$this->assertSamePipeline(Pipelines::InUseTheInOperatorToMatchValuesInAnArray, $pipeline);
27+
}
28+
29+
public function testUseTheInOperatorWithARegularExpression(): void
30+
{
31+
$pipeline = new Pipeline(
32+
Stage::match(
33+
tags: Query::in([new Regex('^be'), new Regex('^st')]),
34+
),
35+
);
36+
37+
$this->assertSamePipeline(Pipelines::InUseTheInOperatorWithARegularExpression, $pipeline);
38+
}
39+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 $lt query
14+
*/
15+
class LtOperatorTest extends PipelineTestCase
16+
{
17+
public function testMatchDocumentFields(): void
18+
{
19+
$pipeline = new Pipeline(
20+
Stage::match(
21+
qty: Query::lt(20),
22+
),
23+
);
24+
25+
$this->assertSamePipeline(Pipelines::LtMatchDocumentFields, $pipeline);
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 $lte query
14+
*/
15+
class LteOperatorTest extends PipelineTestCase
16+
{
17+
public function testMatchDocumentFields(): void
18+
{
19+
$pipeline = new Pipeline(
20+
Stage::match(
21+
qty: Query::lte(20),
22+
),
23+
);
24+
25+
$this->assertSamePipeline(Pipelines::LteMatchDocumentFields, $pipeline);
26+
}
27+
}

0 commit comments

Comments
 (0)