Skip to content

Commit

Permalink
Allow microsecond travel (#52190)
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald authored Jul 19, 2024
1 parent 7d26b7e commit 1d36e23
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Illuminate/Foundation/Testing/Wormhole.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,30 @@ public function __construct($value)
$this->value = $value;
}

/**
* Travel forward the given number of microseconds.
*
* @param callable|null $callback
* @return mixed
*/
public function microsecond($callback = null)
{
return $this->microseconds($callback);
}

/**
* Travel forward the given number of microseconds.
*
* @param callable|null $callback
* @return mixed
*/
public function microseconds($callback = null)
{
Carbon::setTestNow(Carbon::now()->addMicroseconds($this->value));

return $this->handleCallback($callback);
}

/**
* Travel forward the given number of milliseconds.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/Foundation/Testing/WormholeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,18 @@ public function testCarbonImmutableCompatibility()
// Restore the default Date Factory...
Date::useDefault();
}

public function testItCanTravelByMicroseconds()
{
Date::use(CarbonImmutable::class);
Date::setTestNow(Date::parse('2000-01-01 00:00:00')->startOfSecond());

(new Wormhole(1))->microsecond();
$this->assertSame('2000-01-01 00:00:00.000001', Date::now()->format('Y-m-d H:i:s.u'));

(new Wormhole(5))->microseconds();
$this->assertSame('2000-01-01 00:00:00.000006', Date::now()->format('Y-m-d H:i:s.u'));

Date::useDefault();
}
}

0 comments on commit 1d36e23

Please sign in to comment.