A Pest plugin to control the flow of time
This Pest plugin offers a function testTime
that allows you to freeze and manipulate the current time in your tests.
use function Spatie\PestPluginTestTime\testTime;
testTime()->freeze(); // time will not change anymore
testTime()->addMinute(); // move time forward one minute
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Requirements
This package is a wrapper around Carbon's setTestNow()
function. Therefore, you can only use this Pest plugin only in projects that use Carbon.
Installation
You can install the package via composer:
composer require spatie/pest-plugin-test-time
Usage
You can call freeze
on the testTime
function to freeze the current time.
use Carbon\Carbon;
use function Spatie\PestPluginTestTime\testTime;
testTime()->freeze(); // the current time will not change anymore
Carbon::now(); // returns the time
sleep(2);
Carbon::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
.
testTime()->freeze('2021-01-02 12:34:56');
\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
.
testTime()->freeze('2021-01-02 12:34:56');
testTime()->addHour(); // time is now at '2021-01-02 13:34:56'
// you can even chain method calls
testTime()->subMinute()->addSeconds(2); // time is now at '2021-01-02 13:33:58'
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.