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

[BUG] - empty whereIn array results in condition ignored #265

Merged
merged 3 commits into from
Mar 10, 2021
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
10 changes: 7 additions & 3 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,13 @@ public function whereBetween($field, array $values): self
*/
public function whereIn($field, array $values): self
{
$wheres = array_map(function ($value) use ($field) {
return "$field={$this->transform($value)}";
}, array_values($values));
if(! empty($values)) {
$wheres = array_map(function ($value) use ($field) {
return "$field={$this->transform($value)}";
}, array_values($values));
} else {
$wheres = ['0 = 1'];
}

$this->wheres[] = $wheres;

Expand Down
11 changes: 11 additions & 0 deletions tests/Features/WhereQueriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ public function testWhereIn(): void
User::search('foo')->whereIn('id', [1, 2, 3, 4])->get();
}

public function testWhereInEmptyArray(): void
{
$this->mockIndex(User::class)->shouldReceive('search')->once()->with('foo', [
'numericFilters' => [
['0 = 1'],
],
])->andReturn(['hits' => []]);

User::search('foo')->whereIn('id', [])->get();
}

public function testMultipleWheres(): void
{
$this->mockIndex(User::class)->shouldReceive('search')->once()->with('foo', [
Expand Down