Skip to content

Commit

Permalink
default to using package level Clock implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmastech committed Jul 8, 2024
1 parent 669cc0d commit 13c3fb1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/Adapters/InMemory/InMemoryClientAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\Models\InMemoryTimingRecord;
use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter;
use Cosmastech\StatsDClientAdapter\TagNormalizers\NoopTagNormalizer;
use Cosmastech\StatsDClientAdapter\Utility\Clock;
use Psr\Clock\ClockInterface;

class InMemoryClientAdapter implements StatsDClientAdapter, TagNormalizerAware
Expand All @@ -24,7 +25,7 @@ class InMemoryClientAdapter implements StatsDClientAdapter, TagNormalizerAware
protected InMemoryStatsRecord $stats;
protected readonly ClockInterface $clock;

public function __construct(ClockInterface $clock, array $defaultTags = [])
public function __construct(ClockInterface $clock = new Clock(), array $defaultTags = [])
{
$this->clock = $clock;

Expand Down
20 changes: 20 additions & 0 deletions src/Utility/Clock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Cosmastech\StatsDClientAdapter\Utility;

use DateTimeImmutable;
use Psr\Clock\ClockInterface;

/**
* The simplest possible interface: always return the current time.
*/
class Clock implements ClockInterface
{
/**
* @inheritDoc
*/
public function now(): DateTimeImmutable
{
return new DateTimeImmutable();
}
}
10 changes: 4 additions & 6 deletions tests/Adapters/InMemory/InMemoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\InMemoryClientAdapter;
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\Models\InMemoryStatsRecord;
use Cosmastech\StatsDClientAdapter\Tests\BaseTestCase;
use Cosmastech\StatsDClientAdapter\Tests\Doubles\ClockStub;
use Cosmastech\StatsDClientAdapter\Tests\Doubles\TagNormalizerSpy;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\Test;

class InMemoryTest extends BaseTestCase
Expand All @@ -16,7 +14,7 @@ class InMemoryTest extends BaseTestCase
public function getStats_returnsInMemoryStatsRecord(): void
{
// Given
$inMemoryClient = new InMemoryClientAdapter(new ClockStub(new DateTimeImmutable()));
$inMemoryClient = new InMemoryClientAdapter();

// When
$record = $inMemoryClient->getStats();
Expand All @@ -30,7 +28,7 @@ public function getStats_returnsInMemoryStatsRecord(): void
public function reset_clearsStats(): void
{
// Given
$inMemoryClient = new InMemoryClientAdapter(new ClockStub(new DateTimeImmutable()));
$inMemoryClient = new InMemoryClientAdapter();

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

// And
$tagNormalizerSpy = new TagNormalizerSpy();
Expand All @@ -66,7 +64,7 @@ public function setTagNormalizer(): void
public function getClient_returnsNull(): void
{
// Given
$inMemoryClient = new InMemoryClientAdapter(new ClockStub(new DateTimeImmutable()));
$inMemoryClient = new InMemoryClientAdapter();

// When
$client = $inMemoryClient->getClient();
Expand Down
4 changes: 3 additions & 1 deletion tests/Doubles/ClockStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@

class ClockStub implements ClockInterface
{
/** @var array<int,DateTimeImmutable> */
private readonly array $time;

private int $currentIndex = 0;

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

if (empty($time)) {
throw new InvalidArgumentException("Clock requires at least one DateTimeImmutable");
throw new InvalidArgumentException("Clock requires at least one DateTimeImmutable.");
}

$this->time = $time;
Expand Down

0 comments on commit 13c3fb1

Please sign in to comment.