Skip to content

Commit 811cb3d

Browse files
authored
Merge pull request #1870 from CurosMJ/fix-fresh-timestamp
Use Carbon::now() for fresh timestamps so that Carbon's time mocking works
2 parents 87880c0 + 9a5b1bd commit 811cb3d

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/Jenssegers/Mongodb/Eloquent/Model.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function getDateFormat()
118118
*/
119119
public function freshTimestamp()
120120
{
121-
return new UTCDateTime(time() * 1000);
121+
return new UTCDateTime(Carbon::now());
122122
}
123123

124124
/**

tests/ModelTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,16 @@ public function testDates(): void
434434
$this->assertEquals((string) $user->getAttribute('entry.date')->format('Y-m-d H:i:s'), $data['entry']['date']);
435435
}
436436

437+
public function testCarbonDateMockingWorks()
438+
{
439+
$fakeDate = \Carbon\Carbon::createFromDate(2000, 01, 01);
440+
441+
Carbon::setTestNow($fakeDate);
442+
$item = Item::create(['name' => 'sword']);
443+
444+
$this->assertLessThan(1, $fakeDate->diffInSeconds($item->created_at));
445+
}
446+
437447
public function testIdAttribute(): void
438448
{
439449
/** @var User $user */

0 commit comments

Comments
 (0)