Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from Mar 14, 2022
Merged

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

merged 4 commits into from Mar 14, 2022

Conversation

DarkGhostHunter
Copy link
Contributor

@DarkGhostHunter DarkGhostHunter commented Mar 13, 2022

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.

None yet

2 participants