大美食/datetime-factory

此包已被弃用,且不再维护。未建议替代包。

2.0.0 2018-02-08 12:47 UTC

This package is auto-updated.

Last update: 2020-01-16 15:01:12 UTC


README

Downloads this Month Latest Stable Version

动机

在我们的应用中,您不应该通过 new DateTime() 或从 SQL NOW() 获取 NOW。因为它不可模拟,因此不可测试。

这个超简单的库提供了您可以使用和模拟的服务。

安装

composer require damejidlo/datetime-factory

然后只需在您的 config.neon 中将其注册为服务。

示例(测试中的模拟)

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