Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/spatie/test-time
A helper to control the flow of time
https://github.com/spatie/test-time
carbon php testing tests time
Last synced: 7 days ago
JSON representation
A helper to control the flow of time
- Host: GitHub
- URL: https://github.com/spatie/test-time
- Owner: spatie
- License: mit
- Created: 2019-06-12T13:11:51.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-02-05T13:30:59.000Z (12 months ago)
- Last Synced: 2025-01-19T20:00:02.206Z (14 days ago)
- Topics: carbon, php, testing, tests, time
- Language: PHP
- Homepage: https://freek.dev/1378-a-package-to-control-the-flow-of-time
- Size: 57.6 KB
- Stars: 206
- Watchers: 6
- Forks: 9
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README
# A helper to control the flow of time
[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/test-time.svg?style=flat-square)](https://packagist.org/packages/spatie/test-time)
[![run-tests](https://github.com/spatie/test-time/actions/workflows/run-tests.yml/badge.svg)](https://github.com/spatie/test-time/actions/workflows/run-tests.yml)
[![Total Downloads](https://img.shields.io/packagist/dt/spatie/test-time.svg?style=flat-square)](https://packagist.org/packages/spatie/test-time)[Carbon](https://github.com/briannesbitt/Carbon) is an excellent library to work with time in PHP projects. The `Spatie\TestTime\TestTime` class contains a few functions that under the hood manipulate the time that's considered the current time by Carbon.
```php
// the flow of time is frozen
TestTime::freeze();// we're now one year in the future
TestTime::addYear();// will report the real current year + 1
$year = (new Carbon())->format('Y');
```## Are you a visual learner?
[In this video](https://www.youtube.com/watch?v=1dJphbcWRxI), you'll learn how we use this package to test time sensitive functionality in [Mailcoach](https://mailcoach.app).
## Support us
[](https://spatie.be/github-ad-click/test-time)
We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
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](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
## Installation
You can install the package via composer:
```bash
composer require spatie/test-time
```## Usage
You can freeze the time with:
```php
TestTime::freeze();
```Alternatively you can pass in a carbon instance that will be used as the current time.
```php
TestTime::freeze($carbonInstance);
```You can also pass a format and time.
```php
TestTime::freeze('Y-m-d', '2019-06-12');
```### Progressing time
You can progress the time with any of the carbon functions starting with `add` or `sub`.
```php
TestTime::addMinute();TestTime::subHours(5);
// you can also chain calls
TestTime::addMonth(3)->addYear();
```### Unfreezing time
To let time flow naturally again, call `unfreeze`.
```php
TestTime::unfreeze();
```### Testing
``` bash
composer test
```### Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.
### Security
If you've found a bug regarding security please mail [[email protected]](mailto:[email protected]) instead of using the issue tracker.
## Postcardware
You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.
We publish all received postcards [on our company website](https://spatie.be/en/opensource/postcards).
## Credits
- [Freek Van der Herten](https://github.com/freekmurze)
- [Alex Vanderbist](https://github.com/AlexVanderbist)
- [All Contributors](../../contributors)## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.