damejidlo / datetime-factory
Installs: 106 993
Dependents: 0
Suggesters: 0
Security: 0
Stars: 7
Watchers: 14
Forks: 1
Open Issues: 0
Requires
- php: >=7.1
- nette/utils: ^2.4@dev || ^3.0@dev
Requires (Dev)
- damejidlo/coding-standard: ^0.7.0
- nette/tester: ^2.0.0
- phpstan/phpstan-shim: ^0.9
- phpstan/phpstan-strict-rules: ^0.9
This package is auto-updated.
Last update: 2020-01-16 15:01:12 UTC
README
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); }