Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add StatsClientAwareInterface #12

Merged
merged 2 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
};
}
}
Loading