Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Allow microsecond travel #52190

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
Loading