Skip to content

Commit d828e71

Browse files
committed
refactor: improve clock testing
1 parent e3b8dae commit d828e71

File tree

6 files changed

+32
-34
lines changed

6 files changed

+32
-34
lines changed

packages/clock/src/MockClock.php

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,14 @@
99
use Tempest\DateTime\DateTime;
1010
use Tempest\DateTime\DateTimeInterface;
1111
use Tempest\DateTime\Duration;
12-
use Tempest\DateTime\Timestamp;
1312

1413
final class MockClock implements Clock
1514
{
1615
private DateTimeInterface $now;
1716

1817
public function __construct(DateTimeImmutable|DateTimeInterface|string $now = 'now')
1918
{
20-
if ($now instanceof DateTimeImmutable) {
21-
$this->now = DateTime::fromTimestamp($now->getTimestamp());
22-
} else {
23-
$this->now = DateTime::parse($now);
24-
}
19+
$this->setNow($now);
2520
}
2621

2722
public function toPsrClock(): ClockInterface
@@ -34,13 +29,12 @@ public function now(): DateTimeInterface
3429
return $this->now;
3530
}
3631

37-
public function setNow(DateTimeInterface|string $now): void
32+
/**
33+
* Globally sets the current time to the specified value.
34+
*/
35+
public function setNow(DateTimeImmutable|DateTimeInterface|string $now): void
3836
{
39-
if ($now instanceof DateTimeInterface) {
40-
$this->now = $now;
41-
} else {
42-
$this->now = DateTime::parse($now);
43-
}
37+
$this->now = DateTime::parse($now);
4438
}
4539

4640
public function timestamp(): int
@@ -56,31 +50,35 @@ public function timestampMs(): int
5650
public function sleep(int|Duration $milliseconds): void
5751
{
5852
if ($milliseconds instanceof Duration) {
59-
$this->addInterval($milliseconds);
53+
$this->plus($milliseconds);
6054
return;
6155
}
6256

6357
$this->now = $this->now->plusMilliseconds($milliseconds);
6458
}
6559

66-
public function addInterval(Duration $duration): void
60+
/**
61+
* Adds the given duration. Providing an integer value adds the corresponding seconds to the current time.
62+
*/
63+
public function plus(int|Duration $duration): void
6764
{
68-
$this->now = $this->now->plus($duration);
69-
}
65+
if (is_int($duration)) {
66+
$duration = Duration::seconds($duration);
67+
}
7068

71-
public function subInternal(Duration $duration): void
72-
{
73-
$this->now = $this->now->minus($duration);
69+
$this->now = $this->now->plus($duration);
7470
}
7571

76-
public function changeTime(int $seconds): void
72+
/**
73+
* Removes the given duration. Providing an integer value removes the corresponding seconds to the current time.
74+
*/
75+
public function minus(int|Duration $duration): void
7776
{
78-
if ($seconds < 0) {
79-
$seconds = abs($seconds);
80-
$this->now = $this->now->minusSeconds($seconds);
81-
} else {
82-
$this->now = $this->now->plusSeconds($seconds);
77+
if (is_int($duration)) {
78+
$duration = Duration::seconds($duration);
8379
}
80+
81+
$this->now = $this->now->minus($duration);
8482
}
8583

8684
public function dd(): void

packages/clock/tests/MockClockTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ public function test_mock_clock_can_change_time(): void
6262
$addedTime = DateTime::parse('2024-09-11 13:54:25');
6363
$clock = new MockClock($dateTime);
6464

65-
$clock->changeTime(-2);
65+
$clock->minus(2);
6666

6767
$this->assertEquals($subtractedTime, $clock->now());
6868
$this->assertEquals($subtractedTime->getTimestamp()->getSeconds(), $clock->timestamp());
6969

70-
$clock->changeTime(4);
70+
$clock->plus(4);
7171

7272
$this->assertEquals($addedTime, $clock->now());
7373
$this->assertEquals($addedTime->getTimestamp()->getSeconds(), $clock->timestamp());

src/Tempest/Framework/Testing/IntegrationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ protected function migrate(string|object ...$migrationClasses): void
8989
}
9090
}
9191

92-
protected function clock(DateTimeInterface|string $now): MockClock
92+
protected function clock(DateTimeInterface|string $now = 'now'): MockClock
9393
{
9494
$clock = new MockClock($now);
9595

tests/Integration/Cache/CacheTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function test_put(): void
3030
$this->assertTrue($item->isHit());
3131
$this->assertSame('a', $item->get());
3232

33-
$clock->addInterval($interval);
33+
$clock->plus($interval);
3434

3535
$item = $pool->getItem('a');
3636
$this->assertFalse($item->isHit());
@@ -53,7 +53,7 @@ public function test_get(): void
5353
$this->assertSame('a', $cache->get('a'));
5454
$this->assertSame('b', $cache->get('b'));
5555

56-
$clock->addInterval($interval);
56+
$clock->plus($interval);
5757

5858
$this->assertSame(null, $cache->get('a'));
5959
$this->assertSame('b', $cache->get('b'));
@@ -76,7 +76,7 @@ public function test_resolve(): void
7676
$b = $cache->resolve('b', fn () => 'b');
7777
$this->assertSame('b', $b);
7878

79-
$clock->addInterval($interval);
79+
$clock->plus($interval);
8080

8181
$this->assertSame(null, $cache->get('a'));
8282
$this->assertSame('b', $cache->get('b'));

tests/Integration/Http/CleanupSessionsCommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ public function test_destroy_sessions(): void
3232

3333
$sessionManager->set(new SessionId('session_a'), 'test', 'value');
3434

35-
$clock->changeTime(9);
35+
$clock->plus(9);
3636

3737
$sessionManager->set(new SessionId('session_b'), 'test', 'value');
3838

39-
$clock->changeTime(2);
39+
$clock->plus(2);
4040

4141
$this->console
4242
->call('session:clean')

tests/Integration/Http/FileSessionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function test_is_valid(): void
112112

113113
$this->assertTrue($session->isValid());
114114

115-
$clock->changeTime(seconds: 1);
115+
$clock->plus(1);
116116

117117
$this->assertFalse($session->isValid());
118118
}

0 commit comments

Comments
 (0)