Skip to content

Simple one-class lib for separation of NOW resolving.

License

Notifications You must be signed in to change notification settings

VaclavSir/datetime-factory

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 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 install damejidlo/datetime-factory

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

Example (of mocking in tests)

trait MockDateTimeFactoryTrait
{
	/** @return Container */
	abstract function getContainer();

	private function mockDateTimeFactoryService(\DateTime $subjectTime)
	{
		$dateTimeFactory = Mockery::mock(DateTimeFactory::class);
		$dateTimeFactory->shouldReceive('getNow')->andReturn($subjectTime);
		$this->getContainer()->removeService('dateTimeFactory');
		$this->getContainer()->addService('dateTimeFactory', $dateTimeFactory);
	}
}

About

Simple one-class lib for separation of NOW resolving.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%