Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow empty array provided to IN, IN ALL or NOT IN operators #48

Merged
merged 1 commit into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Bridge/Repository/AbstractEntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ protected function handleRegularCriteria(
true
)
) {
if (count($value) === 0) {
$value[] = md5((string) time());
}

$parameters = array_map(
static fn (int $number) => "{$snakeField}_{$number}",
range(0, count($value) - 1),
Expand Down
9 changes: 9 additions & 0 deletions test/Test/Bridge/Repository/PostRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,15 @@ public function testOperatorNotIn(): void
self::assertEquals([11], array_column($posts, 'id'));
}

public function testOperatorNotInEmptyArray(): void
{
$posts = $this->repository->findBy(['post_status' => new Operand([], Operand::OPERATOR_NOT_IN)]);
self::assertIsArray($posts);
self::assertCount(4, $posts);
self::assertContainsOnlyInstancesOf(Post::class, $posts);
self::assertEquals([12, 11, 1, 10], array_column($posts, 'id'));
}

public function testOperatorNotInWithMagicMethod(): void
{
$posts = $this->repository->findByPostStatus(new Operand(['draft', 'private'], Operand::OPERATOR_NOT_IN));
Expand Down
20 changes: 20 additions & 0 deletions test/Test/Bridge/Repository/ProductRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,15 @@ public function testOperatorInWithAssociativeArray(): void
self::assertEquals([17, 27], array_column($products, 'id'));
}

public function testOperatorInWithEmptyArray(): void
{
$products = $this->repository->findBySku(
new Operand([], Operand::OPERATOR_IN),
);
self::assertIsArray($products);
self::assertCount(0, $products);
}

public function testTermRelationshipConditionWithTaxonomyOnly(): void
{
$products = $this->repository->findBy([
Expand Down Expand Up @@ -343,6 +352,17 @@ public function testTermRelationshipConditionWithInAllOperator(): void
self::assertEquals([20, 21], array_column($products, 'id'));
}

public function testTermRelationshipConditionWithInAllOperatorEmptyArray(): void
{
$products = $this->repository->findBy([
new TermRelationshipCondition([
'slug' => new Operand([], Operand::OPERATOR_IN_ALL),
]),
]);

self::assertCount(0, $products);
}

public function testTermRelationshipConditionWithInAllOperatorNotReturningResults(): void
{
$products = $this->repository->findBy([
Expand Down