Statistics
21407
5
1
63d
Badges
Dependencies
  • php (>= 5.6.0)

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)

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

    Removed dependency on nette/utils
    added DateTimeImmutableFactory

  • 1.0.1 Welcome Nette 2.4!

    This release allows to install Nette 2.4

  • 1.0.0 First release

Is this addon outdated? Did you find an issue? Please report it.

Componette Componette admin@componette.com