Skip to content

Commit 1d36e23

Browse files
authored
Allow microsecond travel (#52190)
1 parent 7d26b7e commit 1d36e23

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/Illuminate/Foundation/Testing/Wormhole.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,30 @@ public function __construct($value)
2424
$this->value = $value;
2525
}
2626

27+
/**
28+
* Travel forward the given number of microseconds.
29+
*
30+
* @param callable|null $callback
31+
* @return mixed
32+
*/
33+
public function microsecond($callback = null)
34+
{
35+
return $this->microseconds($callback);
36+
}
37+
38+
/**
39+
* Travel forward the given number of microseconds.
40+
*
41+
* @param callable|null $callback
42+
* @return mixed
43+
*/
44+
public function microseconds($callback = null)
45+
{
46+
Carbon::setTestNow(Carbon::now()->addMicroseconds($this->value));
47+
48+
return $this->handleCallback($callback);
49+
}
50+
2751
/**
2852
* Travel forward the given number of milliseconds.
2953
*

tests/Foundation/Testing/WormholeTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,18 @@ public function testCarbonImmutableCompatibility()
4646
// Restore the default Date Factory...
4747
Date::useDefault();
4848
}
49+
50+
public function testItCanTravelByMicroseconds()
51+
{
52+
Date::use(CarbonImmutable::class);
53+
Date::setTestNow(Date::parse('2000-01-01 00:00:00')->startOfSecond());
54+
55+
(new Wormhole(1))->microsecond();
56+
$this->assertSame('2000-01-01 00:00:00.000001', Date::now()->format('Y-m-d H:i:s.u'));
57+
58+
(new Wormhole(5))->microseconds();
59+
$this->assertSame('2000-01-01 00:00:00.000006', Date::now()->format('Y-m-d H:i:s.u'));
60+
61+
Date::useDefault();
62+
}
4963
}

0 commit comments

Comments
 (0)