|
9 | 9 | use Doctrine\Persistence\Mapping\Driver\MappingDriver;
|
10 | 10 | use Documents81\Card;
|
11 | 11 | use Documents81\Suit;
|
| 12 | +use Error; |
12 | 13 | use MongoDB\BSON\ObjectId;
|
13 | 14 | use ValueError;
|
14 | 15 |
|
@@ -49,6 +50,53 @@ public function testLoadingInvalidBackingValueThrowsError(): void
|
49 | 50 | $this->dm->getRepository(Card::class)->findOneBy([]);
|
50 | 51 | }
|
51 | 52 |
|
| 53 | + public function testQueryWithMappedField(): void |
| 54 | + { |
| 55 | + $qb = $this->dm->createQueryBuilder(Card::class) |
| 56 | + ->field('suit')->equals(Suit::Spades) |
| 57 | + ->field('nullableSuit')->in([Suit::Hearts, Suit::Diamonds]); |
| 58 | + |
| 59 | + $this->assertSame([ |
| 60 | + 'suit' => 'S', |
| 61 | + 'nullableSuit' => [ |
| 62 | + '$in' => ['H', 'D'], |
| 63 | + ], |
| 64 | + ], $qb->getQuery()->debug('query')); |
| 65 | + } |
| 66 | + |
| 67 | + public function testQueryWithMappedFieldAndEnumValue(): void |
| 68 | + { |
| 69 | + $qb = $this->dm->createQueryBuilder(Card::class) |
| 70 | + ->field('suit')->equals('S') // Suit::Spades->value |
| 71 | + ->field('nullableSuit')->in(['H', 'D']); |
| 72 | + |
| 73 | + $this->assertSame([ |
| 74 | + 'suit' => 'S', |
| 75 | + 'nullableSuit' => [ |
| 76 | + '$in' => ['H', 'D'], |
| 77 | + ], |
| 78 | + ], $qb->getQuery()->debug('query')); |
| 79 | + } |
| 80 | + |
| 81 | + public function testQueryWithNotMappedField(): void |
| 82 | + { |
| 83 | + $qb = $this->dm->createQueryBuilder(Card::class) |
| 84 | + ->field('nonExisting')->equals(Suit::Clubs) |
| 85 | + ->field('nonExistingArray')->equals([Suit::Clubs, Suit::Hearts]); |
| 86 | + |
| 87 | + $this->assertSame(['nonExisting' => 'C', 'nonExistingArray' => ['C', 'H']], $qb->getQuery()->debug('query')); |
| 88 | + } |
| 89 | + |
| 90 | + public function testQueryWithMappedNonEnumFieldIsPassedToTypeDirectly(): void |
| 91 | + { |
| 92 | + $this->expectException(Error::class); |
| 93 | + $this->expectExceptionMessage(sprintf('Object of class %s could not be converted to string', Suit::class)); |
| 94 | + |
| 95 | + $qb = $this->dm->createQueryBuilder(Card::class)->field('_id')->equals(Suit::Clubs); |
| 96 | + |
| 97 | + $this->assertSame(['_id' => 'C'], $qb->getQuery()->debug('query')); |
| 98 | + } |
| 99 | + |
52 | 100 | protected function createMetadataDriverImpl(): MappingDriver
|
53 | 101 | {
|
54 | 102 | return AttributeDriver::create(__DIR__ . '/../../../Documents');
|
|
0 commit comments