Time Plugin
Overview
The Time Plugin for Pest allows you to control the flow of time in your Pest tests. This plugin assumes you're using Carbon to handle date and time in your app.
Source code: github.com/spatie/pest-plugin-test-time
Installation
Install the Time Plugin via the Composer package manager:
1composer require spatie/pest-plugin-test-time --dev
Usage
You can call freeze
on the testTime
function to freeze the current time.
1use Carbon\Carbon; 2use function Spatie\PestPluginTestTime\testTime; 3 4testTime()->freeze(); // the current time will not change anymore 5 6Carbon::now(); // returns the time 7 8sleep(2); 9 10Carbon::now(); // will return the same time as above
Freezing at a specific point in time
You can also freeze the time at a specific point by passing the time in format Y-m-d H:i:s
.
1testTime()->freeze('2021-01-02 12:34:56');2 3\Carbon\Carbon::now()->format('Y-m-d H:i:s') // returns '2021-01-02 12:34:56';
Changing the time
You can change the time, by calling any of the add
and sub
functions that are available on Carbon
.
1testTime()->freeze('2021-01-02 12:34:56');2 3testTime()->addHour(); // time is now at '2021-01-02 13:34:56'4 5// you can even chain method calls6testTime()->subMinute()->addSeconds(2); // time is now at '2021-01-02 13:33:58'
Next section: Watch Plugin →