|
5 | 5 | namespace MongoDB\Tests\Builder\Type;
|
6 | 6 |
|
7 | 7 | use Generator;
|
| 8 | +use MongoDB\Builder\Query\EqOperator; |
| 9 | +use MongoDB\Builder\Query\GtOperator; |
8 | 10 | use MongoDB\Builder\Type\CombinedFieldQuery;
|
9 | 11 | use MongoDB\Exception\InvalidArgumentException;
|
10 | 12 | use PHPUnit\Framework\TestCase;
|
11 | 13 |
|
12 | 14 | class CombinedFieldQueryTest extends TestCase
|
13 | 15 | {
|
14 |
| - public function testEmptyFieldQueries(): void |
| 16 | + public function testEmpty(): void |
15 | 17 | {
|
16 | 18 | $fieldQueries = new CombinedFieldQuery([]);
|
17 | 19 |
|
18 | 20 | $this->assertSame([], $fieldQueries->fieldQueries);
|
19 | 21 | }
|
20 | 22 |
|
21 |
| - public function testFieldQueries(): void |
| 23 | + public function testSupportedTypes(): void |
22 | 24 | {
|
23 | 25 | $fieldQueries = new CombinedFieldQuery([
|
24 |
| - $this->createMock(CombinedFieldQuery::class), |
| 26 | + new EqOperator(1), |
25 | 27 | ['$gt' => 1],
|
26 |
| - new CombinedFieldQuery([]), |
| 28 | + (object) ['$lt' => 1], |
| 29 | + ]); |
| 30 | + |
| 31 | + $this->assertCount(3, $fieldQueries->fieldQueries); |
| 32 | + } |
| 33 | + |
| 34 | + public function testFlattenCombinedFieldQueries(): void |
| 35 | + { |
| 36 | + $fieldQueries = new CombinedFieldQuery([ |
| 37 | + new CombinedFieldQuery([ |
| 38 | + new CombinedFieldQuery([ |
| 39 | + ['$lt' => 1], |
| 40 | + new CombinedFieldQuery([]), |
| 41 | + ]), |
| 42 | + ['$gt' => 1], |
| 43 | + ]), |
| 44 | + ['$gte' => 1], |
27 | 45 | ]);
|
28 | 46 |
|
29 | 47 | $this->assertCount(3, $fieldQueries->fieldQueries);
|
30 | 48 | }
|
31 | 49 |
|
32 | 50 | /** @dataProvider provideInvalidFieldQuery */
|
33 |
| - public function testRejectInvalidFieldQueries($invalidQuery): void |
| 51 | + public function testRejectInvalidFieldQueries(mixed $invalidQuery, string $message = '-'): void |
34 | 52 | {
|
35 | 53 | $this->expectException(InvalidArgumentException::class);
|
| 54 | + $this->expectExceptionMessage($message); |
36 | 55 |
|
37 | 56 | new CombinedFieldQuery([$invalidQuery]);
|
38 | 57 | }
|
39 | 58 |
|
40 | 59 | public static function provideInvalidFieldQuery(): Generator
|
41 | 60 | {
|
42 |
| - yield 'int' => [1]; |
43 |
| - yield 'float' => [1.1]; |
44 |
| - yield 'string' => ['foo']; |
45 |
| - yield 'bool' => [true]; |
46 |
| - yield 'null' => [null]; |
| 61 | + yield 'int' => [1, 'Expected filters to be a list of field query operators, array or stdClass, int given']; |
| 62 | + yield 'float' => [1.1, 'Expected filters to be a list of field query operators, array or stdClass, float given']; |
| 63 | + yield 'string' => ['foo', 'Expected filters to be a list of field query operators, array or stdClass, string given']; |
| 64 | + yield 'bool' => [true, 'Expected filters to be a list of field query operators, array or stdClass, bool given']; |
| 65 | + yield 'null' => [null, 'Expected filters to be a list of field query operators, array or stdClass, null given']; |
| 66 | + yield 'empty array' => [[], 'Operator must contain exactly one key, 0 given']; |
| 67 | + yield 'array with two keys' => [['$eq' => 1, '$ne' => 2], 'Operator must contain exactly one key, 2 given']; |
| 68 | + yield 'array key without $' => [['eq' => 1], 'Operator must contain exactly one key starting with $, "eq" given']; |
| 69 | + yield 'empty object' => [(object) [], 'Operator must contain exactly one key, 0 given']; |
| 70 | + yield 'object with two keys' => [(object) ['$eq' => 1, '$ne' => 2], 'Operator must contain exactly one key, 2 given']; |
| 71 | + yield 'object key without $' => [(object) ['eq' => 1], 'Operator must contain exactly one key starting with $, "eq" given']; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @param array<mixed> $fieldQueries |
| 76 | + * |
| 77 | + * @dataProvider provideDuplicateOperator |
| 78 | + */ |
| 79 | + public function testRejectDuplicateOperator(array $fieldQueries): void |
| 80 | + { |
| 81 | + $this->expectException(InvalidArgumentException::class); |
| 82 | + $this->expectExceptionMessage('Duplicate operator "$eq" detected'); |
| 83 | + |
| 84 | + new CombinedFieldQuery([ |
| 85 | + ['$eq' => 1], |
| 86 | + new EqOperator(2), |
| 87 | + ]); |
| 88 | + } |
| 89 | + |
| 90 | + public function provideDuplicateOperator(): Generator |
| 91 | + { |
| 92 | + yield 'array and FieldQuery' => [ |
| 93 | + [ |
| 94 | + ['$eq' => 1], |
| 95 | + new EqOperator(2), |
| 96 | + ], |
| 97 | + ]; |
| 98 | + |
| 99 | + yield 'object and FieldQuery' => [ |
| 100 | + [ |
| 101 | + (object) ['$gt' => 1], |
| 102 | + new GtOperator(2), |
| 103 | + ], |
| 104 | + ]; |
| 105 | + |
| 106 | + yield 'object and array' => [ |
| 107 | + [ |
| 108 | + (object) ['$ne' => 1], |
| 109 | + ['$ne' => 2], |
| 110 | + ], |
| 111 | + ]; |
47 | 112 | }
|
48 | 113 | }
|
0 commit comments