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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/Illuminate/Foundation/Testing/Concerns/InteractsWithTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,28 @@

trait InteractsWithTime
{
/**
* Freeze time.
*
* @param callable|null $callback
* @return mixed
*/
public function freezeTime($callback = null)
{
return $this->travelTo(Carbon::now(), $callback);
}

/**
* Freeze time at the beginning of the current second.
*
* @param callable|null $callback
* @return mixed
*/
public function freezeSecond($callback = null)
{
return $this->travelTo(Carbon::now()->startOfSecond(), $callback);
}

/**
* Begin travelling to another time.
*
Expand All @@ -30,7 +52,7 @@ public function travelTo($date, $callback = null)
Carbon::setTestNow($date);

if ($callback) {
return tap($callback(), function () {
return tap($callback(Carbon::now()), function () {
Carbon::setTestNow();
});
}
Expand Down