DateTime generator with support for adjusting current time.
The main purpose of the library is to assist testing application in time-sensitive scenarios without a need to adjust its code (besides switching to use library itself for creating dates in application).
It is relatively simple to test time-sensitive scenarios for unit tests, but for functional tests it is much harder. Of course, there are plenty of solutions:
slope-it/clock-mocklooks great, but depends on the PHP extension.lcobucci/clockis very popular, but requires dependency on its ownClockobject which would require updates of signatures of functions and methods and types of class properties.- Clock mocking in Symfony's PHPUnit bridge only mocks time-related functions, not
\DateTimeInterfacebased classes.
This library aims to keep the number of required changes at a level comparable with other solutions. Required updates for the code are listed below and basically limited to change of \DateTime constructor methods into call to static method of the different class. It also simplifies testing cases when code execution took some time and this time change used somehow (think of performance tracking as an example).
Whole API is exposed as a single \Flying\Date\Date class. API is provided as a set of static methods to make sure that they will be accessible from any part of code without a need to introduce any kind of additional dependencies.
Generated date objects are limited to \DateTimeImmutable. Upcoming ClockInterface from PSR-20 also provides only immutable dates.
Main API methods - now and from returning \DateTimeImmutable instances, whose values can be adjusted relative to the actual current time by providing a time shifting interval using adjust method.
In order to benefit from the ability to use time shifting, it is required to update parts of your code which creates new instances of the \DateTime or \DateTimeInterval objects.
To get \DateTime object it is required to convert obtained \DateTimeInterval object:
$mutableDateTime = \DateTime::createFromImmutable($immutableDateTime); - Before:
new \DateTimeImmutable() - After:
\Flying\Date\Date::now()
- Before:
new \DateTimeImmutable('2022-08-01', new \DateTimeZone('UTC')) - After:
\Flying\Date\Date::from('2022-08-01', 'UTC')
- Before:
\DateTimeImmutable::createFromFormat(\DateTimeInterface::ATOM, 2022-08-01T12:23:34Z') - After:
\Flying\Date\Date::fromFormat(\DateTimeInterface::ATOM, '2022-08-01T12:23:34Z')
In order to test application behavior in different points of time, you need to do two things:
- Provide date adjustment value by calling
\Flying\Date\Date::adjust()and pass either\DateTimeInterfacethat represents new target date or\DateIntervalto define time shift relatively to the current time. Callingadjust()with no arguments removes date adjustment. - Enable date adjustment by calling
\Flying\Date\Date::allowAdjustment(true)
IMPORTANT! You should never use date adjustment in your application's code, ONLY IN TESTS! Use of it in application's code may result in unexpected behavior!
Even in tests, you should limit use of this feature to the tests which actually needs date adjustment functionality and disable it after doing the test.
In order to simplify use of the library and to ensure correct enabling / disabling of the date adjustment functionality in tests - it is possible to use AdjustableDate PHP attribute.
To enable use of the attribute you need to edit your PHPUnit configuration (phpunit.xml) and add test extension:
<extensions>
<!-- ... other extensions ... -->
<bootstrap class="Flying\Date\PHPUnit\Extension\DateExtension"/>
</extensions>Then for test classes or (preferably) test methods that need to use adjustable date functionality you need to add #[AdjustableDate] attribute. It accepts optional configuration parameters:
bool $enabled- to control default state of the adjustable date functionality (trueby default)\DateTimeZone|string|null $timezone- to define timezone to use by default
For time shifting interval and adjusted dates, generated by the now and from methods microseconds part of the resulted date is forcibly set to zero. Date objects, which are generated without a date adjustment, are not affected.
It should be safe because date adjustment is meant to only be used for tests. Testing time shifts with microsecond precision on intervals less than a second is more reliable with use of the usleep(). For larger intervals include of microseconds may introduce difference of the whole second which may cause tests to break from time to time.
Signature:
\Flying\Date\Date::now(): \DateTimeImmutableProvides the date object for the current date. Either timezone, defined through setTimezone() or PHP default timezone is used.
In case if date adjustment is enabled - returned date will be adjusted by the defined time shifting interval.
Signature:
\Flying\Date\Date::from(\DateTimeInterface|\DateInterval|string $date, \DateTimeZone|string|null $timezone = null): \DateTimeImmutableCreate the date object for a given arbitrary point of time from provided date and timezone information. Either given timezone, timezone defined through setTimezone() or PHP default timezone is used. It is allowed to pass valid timezone name as timezone value.
In case if date adjustment is enabled - returned date will be adjusted by the defined time shifting interval.
Signature:
\Flying\Date\Date::fromFormat(string $format, string $datetime, \DateTimeZone|string|null $timezone = null): \DateTimeImmutable|boolCreate the date object for given date string, formatted using given format using given timezone information. Either given timezone, timezone defined through setTimezone() or PHP default timezone is used. It is allowed to pass valid timezone name as timezone value.
In case if date adjustment is enabled - returned date will be adjusted by the defined time shifting interval.
Signature:
\Flying\Date\Date::getTimezone(): \DateTimeZoneGet timezone that is used for creating date objects.
Signature:
\Flying\Date\Date::setTimezone(\DateTimeZone|string|null $timezone = null): voidDefines (or resets) timezone that is used for creating date objects. It is allowed to pass valid timezone name as value.
Signature:
\Flying\Date\Date::adjust(\DateInterval|\DateTimeInterface|string|null $adjustment = null): voidDefines (or resets) the time shifting interval that is used for date adjusting while creating date objects. It is allowed to define either absolute point of time by passing \DateTimeInterface instance or a time shift relative to the current point of time by passing \DateInterval object.
It is also allowed to pass date formats or date interval definitions.
Signature:
\Flying\Date\Date::getAdjustment(): ?\DateIntervalReturns currently defined the time shifting interval that is used for date adjusting while creating date objects.
Signature:
\Flying\Date\Date::allowAdjustment(bool $status): voidAllows control of the status of date adjustment functionality.
Signature:
\Flying\Date\Date::isAdjustmentAllowed(): boolReturns current status of date adjustment functionality.
Library is licensed under MIT License.