Time traveling is coming to Laravel 8
Published on by Eric L. Barnes
Another small helper feature coming to Laravel 8 is the ability to fluently time travel in your tests. This will allow you to easily test for things like what happens when a free trial ends, or a next billing date, etc. Here is a quick example that @enunomaduro shared on Twitter showing how you can use this new feature:
// Travel forward five minutes$this->travel(5)->minutes;$this->get($route)->assertSee('Created 5 mins ago'); // Travel forward one year$this->travel(1)->year;$this->get($route)->assertSee('Created 1 year ago'); // Travel to the given date time$this->travelTo($user->trial_ends_at);$this->get($route)->assertSee('Your free trial is expired');
Under the hood, this is a wrapper to Carbon’s setTestNow and is designed to be a little more fluent than the default. Of course, usage is optional but it’s one of those little things that aim to make your development life a little easier.
Eric is the creator of Laravel News and has been covering Laravel since 2012.