Skip to content

Commit 6f6ec90

Browse files
authored
Merge pull request #19 from rawveg/nullable_dates
Added null check for dates, along with tests
2 parents 88dc574 + c5b95a6 commit 6f6ec90

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

src/Filters/DateFilter.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,18 @@ class DateFilter implements ModifiesQueries
1010
public function modifyQuery($query, SearchTerm $search)
1111
{
1212
if ($search->operator() == 'eq') {
13-
$query->where($search->column(), $search->term());
13+
if ($search->term() == 'null') {
14+
$query->whereNull($search->column());
15+
} else {
16+
$query->where($search->column(), $search->term());
17+
}
1418
}
1519
if ($search->operator() == 'neq') {
16-
$query->where($search->column(), '!=', $search->term());
20+
if ($search->term() == 'null') {
21+
$query->whereNotNull($search->column());
22+
} else {
23+
$query->where($search->column(), '!=', $search->term());
24+
}
1725
}
1826
if ($search->operator() == 'in') {
1927
$query->whereIn($search->column(), explode(',', $search->term()));

tests/Filters/DateFilterTest.php

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function can_filter_by_eq()
1818

1919
$this->assertEquals(
2020
"select * from `pets` where `created_at` = ?",
21-
$query->toSql(),
21+
$query->toSql()
2222
);
2323
}
2424

@@ -32,7 +32,7 @@ public function can_filter_by_neq()
3232

3333
$this->assertEquals(
3434
"select * from `pets` where `created_at` != ?",
35-
$query->toSql(),
35+
$query->toSql()
3636
);
3737
}
3838

@@ -51,7 +51,7 @@ public function can_filter_by_in()
5151

5252
$this->assertEquals(
5353
"select * from `pets` where `created_at` in (?, ?, ?)",
54-
$query->toSql(),
54+
$query->toSql()
5555
);
5656
}
5757

@@ -70,7 +70,7 @@ public function can_filter_by_nin()
7070

7171
$this->assertEquals(
7272
"select * from `pets` where `created_at` not in (?, ?, ?)",
73-
$query->toSql(),
73+
$query->toSql()
7474
);
7575
}
7676

@@ -84,7 +84,7 @@ public function can_filter_by_lt()
8484

8585
$this->assertEquals(
8686
"select * from `pets` where `created_at` < ?",
87-
$query->toSql(),
87+
$query->toSql()
8888
);
8989
}
9090

@@ -98,7 +98,35 @@ public function can_filter_by_gt()
9898

9999
$this->assertEquals(
100100
"select * from `pets` where `created_at` > ?",
101-
$query->toSql(),
101+
$query->toSql()
102+
);
103+
}
104+
105+
/**
106+
* @test
107+
*/
108+
public function can_filter_null_by_eq()
109+
{
110+
$query = Pet::query()->getQuery();
111+
(new DateFilter)->modifyQuery($query, $this->searchTerm('eq', 'null'));
112+
113+
$this->assertEquals(
114+
"select * from `pets` where `created_at` is null",
115+
$query->toSql()
116+
);
117+
}
118+
119+
/**
120+
* @test
121+
*/
122+
public function can_filter_null_by_neq()
123+
{
124+
$query = Pet::query()->getQuery();
125+
(new DateFilter)->modifyQuery($query, $this->searchTerm('neq', 'null'));
126+
127+
$this->assertEquals(
128+
"select * from `pets` where `created_at` is not null",
129+
$query->toSql()
102130
);
103131
}
104132

tests/Filters/EnumFilterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function can_search_if_passed_a_valid_term()
4343

4444
$this->assertEquals(
4545
'select * from "pets" where "letter" = ?',
46-
$query->toSql(),
46+
$query->toSql()
4747
);
4848
}
4949
}

0 commit comments

Comments
 (0)