Statistics
56309
7
0
1.6y
Badges
Tags
utils
Dependencies

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

    • Minimal required version of PHP is 7.1.
    • Use strict types and return types.
    • Revert removal of nette/utils.
    • DateTimeFactory is deprecated, use DateTimeImmutableFactory instead.
    • Use phpstan in CI build.
    • Use damejidlo/coding-standard in CI build.
  • 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