spatie/pest-plugin-test-time

A Pest plugin to control the flow of time

Fund package maintenance!
spatie

Installs: 763 928

Dependents: 72

Suggesters: 0

Security: 0

Stars: 42

Watchers: 4

Forks: 7

2.2.0 2024-09-20 13:40 UTC

This package is auto-updated.

Last update: 2024-11-20 14:02:50 UTC


README

Latest Version on Packagist Tests Total Downloads

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

It also contains a custom expectation called toBeCarbon to easily check the values of Carbon instances.

$carbon = Carbon::createFromFormat('Y-m-d H:i:s', '2022-05-31 01:02:03');

// make an expectation on the whole date, including time
expect($carbon)->toBeCarbon('2022-05-31 01:02:03');

// make an expectation on only the date part
expect($carbon)->toBeCarbon('2022-05-31');

// explicitly pass in a format
expect($carbon)->toBeCarbon('2022', 'Y');

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 --dev

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'

Expecting a Carbon value

This package offers a custom expectation called toBeCarbon to easily check the value of a Carbon instance.

$carbon = Carbon::createFromFormat('Y-m-d H:i:s', '2022-05-31 01:02:03');

// make an expectation on the whole date, including time
expect($carbon)->toBeCarbon('2022-05-31 01:02:03');

// make an expectation on only the date part
expect($carbon)->toBeCarbon('2022-05-31');

// explicitly pass in a format
expect($carbon)->toBeCarbon('2022', 'Y');

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.