Skip to content

Commit 16a50c6

Browse files
committed
Fix whereDate
1 parent 560984e commit 16a50c6

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
@@ -1195,9 +1195,17 @@ protected function compileWhereDate(array $where): array
11951195
],
11961196
],
11971197
'ne' => [
1198-
$column => [
1199-
'$gt' => $endOfDay,
1200-
'$lt' => $startOfDay,
1198+
'$or' => [
1199+
[
1200+
$column => [
1201+
'$lt' => $startOfDay,
1202+
],
1203+
],
1204+
[
1205+
$column => [
1206+
'$gt' => $endOfDay,
1207+
],
1208+
],
12011209
],
12021210
],
12031211
'lt', 'gte' => [

tests/Query/BuilderTest.php

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

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