Laravel offers a fluent API for manipulating time in your tests.
I can see the appeal of writing code like $this->travel(2)->weeks()
for moving the clock forward.
And I can't deny how cool it is that Laravel has a Wormhole
class backing this API.
But looking at my projects, I don't use these helpers, and I prefer to use the more explicit Carbon::setTestNow($date)
method instead.
Why?
First, it's important to recognize that under the hood Laravel's time API and its Wormhole
class are still calling that Carbon method for you.
So they are two different ways of doing the same basic thing.
But there is a slight difference: $this->travel()
bases movement on the current time, while Carbon::setTestNow()
forces me to be explicit in setting a specific date or time.
This might not seem like a big deal, but I find it helps me to be more explicit in my tests. And I just like the idea of my tests running the same way every time, and not subtly marching date logic forward each run.
Here to help,
Joel
P.S. What about time travel and leap years? Join the Mastering Laravel community and ask all your questions.