|
14 | 14 | use EventEngine\DocumentStore\Filter\AnyOfDocIdFilter;
|
15 | 15 | use EventEngine\DocumentStore\Filter\AnyOfFilter;
|
16 | 16 | use EventEngine\DocumentStore\Filter\DocIdFilter;
|
| 17 | +use EventEngine\DocumentStore\Filter\InArrayFilter; |
17 | 18 | use EventEngine\DocumentStore\Filter\NotFilter;
|
18 | 19 | use PHPUnit\Framework\TestCase;
|
19 | 20 | use EventEngine\DocumentStore\FieldIndex;
|
@@ -305,6 +306,96 @@ public function it_handles_not_any_of_id_filter()
|
305 | 306 | $this->assertEquals(['bat'], $vals);
|
306 | 307 | }
|
307 | 308 |
|
| 309 | + /** |
| 310 | + * @test |
| 311 | + */ |
| 312 | + public function it_handles_in_array_filter() |
| 313 | + { |
| 314 | + $collectionName = 'test_in_array_filter'; |
| 315 | + $this->documentStore->addCollection($collectionName); |
| 316 | + |
| 317 | + $firstDocId = Uuid::uuid4()->toString(); |
| 318 | + $secondDocId = Uuid::uuid4()->toString(); |
| 319 | + $thirdDocId = Uuid::uuid4()->toString(); |
| 320 | + |
| 321 | + $this->documentStore->addDoc($collectionName, $firstDocId, ['foo' => ['bar' => ['tag1', 'tag2'], 'ref' => $firstDocId]]); |
| 322 | + $this->documentStore->addDoc($collectionName, $secondDocId, ['foo' => ['bar' => ['tag2', 'tag3'], 'ref' => $secondDocId]]); |
| 323 | + $this->documentStore->addDoc($collectionName, $thirdDocId, ['foo' => ['bar' => ['tag3', 'tag4'], 'ref' => $thirdDocId]]); |
| 324 | + |
| 325 | + $filteredDocs = \iterator_to_array($this->documentStore->filterDocs( |
| 326 | + $collectionName, |
| 327 | + new InArrayFilter('foo.bar', 'tag3') |
| 328 | + )); |
| 329 | + |
| 330 | + $this->assertCount(2, $filteredDocs); |
| 331 | + |
| 332 | + $refs = array_map(function (array $doc) { |
| 333 | + return $doc['foo']['ref']; |
| 334 | + }, $filteredDocs); |
| 335 | + |
| 336 | + $this->assertEquals([$secondDocId, $thirdDocId], $refs); |
| 337 | + } |
| 338 | + |
| 339 | + /** |
| 340 | + * @test |
| 341 | + */ |
| 342 | + public function it_handles_not_in_array_filter() |
| 343 | + { |
| 344 | + $collectionName = 'test_not_in_array_filter'; |
| 345 | + $this->documentStore->addCollection($collectionName); |
| 346 | + |
| 347 | + $firstDocId = Uuid::uuid4()->toString(); |
| 348 | + $secondDocId = Uuid::uuid4()->toString(); |
| 349 | + $thirdDocId = Uuid::uuid4()->toString(); |
| 350 | + |
| 351 | + $this->documentStore->addDoc($collectionName, $firstDocId, ['foo' => ['bar' => ['tag1', 'tag2'], 'ref' => $firstDocId]]); |
| 352 | + $this->documentStore->addDoc($collectionName, $secondDocId, ['foo' => ['bar' => ['tag2', 'tag3'], 'ref' => $secondDocId]]); |
| 353 | + $this->documentStore->addDoc($collectionName, $thirdDocId, ['foo' => ['bar' => ['tag3', 'tag4'], 'ref' => $thirdDocId]]); |
| 354 | + |
| 355 | + $filteredDocs = \iterator_to_array($this->documentStore->filterDocs( |
| 356 | + $collectionName, |
| 357 | + new NotFilter(new InArrayFilter('foo.bar', 'tag3')) |
| 358 | + )); |
| 359 | + |
| 360 | + $this->assertCount(1, $filteredDocs); |
| 361 | + |
| 362 | + $refs = array_map(function (array $doc) { |
| 363 | + return $doc['foo']['ref']; |
| 364 | + }, $filteredDocs); |
| 365 | + |
| 366 | + $this->assertEquals([$firstDocId], $refs); |
| 367 | + } |
| 368 | + |
| 369 | + /** |
| 370 | + * @test |
| 371 | + */ |
| 372 | + public function it_handles_in_array_filter_with_object_items() |
| 373 | + { |
| 374 | + $collectionName = 'test_in_array_with_object_filter'; |
| 375 | + $this->documentStore->addCollection($collectionName); |
| 376 | + |
| 377 | + $firstDocId = Uuid::uuid4()->toString(); |
| 378 | + $secondDocId = Uuid::uuid4()->toString(); |
| 379 | + $thirdDocId = Uuid::uuid4()->toString(); |
| 380 | + |
| 381 | + $this->documentStore->addDoc($collectionName, $firstDocId, ['foo' => ['bar' => [['tag' => 'tag1', 'other' => 'data'], ['tag' => 'tag2']], 'ref' => $firstDocId]]); |
| 382 | + $this->documentStore->addDoc($collectionName, $secondDocId, ['foo' => ['bar' => [['tag' => 'tag2', 'other' => 'data'], ['tag' => 'tag3']], 'ref' => $secondDocId]]); |
| 383 | + $this->documentStore->addDoc($collectionName, $thirdDocId, ['foo' => ['bar' => [['tag' => 'tag3', 'other' => 'data'], ['tag' => 'tag4']], 'ref' => $thirdDocId]]); |
| 384 | + |
| 385 | + $filteredDocs = \iterator_to_array($this->documentStore->filterDocs( |
| 386 | + $collectionName, |
| 387 | + new InArrayFilter('foo.bar', ['tag' => 'tag3']) |
| 388 | + )); |
| 389 | + |
| 390 | + $this->assertCount(2, $filteredDocs); |
| 391 | + |
| 392 | + $refs = array_map(function (array $doc) { |
| 393 | + return $doc['foo']['ref']; |
| 394 | + }, $filteredDocs); |
| 395 | + |
| 396 | + $this->assertEquals([$secondDocId, $thirdDocId], $refs); |
| 397 | + } |
| 398 | + |
308 | 399 | private function getIndexes(string $collectionName): array
|
309 | 400 | {
|
310 | 401 | return TestUtil::getIndexes($this->connection, self::TABLE_PREFIX.$collectionName);
|
|
0 commit comments