阅读(1454) (5)

Laravel 8 时间交互

2021-07-08 16:55:53 更新

测试时,有时可能需要修改诸如 nowIlluminateSupportCarbon::now() 之类的助手返回的时间。 值得庆幸的是,Laravel 的基本功能测试类包括一些帮助程序,可让您操纵当前时间:

public function testTimeCanBeManipulated()
{
    // 调至未来...
    $this->travel(5)->milliseconds();
    $this->travel(5)->seconds();
    $this->travel(5)->minutes();
    $this->travel(5)->hours();
    $this->travel(5)->days();
    $this->travel(5)->weeks();
    $this->travel(5)->years();

    // 调至过去...
    $this->travel(-5)->hours();

    // 调至一个明确的时间...
    $this->travelTo(now()->subHours(6));

    // 返回现在...
    $this->travelBack();
}