Skip to content

Commit 0fde761

Browse files
[10.x] Allow Sleep::until() to be passed a timestamp as a string (#48883)
* Allow Sleep::until to be passed a timestamp as a string Also loosens up the type hinting to match what is actually handled. * Add test for milliseconds --------- Co-authored-by: Tim MacDonald <hello@timacdonald.me>
1 parent 65f00dd commit 0fde761

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/Illuminate/Support/Sleep.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ public static function for($duration)
8080
/**
8181
* Sleep until the given timestamp.
8282
*
83-
* @param \DateTimeInterface|int $timestamp
83+
* @param \DateTimeInterface|int|float|numeric-string $timestamp
8484
* @return static
8585
*/
8686
public static function until($timestamp)
8787
{
88-
if (is_int($timestamp)) {
88+
if (is_numeric($timestamp)) {
8989
$timestamp = Carbon::createFromTimestamp($timestamp);
9090
}
9191

tests/Support/SleepTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,32 @@ public function testItCanSleepTillGivenTimestamp()
229229
]);
230230
}
231231

232+
public function testItCanSleepTillGivenTimestampAsString()
233+
{
234+
Sleep::fake();
235+
Carbon::setTestNow(now()->startOfDay());
236+
237+
Sleep::until(strval(now()->addMinute()->timestamp));
238+
239+
Sleep::assertSequence([
240+
Sleep::for(60)->seconds(),
241+
]);
242+
}
243+
244+
public function testItCanSleepTillGivenTimestampAsStringWithMilliseconds()
245+
{
246+
Sleep::fake();
247+
Carbon::setTestNow('2000-01-01 00:00:00.000'); // 946684800
248+
249+
Sleep::until('946684899.123');
250+
251+
Sleep::assertSequence([
252+
Sleep::for(1)->minute()
253+
->and(39)->seconds()
254+
->and(123)->milliseconds(),
255+
]);
256+
}
257+
232258
public function testItSleepsForZeroTimeWithNegativeDateTime()
233259
{
234260
Sleep::fake();

0 commit comments

Comments
 (0)