Skip to content

Commit

Permalink
[9.x] Adds "freezeTime" helper for tests. (#41460)
Browse files Browse the repository at this point in the history
* [9.x] Adds "freezeTime" helper for tests.

* Removes start of second.
Returns callback result, if any.

* Adds freezeCurrentSecond for DB timestamps without subsecond precision.

* formatting

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
DarkGhostHunter and taylorotwell committed Mar 14, 2022
1 parent a9b020e commit 45d3e0a
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/Illuminate/Foundation/Testing/Concerns/InteractsWithTime.php
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

0 comments on commit 45d3e0a

Please sign in to comment.