Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
Add and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Aug 21, 2023
1 parent 4ec7a5f commit 40ee389
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 44 deletions.
49 changes: 18 additions & 31 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class Builder extends BaseBuilder
* @var array
*/
protected $conversion = [
'=' => 'eq',
'!=' => 'ne',
'<>' => 'ne',
'<' => 'lt',
Expand Down Expand Up @@ -1085,7 +1086,7 @@ protected function compileWhereBasic(array $where): array
$operator = $operator === 'regex' ? '=' : 'not';
}

if (! isset($operator) || $operator == '=') {
if (! isset($operator) || $operator === '=' || $operator === 'eq') {
$query = [$column => $value];
} else {
$query = [$column => ['$'.$operator => $value]];
Expand Down Expand Up @@ -1195,39 +1196,27 @@ protected function compileWhereDate(array $where): array
$startOfDay = new UTCDateTime(Carbon::parse($value)->startOfDay());
$endOfDay = new UTCDateTime(Carbon::parse($value)->endOfDay());

$operator = $this->conversion[$operator];

return match($operator) {
'=' => [
'eq', '=' => [
$column => [
'$gte' => $startOfDay,
'$lte' => $endOfDay,
],
],
'$ne' => [
'ne' => [
$column => [
'$gt' => $endOfDay,
'$lt' => $startOfDay,
],
],
'$lt' => [
$column => [
'$lt' => $startOfDay,
],
],
'$gt' => [
$column => [
'$gt' => $endOfDay,
],
],
'$lte' => [
'lt', 'gte' => [
$column => [
'$lte' => $endOfDay,
'$'.$operator => $startOfDay,
],
],
'$gte' => [
'gt', 'lte' => [
$column => [
'$gte' => $startOfDay,
'$'.$operator => $endOfDay,
],
],
};
Expand All @@ -1241,14 +1230,13 @@ protected function compileWhereMonth(array $where): array
{
extract($where);

$operator = $operator === '=' ? '$eq' : $this->conversion[$operator];
$value = str_starts_with($value, '0') ? intval(str_replace('0', '', $value)) : $value;
$value = (int) ltrim($value, '0');

return [
'$expr' => [
$operator => [
'$'.$operator => [
[
'$month' => '$'.$column
'$month' => '$'.$column,
],
$value,
],
Expand All @@ -1264,14 +1252,13 @@ protected function compileWhereDay(array $where): array
{
extract($where);

$operator = $operator === '=' ? '$eq' : $this->conversion[$operator];
$value = str_starts_with($value, '0') ? intval(str_replace('0', '', $value)) : $value;
$value = (int) ltrim($value, '0');

return [
'$expr' => [
$operator => [
'$'.$operator => [
[
'$dayOfMonth' => '$'.$column
'$dayOfMonth' => '$'.$column,
],
$value,
],
Expand All @@ -1287,15 +1274,15 @@ protected function compileWhereYear(array $where): array
{
extract($where);

$operator = $operator === '=' ? '$eq' : $this->conversion[$operator];
$value = (int) $value;

return [
'$expr' => [
$operator => [
'$'.$operator => [
[
'$year' => '$'.$column
'$year' => '$'.$column,
],
$value
$value,
],
],
];
Expand Down
4 changes: 4 additions & 0 deletions tests/Models/Birthday.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ class Birthday extends Eloquent
protected $connection = 'mongodb';
protected $collection = 'birthday';
protected $fillable = ['name', 'birthday', 'time'];

protected $casts = [
'birthday' => 'datetime',
];
}
104 changes: 104 additions & 0 deletions tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,110 @@ function (Builder $builder) {
fn (Builder $builder) => $builder->where('name', 'not regex', '/^acme$/si'),
];

yield 'where date' => [
['find' => [['created_at' => [
'$gte' => new UTCDateTime(new DateTimeImmutable('2018-09-30 00:00:00.000 +00:00')),
'$lte' => new UTCDateTime(new DateTimeImmutable('2018-09-30 23:59:59.999 +00:00')),
]], []]],
fn (Builder $builder) => $builder->whereDate('created_at', '2018-09-30'),
];

yield 'where date DateTimeImmutable' => [
['find' => [['created_at' => [
'$gte' => new UTCDateTime(new DateTimeImmutable('2018-09-30 00:00:00.000 +00:00')),
'$lte' => new UTCDateTime(new DateTimeImmutable('2018-09-30 23:59:59.999 +00:00')),
]], []]],
fn (Builder $builder) => $builder->whereDate('created_at', '=', new DateTimeImmutable('2018-09-30 15:00:00 +02:00')),
];

yield 'where date <' => [
['find' => [['created_at' => [
'$lt' => new UTCDateTime(new DateTimeImmutable('2018-09-30 00:00:00.000 +00:00')),
]], []]],
fn (Builder $builder) => $builder->whereDate('created_at', '<', '2018-09-30'),
];

yield 'where date >=' => [
['find' => [['created_at' => [
'$gte' => new UTCDateTime(new DateTimeImmutable('2018-09-30 00:00:00.000 +00:00')),
]], []]],
fn (Builder $builder) => $builder->whereDate('created_at', '>=', '2018-09-30'),
];

yield 'where date >' => [
['find' => [['created_at' => [
'$gt' => new UTCDateTime(new DateTimeImmutable('2018-09-30 23:59:59.999 +00:00')),
]], []]],
fn (Builder $builder) => $builder->whereDate('created_at', '>', '2018-09-30'),
];

yield 'where date <=' => [
['find' => [['created_at' => [
'$lte' => new UTCDateTime(new DateTimeImmutable('2018-09-30 23:59:59.999 +00:00')),
]], []]],
fn (Builder $builder) => $builder->whereDate('created_at', '<=', '2018-09-30'),
];

yield 'where day' => [
['find' => [['$expr' => [
'$eq' => [
['$dayOfMonth' => '$created_at'],
5,
],
]], []]],
fn (Builder $builder) => $builder->whereDay('created_at', 5),
];

yield 'where day > string' => [
['find' => [['$expr' => [
'$gt' => [
['$dayOfMonth' => '$created_at'],
5,
],
]], []]],
fn (Builder $builder) => $builder->whereDay('created_at', '>', '05'),
];

yield 'where month' => [
['find' => [['$expr' => [
'$eq' => [
['$month' => '$created_at'],
10,
],
]], []]],
fn (Builder $builder) => $builder->whereMonth('created_at', 10),
];

yield 'where month > string' => [
['find' => [['$expr' => [
'$gt' => [
['$month' => '$created_at'],
5,
],
]], []]],
fn (Builder $builder) => $builder->whereMonth('created_at', '>', '05'),
];

yield 'where year' => [
['find' => [['$expr' => [
'$eq' => [
['$year' => '$created_at'],
2023,
],
]], []]],
fn (Builder $builder) => $builder->whereYear('created_at', 2023),
];

yield 'where year > string' => [
['find' => [['$expr' => [
'$gt' => [
['$year' => '$created_at'],
2023,
],
]], []]],
fn (Builder $builder) => $builder->whereYear('created_at', '>', '2023'),
];

/** @see DatabaseQueryBuilderTest::testBasicSelectDistinct */
yield 'distinct' => [
['distinct' => ['foo', [], []]],
Expand Down
51 changes: 38 additions & 13 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Jenssegers\Mongodb\Tests;

use DateTimeImmutable;
use Jenssegers\Mongodb\Tests\Models\Birthday;
use Jenssegers\Mongodb\Tests\Models\Scoped;
use Jenssegers\Mongodb\Tests\Models\User;
Expand All @@ -24,12 +25,12 @@ public function setUp(): void
User::create(['name' => 'Tommy Toe', 'age' => 33, 'title' => 'user']);
User::create(['name' => 'Yvonne Yoe', 'age' => 35, 'title' => 'admin']);
User::create(['name' => 'Error', 'age' => null, 'title' => null]);
Birthday::create(['name' => 'Mark Moe', 'birthday' => '2020-04-10', 'day' => '10', 'month' => '04', 'year' => '2020', 'time' => '10:53:11']);
Birthday::create(['name' => 'Jane Doe', 'birthday' => '2021-05-12', 'day' => '12', 'month' => '05', 'year' => '2021', 'time' => '10:53:12']);
Birthday::create(['name' => 'Harry Hoe', 'birthday' => '2021-05-11', 'day' => '11', 'month' => '05', 'year' => '2021', 'time' => '10:53:13']);
Birthday::create(['name' => 'Robert Doe', 'birthday' => '2021-05-12', 'day' => '12', 'month' => '05', 'year' => '2021', 'time' => '10:53:14']);
Birthday::create(['name' => 'Mark Moe', 'birthday' => '2021-05-12', 'day' => '12', 'month' => '05', 'year' => '2021', 'time' => '10:53:15']);
Birthday::create(['name' => 'Mark Moe', 'birthday' => '2022-05-12', 'day' => '12', 'month' => '05', 'year' => '2022', 'time' => '10:53:16']);
Birthday::create(['name' => 'Mark Moe', 'birthday' => new DateTimeImmutable('2020-04-10 10:53:11'), 'time' => '10:53:11']);
Birthday::create(['name' => 'Jane Doe', 'birthday' => new DateTimeImmutable('2021-05-12 10:53:12'), 'time' => '10:53:12']);
Birthday::create(['name' => 'Harry Hoe', 'birthday' => new DateTimeImmutable('2021-05-11 10:53:13'), 'time' => '10:53:13']);
Birthday::create(['name' => 'Robert Doe', 'birthday' => new DateTimeImmutable('2021-05-12 10:53:14'), 'time' => '10:53:14']);
Birthday::create(['name' => 'Mark Moe', 'birthday' => new DateTimeImmutable('2021-05-12 10:53:15'), 'time' => '10:53:15']);
Birthday::create(['name' => 'Mark Moe', 'birthday' => new DateTimeImmutable('2022-05-12 10:53:16'), 'time' => '10:53:16']);
}

public function tearDown(): void
Expand Down Expand Up @@ -204,36 +205,60 @@ public function testWhereDate(): void

$birthdayCount = Birthday::whereDate('birthday', '2021-05-11')->get();
$this->assertCount(1, $birthdayCount);

$birthdayCount = Birthday::whereDate('birthday', '>', '2021-05-11')->get();
$this->assertCount(4, $birthdayCount);

$birthdayCount = Birthday::whereDate('birthday', '>=', '2021-05-11')->get();
$this->assertCount(5, $birthdayCount);

$birthdayCount = Birthday::whereDate('birthday', '<', '2021-05-11')->get();
$this->assertCount(1, $birthdayCount);

$birthdayCount = Birthday::whereDate('birthday', '<=', '2021-05-11')->get();
$this->assertCount(2, $birthdayCount);
}

public function testWhereDay(): void
{
$day = Birthday::whereDay('day', '12')->get();
$day = Birthday::whereDay('birthday', '12')->get();
$this->assertCount(4, $day);

$day = Birthday::whereDay('day', '11')->get();
$day = Birthday::whereDay('birthday', '11')->get();
$this->assertCount(1, $day);
}

public function testWhereMonth(): void
{
$month = Birthday::whereMonth('month', '04')->get();
$month = Birthday::whereMonth('birthday', '04')->get();
$this->assertCount(1, $month);

$month = Birthday::whereMonth('month', '05')->get();
$month = Birthday::whereMonth('birthday', '05')->get();
$this->assertCount(5, $month);

$month = Birthday::whereMonth('birthday', '>=', '5')->get();
$this->assertCount(5, $month);

$month = Birthday::whereMonth('birthday', '<', '10')->get();
$this->assertCount(6, $month);

$month = Birthday::whereMonth('birthday', '<>', '5')->get();
$this->assertCount(1, $month);
}

public function testWhereYear(): void
{
$year = Birthday::whereYear('year', '2021')->get();
$year = Birthday::whereYear('birthday', '2021')->get();
$this->assertCount(4, $year);

$year = Birthday::whereYear('year', '2022')->get();
$year = Birthday::whereYear('birthday', '2022')->get();
$this->assertCount(1, $year);

$year = Birthday::whereYear('year', '<', '2021')->get();
$year = Birthday::whereYear('birthday', '<', '2021')->get();
$this->assertCount(1, $year);

$year = Birthday::whereYear('birthday', '<>', '2021')->get();
$this->assertCount(2, $year);
}

public function testWhereTime(): void
Expand Down

0 comments on commit 40ee389

Please sign in to comment.