Skip to content

[9.x] Adds "freezeTime" helper for tests#41460

Merged
taylorotwell merged 4 commits into
laravel:9.xfrom
DarkGhostHunter:feat/freeze-time
Mar 14, 2022
Merged

[9.x] Adds "freezeTime" helper for tests#41460
taylorotwell merged 4 commits into
laravel:9.xfrom
DarkGhostHunter:feat/freeze-time

Conversation

@DarkGhostHunter

@DarkGhostHunter DarkGhostHunter commented Mar 13, 2022

Copy link
Copy Markdown
Contributor

What?

The freezeTime() method in tests will freeze the current time.

public function test_something()
{
    $this->freezeTime();
}

This is equivalent to $this->travelTo(Carbon::now()).

This PR also adds freezeSecond() which does the same, but sets the time at the start of the current second. This fixes any problem when comparing dates that have no sub-second precision, like the default database timestamps.

public function test_something()
{
    $this->freezeSecond();

    User::factory()->createOne();

    static::assertEquals(now(), User::find(1)->created_at);
}

Why?

Just better syntax.

@taylorotwell taylorotwell merged commit 45d3e0a into laravel:9.x Mar 14, 2022
@DarkGhostHunter DarkGhostHunter deleted the feat/freeze-time branch March 14, 2022 19:01
@driesvints driesvints changed the title [9.x] Adds "freezeTime" helper for tests. [9.x] Adds "freezeTime" helper for tests Mar 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants