Skip to content

Commit

Permalink
add StatsClientAwareInterface (#12)
Browse files Browse the repository at this point in the history
* add StatsClientAwareInterface
  • Loading branch information
cosmastech authored Jul 19, 2024
1 parent 6affe04 commit 6c4d841
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Adapters/Concerns/StatsClientAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Cosmastech\StatsDClientAdapter\Adapters\Concerns;

use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter;

trait StatsClientAwareTrait
{
protected StatsDClientAdapter $statsClient;

public function setStatsClient(StatsDClientAdapter $statsDClientAdapter): void
{
$this->statsClient = $statsDClientAdapter;
}
}
10 changes: 10 additions & 0 deletions src/Adapters/Contracts/StatsClientAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Cosmastech\StatsDClientAdapter\Adapters\Contracts;

use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter;

interface StatsClientAwareInterface
{
public function setStatsClient(StatsDClientAdapter $statsDClientAdapter): void;
}
43 changes: 43 additions & 0 deletions tests/Adapters/StatsClientAware/StatsClientAwareTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Cosmastech\StatsDClientAdapter\Tests\Adapters\StatsClientAware;

use Cosmastech\StatsDClientAdapter\Adapters\Concerns\StatsClientAwareTrait;
use Cosmastech\StatsDClientAdapter\Adapters\Contracts\StatsClientAwareInterface;
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\InMemoryClientAdapter;
use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter;
use Cosmastech\StatsDClientAdapter\Tests\BaseTestCase;
use PHPUnit\Framework\Attributes\Test;

class StatsClientAwareTraitTest extends BaseTestCase
{
#[Test]
public function classHasStatsClientAware_canSetStatsClient(): void
{
// Given
$wantsStatsClient = self::makeWantsStatsClientClass();

// And
$statsDClientAdapter = new InMemoryClientAdapter();

// When
$wantsStatsClient->setStatsClient($statsDClientAdapter);

// Then
self::assertSame(
$statsDClientAdapter,
$wantsStatsClient->getStatsClient() /** @phpstan-ignore method.notFound (this is a method on an anonymous class) */
);
}

private static function makeWantsStatsClientClass(): StatsClientAwareInterface
{
return new class () implements StatsClientAwareInterface {
use StatsClientAwareTrait;
public function getStatsClient(): StatsDClientAdapter
{
return $this->statsClient;
}
};
}
}

0 comments on commit 6c4d841

Please sign in to comment.