Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check dates against DateTimeInterface instead of DateTime #2239

Merged
merged 3 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Jenssegers/Mongodb/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Jenssegers\Mongodb\Eloquent;

use DateTime;
use DateTimeInterface;
use Illuminate\Contracts\Queue\QueueableCollection;
use Illuminate\Contracts\Queue\QueueableEntity;
use Illuminate\Database\Eloquent\Model as BaseModel;
Expand Down Expand Up @@ -85,7 +85,7 @@ public function fromDateTime($value)
}

// Let Eloquent convert the value to a DateTime instance.
if (!$value instanceof DateTime) {
if (!$value instanceof DateTimeInterface) {
$value = parent::asDateTime($value);
}

Expand Down
8 changes: 4 additions & 4 deletions src/Jenssegers/Mongodb/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Jenssegers\Mongodb\Query;

use Closure;
use DateTime;
use DateTimeInterface;
use Illuminate\Database\Query\Builder as BaseBuilder;
use Illuminate\Database\Query\Expression;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -929,18 +929,18 @@ protected function compileWheres()
if (isset($where['value'])) {
if (is_array($where['value'])) {
array_walk_recursive($where['value'], function (&$item, $key) {
if ($item instanceof DateTime) {
if ($item instanceof DateTimeInterface) {
$item = new UTCDateTime($item->format('Uv'));
}
});
} else {
if ($where['value'] instanceof DateTime) {
if ($where['value'] instanceof DateTimeInterface) {
$where['value'] = new UTCDateTime($where['value']->format('Uv'));
}
}
} elseif (isset($where['values'])) {
array_walk_recursive($where['values'], function (&$item, $key) {
if ($item instanceof DateTime) {
if ($item instanceof DateTimeInterface) {
$item = new UTCDateTime($item->format('Uv'));
}
});
Expand Down
3 changes: 3 additions & 0 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,9 @@ public function testDates(): void
$user->setAttribute('birthday', new DateTime('1965-08-08 04.08.37.324'));
$this->assertInstanceOf(Carbon::class, $user->birthday);

$user->setAttribute('birthday', new DateTimeImmutable('1965-08-08 04.08.37.324'));
$this->assertInstanceOf(Carbon::class, $user->birthday);

//Test with create and array property
$user = User::create(['name' => 'Jane Doe', 'entry' => ['date' => time()]]);
$this->assertInstanceOf(Carbon::class, $user->getAttribute('entry.date'));
Expand Down
27 changes: 27 additions & 0 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,33 @@ public function testDates()
$this->assertCount(2, $users);
}

public function testImmutableDates()
{
DB::collection('users')->insert([
['name' => 'John Doe', 'birthday' => new UTCDateTime(Date::parse("1980-01-01 00:00:00")->format('Uv'))],
['name' => 'Robert Roe', 'birthday' => new UTCDateTime(Date::parse("1982-01-01 00:00:00")->format('Uv'))],
]);

$users = DB::collection('users')->where('birthday', '=', new DateTimeImmutable("1980-01-01 00:00:00"))->get();
$this->assertCount(1, $users);

$users = DB::collection('users')->where('birthday', new DateTimeImmutable("1980-01-01 00:00:00"))->get();
$this->assertCount(1, $users);

$users = DB::collection('users')->whereIn('birthday', [
new DateTimeImmutable("1980-01-01 00:00:00"),
new DateTimeImmutable("1982-01-01 00:00:00")
])->get();
$this->assertCount(2, $users);

$users = DB::collection('users')->whereBetween('birthday', [
new DateTimeImmutable("1979-01-01 00:00:00"),
new DateTimeImmutable("1983-01-01 00:00:00")
])->get();

$this->assertCount(2, $users);
}

public function testOperators()
{
DB::collection('users')->insert([
Expand Down