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

Commit 4176b5a

Browse files
authored
PHPLIB-1334 Add tests on Logical Query Operators (#21)
https://www.mongodb.com/docs/manual/reference/operator/query/#logical $and $not $nor $or
1 parent 919c4e6 commit 4176b5a

File tree

9 files changed

+651
-0
lines changed

9 files changed

+651
-0
lines changed

generator/config/query/and.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,39 @@ arguments:
1313
- query
1414
variadic: array
1515
variadicMin: 1
16+
tests:
17+
-
18+
name: 'AND Queries With Multiple Expressions Specifying the Same Field'
19+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/and/#and-queries-with-multiple-expressions-specifying-the-same-field'
20+
pipeline:
21+
-
22+
$match:
23+
$and:
24+
-
25+
price:
26+
$ne: 1.99
27+
-
28+
price:
29+
$exists: true
30+
-
31+
name: 'AND Queries With Multiple Expressions Specifying the Same Operator'
32+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/and/#and-queries-with-multiple-expressions-specifying-the-same-operator'
33+
pipeline:
34+
-
35+
$match:
36+
$and:
37+
-
38+
$or:
39+
-
40+
qty:
41+
$lt: 10
42+
-
43+
qty:
44+
$gt: 50
45+
-
46+
$or:
47+
-
48+
sale: true
49+
-
50+
price:
51+
$lt: 5

generator/config/query/nor.yaml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,46 @@ arguments:
1313
- query
1414
variadic: array
1515
variadicMin: 1
16+
tests:
17+
-
18+
name: 'Query with Two Expressions'
19+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/nor/#-nor-query-with-two-expressions'
20+
pipeline:
21+
-
22+
$match:
23+
$nor:
24+
-
25+
price: 1.99
26+
-
27+
sale: true
28+
-
29+
name: 'Additional Comparisons'
30+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/nor/#-nor-and-additional-comparisons'
31+
pipeline:
32+
-
33+
$match:
34+
$nor:
35+
-
36+
price: 1.99
37+
-
38+
qty:
39+
$lt: 20
40+
-
41+
sale: true
42+
-
43+
name: '$nor and $exists'
44+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/nor/#-nor-and--exists'
45+
pipeline:
46+
-
47+
$match:
48+
$nor:
49+
-
50+
price: 1.99
51+
-
52+
price:
53+
$exists: false
54+
-
55+
sale: true
56+
-
57+
sale:
58+
$exists: false

generator/config/query/not.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,24 @@ arguments:
1111
name: expression
1212
type:
1313
- fieldQuery
14+
tests:
15+
-
16+
name: 'Syntax'
17+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/not/#syntax'
18+
pipeline:
19+
-
20+
$match:
21+
price:
22+
$not:
23+
$gt: 1.99
24+
-
25+
name: 'Regular Expressions'
26+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/not/#-not-and-regular-expressions'
27+
pipeline:
28+
-
29+
$match:
30+
price:
31+
$not:
32+
$regularExpression:
33+
pattern: '^p.*'
34+
options: ''

generator/config/query/or.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,35 @@ arguments:
1313
- query
1414
variadic: array
1515
variadicMin: 1
16+
tests:
17+
-
18+
name: '$or Clauses'
19+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/or/#-or-clauses-and-indexes'
20+
pipeline:
21+
-
22+
$match:
23+
$or:
24+
-
25+
quantity:
26+
$lt: 20
27+
-
28+
price: 10
29+
30+
-
31+
name: 'Error Handling'
32+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/or/#error-handling'
33+
pipeline:
34+
-
35+
$match:
36+
$or:
37+
-
38+
x:
39+
$eq: 0
40+
-
41+
$expr:
42+
$eq:
43+
-
44+
$divide:
45+
- 1
46+
- '$x'
47+
- 3
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 $and query
14+
*/
15+
class AndOperatorTest extends PipelineTestCase
16+
{
17+
public function testANDQueriesWithMultipleExpressionsSpecifyingTheSameField(): void
18+
{
19+
$pipeline = new Pipeline(
20+
Stage::match(
21+
Query::and(
22+
Query::query(
23+
price: Query::ne(1.99),
24+
),
25+
Query::query(
26+
price: Query::exists(true),
27+
),
28+
),
29+
),
30+
);
31+
32+
$this->assertSamePipeline(Pipelines::AndANDQueriesWithMultipleExpressionsSpecifyingTheSameField, $pipeline);
33+
}
34+
35+
public function testANDQueriesWithMultipleExpressionsSpecifyingTheSameOperator(): void
36+
{
37+
$pipeline = new Pipeline(
38+
Stage::match(
39+
Query::and(
40+
Query::or(
41+
Query::query(
42+
qty: Query::lt(10),
43+
),
44+
Query::query(
45+
qty: Query::gt(50),
46+
),
47+
),
48+
Query::or(
49+
Query::query(
50+
sale: true,
51+
),
52+
Query::query(
53+
price: Query::lt(5),
54+
),
55+
),
56+
),
57+
),
58+
);
59+
60+
$this->assertSamePipeline(Pipelines::AndANDQueriesWithMultipleExpressionsSpecifyingTheSameOperator, $pipeline);
61+
}
62+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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 $nor query
14+
*/
15+
class NorOperatorTest extends PipelineTestCase
16+
{
17+
public function testAdditionalComparisons(): void
18+
{
19+
$pipeline = new Pipeline(
20+
Stage::match(
21+
Query::nor(
22+
Query::query(
23+
price: 1.99,
24+
),
25+
Query::query(
26+
qty: Query::lt(20),
27+
),
28+
Query::query(
29+
sale: true,
30+
),
31+
),
32+
),
33+
);
34+
35+
$this->assertSamePipeline(Pipelines::NorAdditionalComparisons, $pipeline);
36+
}
37+
38+
public function testNorAndExists(): void
39+
{
40+
$pipeline = new Pipeline(
41+
Stage::match(
42+
Query::nor(
43+
Query::query(
44+
price: 1.99,
45+
),
46+
Query::query(
47+
price: Query::exists(false),
48+
),
49+
Query::query(
50+
sale: true,
51+
),
52+
Query::query(
53+
sale: Query::exists(false),
54+
),
55+
),
56+
),
57+
);
58+
59+
$this->assertSamePipeline(Pipelines::NorNorAndExists, $pipeline);
60+
}
61+
62+
public function testQueryWithTwoExpressions(): void
63+
{
64+
$pipeline = new Pipeline(
65+
Stage::match(
66+
Query::nor(
67+
Query::query(
68+
price: 1.99,
69+
),
70+
Query::query(
71+
sale: true,
72+
),
73+
),
74+
),
75+
);
76+
77+
$this->assertSamePipeline(Pipelines::NorQueryWithTwoExpressions, $pipeline);
78+
}
79+
}
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\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 $not query
15+
*/
16+
class NotOperatorTest extends PipelineTestCase
17+
{
18+
public function testRegularExpressions(): void
19+
{
20+
$pipeline = new Pipeline(
21+
Stage::match(
22+
price: Query::not(
23+
new Regex('^p.*'),
24+
),
25+
),
26+
);
27+
28+
$this->assertSamePipeline(Pipelines::NotRegularExpressions, $pipeline);
29+
}
30+
31+
public function testSyntax(): void
32+
{
33+
$pipeline = new Pipeline(
34+
Stage::match(
35+
price: Query::not(
36+
Query::gt(1.99),
37+
),
38+
),
39+
);
40+
41+
$this->assertSamePipeline(Pipelines::NotSyntax, $pipeline);
42+
}
43+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Tests\Builder\Query;
6+
7+
use MongoDB\Builder\Expression;
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 $or query
15+
*/
16+
class OrOperatorTest extends PipelineTestCase
17+
{
18+
public function testErrorHandling(): void
19+
{
20+
$pipeline = new Pipeline(
21+
Stage::match(
22+
Query::or(
23+
Query::query(
24+
x: Query::eq(0),
25+
),
26+
Query::expr(
27+
Expression::eq(
28+
Expression::divide(
29+
1,
30+
Expression::intFieldPath('x'),
31+
),
32+
3,
33+
),
34+
),
35+
),
36+
),
37+
);
38+
39+
$this->assertSamePipeline(Pipelines::OrErrorHandling, $pipeline);
40+
}
41+
42+
public function testOrClauses(): void
43+
{
44+
$pipeline = new Pipeline(
45+
Stage::match(
46+
Query::or(
47+
Query::query(
48+
quantity: Query::lt(20),
49+
),
50+
Query::query(
51+
price: 10,
52+
),
53+
),
54+
),
55+
);
56+
57+
$this->assertSamePipeline(Pipelines::OrOrClauses, $pipeline);
58+
}
59+
}

0 commit comments

Comments
 (0)