Skip to content

Commit 11b9c4a

Browse files
author
codeliner
committed
Fix InArray filter for object items
1 parent b4c7b04 commit 11b9c4a

File tree

4 files changed

+94
-2
lines changed

4 files changed

+94
-2
lines changed

src/PostgresDocumentStore.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ private function filterToWhereClause(Filter $filter, $argsCount = 0): array
621621
case DocumentStore\Filter\InArrayFilter::class:
622622
/** @var DocumentStore\Filter\InArrayFilter $filter */
623623
$prop = $this->propToJsonPath($filter->prop());
624-
return ["$prop @> :a$argsCount", ["a$argsCount" => $this->prepareVal($filter->val(), $filter->prop())], ++$argsCount];
624+
return ["$prop @> :a$argsCount", ["a$argsCount" => '[' . $this->prepareVal($filter->val(), $filter->prop()) . ']'], ++$argsCount];
625625
case DocumentStore\Filter\ExistsFilter::class:
626626
/** @var DocumentStore\Filter\ExistsFilter $filter */
627627
$prop = $this->propToJsonPath($filter->prop());

tests/PostgresDocumentStoreTest.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use EventEngine\DocumentStore\Filter\AnyOfDocIdFilter;
1515
use EventEngine\DocumentStore\Filter\AnyOfFilter;
1616
use EventEngine\DocumentStore\Filter\DocIdFilter;
17+
use EventEngine\DocumentStore\Filter\InArrayFilter;
1718
use EventEngine\DocumentStore\Filter\NotFilter;
1819
use PHPUnit\Framework\TestCase;
1920
use EventEngine\DocumentStore\FieldIndex;
@@ -305,6 +306,96 @@ public function it_handles_not_any_of_id_filter()
305306
$this->assertEquals(['bat'], $vals);
306307
}
307308

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+
308399
private function getIndexes(string $collectionName): array
309400
{
310401
return TestUtil::getIndexes($this->connection, self::TABLE_PREFIX.$collectionName);

tests/SchemedPostgresDocumentStoreTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ protected function setUp(): void
4343
$this->documentStore = new PostgresDocumentStore($this->connection, self::TABLE_PREFIX);
4444
}
4545

46-
public function tearDown(): void
46+
protected function tearDown(): void
4747
{
4848
TestUtil::tearDownDatabase();
4949
}

tests/TestUtil.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public static function tearDownDatabase(): void
8686
$connection = self::getConnection();
8787
$statement = $connection->prepare('SELECT table_name FROM information_schema.tables WHERE table_schema = \'public\';');
8888
$connection->exec('DROP SCHEMA IF EXISTS prooph CASCADE');
89+
$connection->exec('DROP SCHEMA IF EXISTS test CASCADE');
8990

9091
$statement->execute();
9192
$tables = $statement->fetchAll(PDO::FETCH_COLUMN);

0 commit comments

Comments
 (0)