Simple one-class lib for separation of NOW resolving.
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
tests
.gitignore
.travis.yml
README.md
composer.json
license.md

README.md

Downloads this Month Latest Stable Version

Motivation

In our application you should never obtain NOW via new DateTime() or from SQL NOW(). Because it's unmockable and therefore untestable.

This ultra-simple library provides service you can easily use and mock.

Install

composer require damejidlo/datetime-factory

And then just register it in your config.neon as a service.

Example (of mocking in tests)

public function testXyz(\DateTimeImmutable $subjectTime)
{
    $container = $this->createContainer();
    $dateTimeFactory = Mockery::mock(DateTimeFactoryImmutable::class);
    $dateTimeFactory->shouldReceive('getNow')->andReturn($subjectTime);
    $container->removeService('dateTimeFactory');
    $container->addService('dateTimeFactory', $dateTimeFactory);
}