Core\Test::advance
Core\Test::advance(Core\Time\Duration $by): voidCore\Test::advance( Core\Time\Duration $by, ): void
Core\Test::advance() moves the clock of a test forward by a duration. It works only in a test that fixes its clock with #[Test(at: ...)]. After the call, Core\Time::now() returns the moved time.
Use it to test code that depends on time. Examples are a login that ends after 30 minutes, or a link that stops working after a day. The test does not wait: it moves the clock and checks the result at once. Every test has its own clock, so moving it in one test does not change any other test. A Core\Time::sleep in that test does not wait either. It moves the same clock forward.
A negative duration moves the clock back. In a test without at:, or outside a test, advance throws a LogicError. The clock can show a time between the years -9999 and 9999. When the moved time is outside that range, advance throws a RuntimeError and the clock does not move.
The examples below show a token that expires and a clock moved in several small steps. They also show a session that ends after 30 minutes without use, and a retry that waits longer after each failure.
Parameters
$byCore\Time\DurationHow far to move the clock. A negative duration moves it back.
Return value
Returns void. Nothing. The next Core\Time::now() returns the moved time.
Errors
LogicErrorThe running test has noat:, so there is no fixed clock to move. The real clock of the computer is never moved.RuntimeErrorThe moved time is outside the range a clock can show, about the years -9999 to 9999. The clock does not move.
Examples
A token that expires
<?nvs// Shows how to test a token that expires. `Core\Test::advance` moves the fixed// clock past the end of the token, so the test does not wait 15 minutes.
use Core\Test;use Core\Time;use Core\Time\Instant;
class ResetToken{ public function constructor(public Instant $expiresAt) {}
public static function issue(): ResetToken { return new ResetToken(Time::now()->plus(15m)); }
public function isValid(): bool { return Time::now()->compareTo($this->expiresAt) < 0; }}
class ResetTokenTest{ #[Test(at: '2026-03-01T09:00:00Z')] public function itExpiresAfterFifteenMinutes(): void { ResetToken $token = ResetToken::issue(); Test::assertTrue($token->isValid());
// The clock now shows 09:14. The token is still valid. Test::advance(14m); Test::assertTrue($token->isValid());
// The clock now shows 09:15, the time the token expires. Test::advance(1m); Test::assertSame($token->isValid(), false); }}
// `nvs test` runs the test above. This line runs under `nvs run` and prints// `true`, because a new token is valid.echo ResetToken::issue()->isValid() ? 'true' : 'false', "\n";trueA clock moved in small steps
<?nvs// Shows how to move the clock in several small steps. Each call to// `Core\Test::advance` adds to the moves before it.
use Core\Test;use Core\Time;use Core\Time\Instant;
class Uptime{ public static function seconds(Instant $start): int { return Time::now()->toEpochSeconds() - $start->toEpochSeconds(); }}
class UptimeTest{ #[Test(at: '2026-03-01T00:00:00Z')] public function itCountsFromTheStart(): void { Instant $start = Time::now();
// This loop moves the clock forward 20 seconds, three times. uint $step = 0; while ($step < 3) { Test::advance(20s); $step = $step + 1; } Test::assertSame(Uptime::seconds($start), 60);
// `negated()` turns 30 seconds into minus 30 seconds. // A negative duration moves the clock back. Test::advance(30s->negated()); Test::assertSame(Uptime::seconds($start), 30); }}
// Outside a test with `at:`, there is no fixed clock to move.// `advance` throws a `LogicError`, so this prints `LogicError`.try { Test::advance(20s);} catch (LogicError $error) { echo 'LogicError', "\n";}LogicErrorA session that ends after idle time
<?nvs// Shows the test a web application writes for its login session. The session// ends when the user does nothing for 30 minutes.
use Core\Test;use Core\Time;use Core\Time\Instant;
class Session{ public function constructor(public Instant $lastSeen) {}
public static function start(): Session { return new Session(Time::now()); }
// Each request from the user calls this method. public function touch(): void { $this->lastSeen = Time::now(); }
public function isActive(): bool { return Time::now()->compareTo($this->lastSeen->plus(30m)) < 0; }}
class SessionTest{ #[Test(at: '2026-03-01T12:00:00Z')] public function aRequestKeepsTheSessionActive(): void { Session $session = Session::start(); Test::advance(25m); $session->touch();
// 50 minutes have passed, but the last request was 25 minutes ago. Test::advance(25m); Test::assertTrue($session->isActive()); }
#[Test(at: '2026-03-01T12:00:00Z')] public function thirtyIdleMinutesEndTheSession(): void { Session $session = Session::start(); Test::advance(30m); Test::assertSame($session->isActive(), false); }}
// `nvs test` runs the two tests above. Under `nvs run`, this prints `true`.echo Session::start()->isActive() ? 'true' : 'false', "\n";trueA retry that waits longer each time
<?nvs// Shows how to test a retry that waits twice as long after each failure. The test takes microseconds.// proof: test
use Core\Test;use Core\Time;use Core\Time\Duration;use Core\Time\Instant;
class Retry{ private Duration $wait; private Instant $nextTry;
public function constructor() { $this->wait = 1s; $this->nextTry = Time::now(); }
// Returns `true` when the wait after the last failure has passed. public function canTry(): bool { return Time::now()->compareTo($this->nextTry) >= 0; }
// Sets the time of the next try, and doubles the wait for the failure after it. public function failed(): void { $this->nextTry = Time::now()->plus($this->wait); $this->wait = $this->wait->multipliedBy(2); }}
class RetryTest{ #[Test(at: '2026-03-01T09:00:00Z')] public function theWaitDoublesAfterEachFailure(): void { var $retry = new Retry(); Test::assertTrue($retry->canTry());
// After the first failure, the retry waits 1 second. $retry->failed(); Test::assertSame($retry->canTry(), false); Test::advance(1s); Test::assertTrue($retry->canTry());
// After the second failure, it waits 2 seconds. After 1 second, it cannot try yet. $retry->failed(); Test::advance(1s); Test::assertSame($retry->canTry(), false); Test::advance(1s); Test::assertTrue($retry->canTry());
// After the third failure, it waits 4 seconds. $retry->failed(); Test::advance(4s); Test::assertTrue($retry->canTry()); }} RetryTest ✓ theWaitDoublesAfterEachFailure
0 failed, 1 passed, 0 skipped, 0 flaky