InteractsWithTime
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
Freezes time at the current moment.
Freezes time at the start of the current second, dropping microseconds.
Moves time to a specific moment.
Restores the real clock.
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.
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.
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.
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();
at line 108
protected CarbonInterface
travelBack()
Restores the real clock.
at line 119
protected void
restoreRealTime()
Restores the real clock after every test so frozen time never leaks into the next test.