Skip to content

Commit 45d3e0a

Browse files
[9.x] Adds "freezeTime" helper for tests. (#41460)
* [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 <[email protected]>
1 parent a9b020e commit 45d3e0a

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/Illuminate/Foundation/Testing/Concerns/InteractsWithTime.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,28 @@
77

88
trait InteractsWithTime
99
{
10+
/**
11+
* Freeze time.
12+
*
13+
* @param callable|null $callback
14+
* @return mixed
15+
*/
16+
public function freezeTime($callback = null)
17+
{
18+
return $this->travelTo(Carbon::now(), $callback);
19+
}
20+
21+
/**
22+
* Freeze time at the beginning of the current second.
23+
*
24+
* @param callable|null $callback
25+
* @return mixed
26+
*/
27+
public function freezeSecond($callback = null)
28+
{
29+
return $this->travelTo(Carbon::now()->startOfSecond(), $callback);
30+
}
31+
1032
/**
1133
* Begin travelling to another time.
1234
*
@@ -30,7 +52,7 @@ public function travelTo($date, $callback = null)
3052
Carbon::setTestNow($date);
3153

3254
if ($callback) {
33-
return tap($callback(), function () {
55+
return tap($callback(Carbon::now()), function () {
3456
Carbon::setTestNow();
3557
});
3658
}

0 commit comments

Comments
 (0)