olajoscs/dateprovider

一个简单的 DateTimeImmutable 提供器,用于替换或模拟 "new DateTimeImmutable"

1.0.0 2021-02-25 21:31 UTC

This package is auto-updated.

Last update: 2024-09-26 05:08:56 UTC


README

一个简单的库,用于替换代码中的 new \DateTimenew \DateTimImmutable

为什么?

当你有隐藏的依赖时,测试很难。获取当前时间是其中之一,因为它总是变化的。

如何使用

类应该依赖于 DateProvider 接口。库中有两个实现

  • CurrentDateProvider:返回当前日期/时间,就像 new \DateTimeImmutable,你的依赖注入容器应该实例化这个类
  • ExactDateProvider:返回一个精确的日期/时间,可以用来模拟时间流逝,当编写测试时非常有用。