Skip to content

Commit 3982f17

Browse files
authored
Avoid time-sensible tests from failing randomly (#2674)
Freeze the clock for tests that use now() function.
1 parent 1eda4de commit 3982f17

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

tests/Casts/DatetimeTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ protected function setUp(): void
1818
{
1919
parent::setUp();
2020

21+
Carbon::setTestNow(now());
2122
Casting::truncate();
2223
}
2324

@@ -55,7 +56,7 @@ public function testDatetimeAsString(): void
5556

5657
public function testDatetimeWithCustomFormat(): void
5758
{
58-
$model = Casting::query()->create(['datetimeWithFormatField' => new DateTime()]);
59+
$model = Casting::query()->create(['datetimeWithFormatField' => DateTime::createFromInterface(now())]);
5960

6061
self::assertInstanceOf(Carbon::class, $model->datetimeWithFormatField);
6162
self::assertEquals(now()->format('j.n.Y H:i'), (string) $model->datetimeWithFormatField);
@@ -68,7 +69,7 @@ public function testDatetimeWithCustomFormat(): void
6869

6970
public function testImmutableDatetime(): void
7071
{
71-
$model = Casting::query()->create(['immutableDatetimeField' => new DateTime()]);
72+
$model = Casting::query()->create(['immutableDatetimeField' => DateTime::createFromInterface(now())]);
7273

7374
self::assertInstanceOf(CarbonImmutable::class, $model->immutableDatetimeField);
7475
self::assertEquals(now()->format('Y-m-d H:i:s'), (string) $model->immutableDatetimeField);
@@ -89,7 +90,7 @@ public function testImmutableDatetime(): void
8990

9091
public function testImmutableDatetimeWithCustomFormat(): void
9192
{
92-
$model = Casting::query()->create(['immutableDatetimeWithFormatField' => new DateTime()]);
93+
$model = Casting::query()->create(['immutableDatetimeWithFormatField' => DateTime::createFromInterface(now())]);
9394

9495
self::assertInstanceOf(CarbonImmutable::class, $model->immutableDatetimeWithFormatField);
9596
self::assertEquals(now()->format('j.n.Y H:i'), (string) $model->immutableDatetimeWithFormatField);

0 commit comments

Comments
 (0)