Use $this->travelTo() or freezeTime() in test suites to test date-sensitive logic without slowing down test execution with sleep().
Using sleep() in tests slows down execution suites significantly. Laravel's time travel helpers let you manipulate Carbon's internal clock instantaneously without real-world delays.
test('trial expires after 14 days', function () {
$user = User::factory()->create(['trial_ends_at' => now()->addDays(14)]);
// Instantly jump 15 days into the future
$this->travel(15)->days();
expect($user->fresh()->hasExpiredTrial())->toBeTrue();
});
- Replaces slow real-time sleep() calls with instant mock clock jumps
- travel(15)->days() moves time forward dynamically
- freezeTime() locks current time to prevent test flickering
Related Tips
View all tips →Use config() Instead of env() in Application Code
Call env() only inside configuration files and use config() throughout your application code to prevent missing values when configuration is cached.
Conditional Dependency Binding with #[BindWhen] in Laravel 13.22
Laravel 13.22 introduces the #[BindWhen] attribute for declarative, conditional service container bindings directly on implementation classes.