spatie/pest-plugin-test-time

A Pest plugin to control the flow of time

Fund package maintenance!
spatie

Installs: 163 415

Dependents: 42

Suggesters: 0

Security: 0

Stars: 34

Watchers: 4

Forks: 0

1.1.1 2022-06-05 13:57 UTC

This package is auto-updated.

Last update: 2023-01-10 14:02:58 UTC


README

Latest Version on Packagist Tests GitHub Code Style Action Status 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

68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f706573742d706c7567696e2d746573742d74696d652e6a70673f743d31

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'

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.