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.
composer require damejidlo/datetime-factory
And then just register it in your config.neon
as a service.
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);
}