Skip to content
This repository has been archived by the owner on Jun 19, 2020. It is now read-only.
/ datetime-factory Public archive

Simple one-class lib for separation of NOW resolving.

License

Notifications You must be signed in to change notification settings

damejidlo/datetime-factory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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);
}

About

Simple one-class lib for separation of NOW resolving.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages