Skip to content

Commit f6ac96d

Browse files
committed
Fix whereDate
1 parent ff58a07 commit f6ac96d

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

src/Query/Builder.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,9 +1204,17 @@ protected function compileWhereDate(array $where): array
12041204
],
12051205
],
12061206
'ne' => [
1207-
$column => [
1208-
'$gt' => $endOfDay,
1209-
'$lt' => $startOfDay,
1207+
'$or' => [
1208+
[
1209+
$column => [
1210+
'$lt' => $startOfDay,
1211+
],
1212+
],
1213+
[
1214+
$column => [
1215+
'$gt' => $endOfDay,
1216+
],
1217+
],
12101218
],
12111219
],
12121220
'lt', 'gte' => [

tests/Query/BuilderTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,14 @@ function (Builder $builder) {
661661
fn (Builder $builder) => $builder->whereDate('created_at', '=', new DateTimeImmutable('2018-09-30 15:00:00 +02:00')),
662662
];
663663

664+
yield 'where date !=' => [
665+
['find' => [['$or' => [
666+
['created_at' => ['$lt' => new UTCDateTime(new DateTimeImmutable('2018-09-30 00:00:00.000 +00:00'))]],
667+
['created_at' => ['$gt' => new UTCDateTime(new DateTimeImmutable('2018-09-30 23:59:59.999 +00:00'))]],
668+
]], []]],
669+
fn (Builder $builder) => $builder->whereDate('created_at', '!=', '2018-09-30'),
670+
];
671+
664672
yield 'where date <' => [
665673
['find' => [['created_at' => [
666674
'$lt' => new UTCDateTime(new DateTimeImmutable('2018-09-30 00:00:00.000 +00:00')),

tests/QueryTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function setUp(): void
3131
Birthday::create(['name' => 'Robert Doe', 'birthday' => new DateTimeImmutable('2021-05-12 10:53:14'), 'time' => '10:53:14']);
3232
Birthday::create(['name' => 'Mark Moe', 'birthday' => new DateTimeImmutable('2021-05-12 10:53:15'), 'time' => '10:53:15']);
3333
Birthday::create(['name' => 'Mark Moe', 'birthday' => new DateTimeImmutable('2022-05-12 10:53:16'), 'time' => '10:53:16']);
34+
Birthday::create(['name' => 'Boo']);
3435
}
3536

3637
public function tearDown(): void
@@ -217,6 +218,9 @@ public function testWhereDate(): void
217218

218219
$birthdayCount = Birthday::whereDate('birthday', '<=', '2021-05-11')->get();
219220
$this->assertCount(2, $birthdayCount);
221+
222+
$birthdayCount = Birthday::whereDate('birthday', '<>', '2021-05-11')->get();
223+
$this->assertCount(5, $birthdayCount);
220224
}
221225

222226
public function testWhereDay(): void
@@ -240,10 +244,10 @@ public function testWhereMonth(): void
240244
$this->assertCount(5, $month);
241245

242246
$month = Birthday::whereMonth('birthday', '<', '10')->get();
243-
$this->assertCount(6, $month);
247+
$this->assertCount(7, $month);
244248

245249
$month = Birthday::whereMonth('birthday', '<>', '5')->get();
246-
$this->assertCount(1, $month);
250+
$this->assertCount(2, $month);
247251
}
248252

249253
public function testWhereYear(): void
@@ -255,10 +259,10 @@ public function testWhereYear(): void
255259
$this->assertCount(1, $year);
256260

257261
$year = Birthday::whereYear('birthday', '<', '2021')->get();
258-
$this->assertCount(1, $year);
262+
$this->assertCount(2, $year);
259263

260264
$year = Birthday::whereYear('birthday', '<>', '2021')->get();
261-
$this->assertCount(2, $year);
265+
$this->assertCount(3, $year);
262266
}
263267

264268
public function testWhereTime(): void

0 commit comments

Comments
 (0)