trait InteractsWithTime

Time travel helpers for tests.

Works in both unit tests (plain PHPUnit TestCase) and feature tests (ApplicationTestCase). Every helper sets Carbon's test "now", so it affects anything that creates dates through DateFactory or Carbon.

The real clock is restored automatically after each test.

Examples: $this->freezeTime(); $this->travelTo('2026-03-15 14:30:00'); $this->travel(5)->minutes(); $this->travel(-2)->days(); $this->travelBack();

Methods

mixed
freezeTime(callable|null $callback = null)

Freezes time at the current moment.

mixed
freezeSecond(callable|null $callback = null)

Freezes time at the start of the current second, dropping microseconds.

mixed
travelTo(DateTimeInterface|string|int $date, callable|null $callback = null)

Moves time to a specific moment.

travel(int $value)

Moves time relative to the current (possibly already frozen) time.

CarbonInterface
travelBack()

Restores the real clock.

void
restoreRealTime()

Restores the real clock after every test so frozen time never leaks into the next test.

Details

at line 38
protected mixed freezeTime(callable|null $callback = null)

Freezes time at the current moment.

Without a callback, time stays frozen until travelBack() or the end of the test, and the frozen instance is returned. With a callback, time is frozen only while the callback runs, and the callback's return value is returned.

Parameters

callable|null $callback

Receives the frozen instance.

Return Value

mixed

at line 54
protected mixed freezeSecond(callable|null $callback = null)

Freezes time at the start of the current second, dropping microseconds.

Useful when comparing against values stored in the database, which usually have no microsecond precision.

Parameters

callable|null $callback

Receives the frozen instance.

Return Value

mixed

at line 75
protected mixed travelTo(DateTimeInterface|string|int $date, callable|null $callback = null)

Moves time to a specific moment.

Strings are interpreted as UTC, matching how Chappy stores dates. Integers are treated as Unix timestamps.

Without a callback, time stays at $date until travelBack() or the end of the test. With a callback, the clock returns to its previous state after the callback runs, even if it throws.

Parameters

DateTimeInterface|string|int $date

The moment to travel to.

callable|null $callback

Optional code to run at that moment.

Return Value

mixed

The callback's return value, or null without a callback.

at line 99
protected Wormhole travel(int $value)

Moves time relative to the current (possibly already frozen) time.

Use negative values to move backward: $this->travel(-3)->hours();

Parameters

int $value

The amount of time to travel.

Return Value

Wormhole

at line 108
protected CarbonInterface travelBack()

Restores the real clock.

Return Value

CarbonInterface

The current real time.

at line 119
protected void restoreRealTime()

Restores the real clock after every test so frozen time never leaks into the next test.

Return Value

void