Freek has created spatie/test-time
, a package to easily freeze/rewind/advance time in PHP.
Imagine you’re building that your app can notify your user, but you don’t want to send more than one notification in a timeframe of five seconds. How are you going to test the time aspect? Do you have to create a test that takes five minutes?
Luckily the answer is “no”. If you’re using the popular Carbon library, you can set the value that the library considers “now”
The package provides a few convenience methods around Carbon’s setTestNow
method:
// time will not progress anymore
TestTime::freeze();
// Advance one minute
TestTime::addMinute();
// Rewind 5 hours
TestTime::subHours(5);
💁♂️ If you are not using Carbon you can reside to the php-timecop
PHP extension that does the job (and affects \DateTime
).
A PHP package to control the flow of time →spatie/test-time
(GitHub) →