大美食 / datetime-factory
此包已被弃用,且不再维护。未建议替代包。
2.0.0
2018-02-08 12:47 UTC
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
动机
在我们的应用中,您不应该通过 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); }