Giter VIP home page Giter VIP logo

pest-plugin-test-time's Introduction

A Pest plugin to control the flow of time

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.

pest-plugin-test-time's People

Contributors

adrianmrn avatar chengkangzai avatar freekmurze avatar gpibarra avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.