Skip to content
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
9 changes: 9 additions & 0 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,15 @@ public function whereNotContains(string $field, string $value): static
*/
public function whereIn(string $field, array $values): static
{
if (empty($values)) {
// If the array of values is empty, we will
// add an empty OR filter to the query to
// ensure that no results are returned.
$this->rawFilter('(|)');

return $this;
}

return $this->orFilter(function (Builder $query) use ($field, $values) {
foreach ($values as $value) {
$query->whereEquals($field, $value);
Expand Down
11 changes: 11 additions & 0 deletions tests/Integration/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,15 @@ public function test_it_cannot_override_limit_when_chunking()

$this->assertEquals(2, $pages);
}

public function test_it_returns_no_results_with_empty_where_in_array()
{
$user = $this->makeUser($this->ou);

$user->save();

$this->assertCount(1, User::get());
$this->assertCount(1, User::whereIn('cn', [$user->getFirstAttribute('cn')])->get());
$this->assertEmpty(User::whereIn('cn', [])->get());
}
}
9 changes: 9 additions & 0 deletions tests/Unit/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,15 @@ public function test_built_where_in()
$this->assertEquals('(|(name=john)(name=mary)(name=sue))', $b->getUnescapedQuery());
}

public function test_built_where_in_with_empty_array()
{
$b = $this->newBuilder();

$b->whereIn('name', []);

$this->assertEquals('(|)', $b->getUnescapedQuery());
}

public function test_built_where_approximately_equals()
{
$b = $this->newBuilder();
Expand Down