Skip to content

Commit 13c3fb1

Browse files
committed
default to using package level Clock implementation
1 parent 669cc0d commit 13c3fb1

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

src/Adapters/InMemory/InMemoryClientAdapter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\Models\InMemoryTimingRecord;
1515
use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter;
1616
use Cosmastech\StatsDClientAdapter\TagNormalizers\NoopTagNormalizer;
17+
use Cosmastech\StatsDClientAdapter\Utility\Clock;
1718
use Psr\Clock\ClockInterface;
1819

1920
class InMemoryClientAdapter implements StatsDClientAdapter, TagNormalizerAware
@@ -24,7 +25,7 @@ class InMemoryClientAdapter implements StatsDClientAdapter, TagNormalizerAware
2425
protected InMemoryStatsRecord $stats;
2526
protected readonly ClockInterface $clock;
2627

27-
public function __construct(ClockInterface $clock, array $defaultTags = [])
28+
public function __construct(ClockInterface $clock = new Clock(), array $defaultTags = [])
2829
{
2930
$this->clock = $clock;
3031

src/Utility/Clock.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Cosmastech\StatsDClientAdapter\Utility;
4+
5+
use DateTimeImmutable;
6+
use Psr\Clock\ClockInterface;
7+
8+
/**
9+
* The simplest possible interface: always return the current time.
10+
*/
11+
class Clock implements ClockInterface
12+
{
13+
/**
14+
* @inheritDoc
15+
*/
16+
public function now(): DateTimeImmutable
17+
{
18+
return new DateTimeImmutable();
19+
}
20+
}

tests/Adapters/InMemory/InMemoryTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\InMemoryClientAdapter;
66
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\Models\InMemoryStatsRecord;
77
use Cosmastech\StatsDClientAdapter\Tests\BaseTestCase;
8-
use Cosmastech\StatsDClientAdapter\Tests\Doubles\ClockStub;
98
use Cosmastech\StatsDClientAdapter\Tests\Doubles\TagNormalizerSpy;
10-
use DateTimeImmutable;
119
use PHPUnit\Framework\Attributes\Test;
1210

1311
class InMemoryTest extends BaseTestCase
@@ -16,7 +14,7 @@ class InMemoryTest extends BaseTestCase
1614
public function getStats_returnsInMemoryStatsRecord(): void
1715
{
1816
// Given
19-
$inMemoryClient = new InMemoryClientAdapter(new ClockStub(new DateTimeImmutable()));
17+
$inMemoryClient = new InMemoryClientAdapter();
2018

2119
// When
2220
$record = $inMemoryClient->getStats();
@@ -30,7 +28,7 @@ public function getStats_returnsInMemoryStatsRecord(): void
3028
public function reset_clearsStats(): void
3129
{
3230
// Given
33-
$inMemoryClient = new InMemoryClientAdapter(new ClockStub(new DateTimeImmutable()));
31+
$inMemoryClient = new InMemoryClientAdapter();
3432

3533
// And set some data
3634
$inMemoryClient->increment("bogus", 1, ["tag1" => true], 99);
@@ -49,7 +47,7 @@ public function reset_clearsStats(): void
4947
public function setTagNormalizer(): void
5048
{
5149
// Given
52-
$inMemoryClient = new InMemoryClientAdapter(new ClockStub(new DateTimeImmutable()));
50+
$inMemoryClient = new InMemoryClientAdapter();
5351

5452
// And
5553
$tagNormalizerSpy = new TagNormalizerSpy();
@@ -66,7 +64,7 @@ public function setTagNormalizer(): void
6664
public function getClient_returnsNull(): void
6765
{
6866
// Given
69-
$inMemoryClient = new InMemoryClientAdapter(new ClockStub(new DateTimeImmutable()));
67+
$inMemoryClient = new InMemoryClientAdapter();
7068

7169
// When
7270
$client = $inMemoryClient->getClient();

tests/Doubles/ClockStub.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@
88

99
class ClockStub implements ClockInterface
1010
{
11+
/** @var array<int,DateTimeImmutable> */
1112
private readonly array $time;
13+
1214
private int $currentIndex = 0;
1315

1416
public function __construct(array|DateTimeImmutable $now)
1517
{
1618
$time = is_array($now) ? $now : [$now];
1719

1820
if (empty($time)) {
19-
throw new InvalidArgumentException("Clock requires at least one DateTimeImmutable");
21+
throw new InvalidArgumentException("Clock requires at least one DateTimeImmutable.");
2022
}
2123

2224
$this->time = $time;

0 commit comments

Comments
 (0)