Skip to content

Commit

Permalink
Merge pull request #11 from cosmastech/service-provider-for-stats-cli…
Browse files Browse the repository at this point in the history
…ent-aware

Automatically set the stats client when class implements `StatsClientAwareInterface`
  • Loading branch information
cosmastech authored Jul 19, 2024
2 parents 3ecf2f8 + ac70a46 commit d9b1f6c
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"require": {
"php": "^8.2",
"cosmastech/statsd-client-adapter": "^0.2.0",
"cosmastech/statsd-client-adapter": "^0.3",
"illuminate/support": "^10.0|^11.0",
"illuminate/contracts": "^10.0|^11.0"
},
Expand All @@ -45,7 +45,8 @@
"extra": {
"laravel": {
"providers": [
"Cosmastech\\LaravelStatsDAdapter\\StatsDAdapterServiceProvider"
"Cosmastech\\LaravelStatsDAdapter\\StatsDAdapterServiceProvider",
"Cosmastech\\LaravelStatsDAdapter\\StatsClientAwareServiceProvider"
],
"aliases": {
"Stats": "Cosmastech\\LaravelStatsDAdapter\\Stats"
Expand Down
26 changes: 26 additions & 0 deletions src/StatsClientAwareServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Cosmastech\LaravelStatsDAdapter;

use Cosmastech\StatsDClientAdapter\Adapters\Contracts\StatsClientAwareInterface;
use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\ServiceProvider;

class StatsClientAwareServiceProvider extends ServiceProvider
{
public function register(): void
{
//
}

public function boot(): void
{
$this->app->afterResolving(
StatsClientAwareInterface::class,
function (StatsClientAwareInterface $wantsStatsClient, Application $application): void {
$wantsStatsClient->setStatsClient($application->make(StatsDClientAdapter::class));
}
);
}
}
8 changes: 8 additions & 0 deletions tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cosmastech\LaravelStatsDAdapter\Tests;

use Cosmastech\LaravelStatsDAdapter\AdapterManager;
use Cosmastech\LaravelStatsDAdapter\StatsClientAwareServiceProvider;
use Illuminate\Config\Repository;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase;
Expand All @@ -11,6 +12,13 @@ class AbstractTestCase extends TestCase
{
use WithWorkbench;

protected $enablesPackageDiscoveries = true;

protected function getPackageProviders($app)
{
return [StatsClientAwareServiceProvider::class];
}

protected function getEnvironmentSetUp($app)
{
/** @var Repository $config */
Expand Down
17 changes: 17 additions & 0 deletions tests/Fixtures/WantsStatsClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Cosmastech\LaravelStatsDAdapter\Tests\Fixtures;

use Cosmastech\StatsDClientAdapter\Adapters\Concerns\StatsClientAwareTrait;
use Cosmastech\StatsDClientAdapter\Adapters\Contracts\StatsClientAwareInterface;
use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter;

class WantsStatsClient implements StatsClientAwareInterface
{
use StatsClientAwareTrait;

public function getStatsClient(): StatsDClientAdapter
{
return $this->statsClient;
}
}
27 changes: 27 additions & 0 deletions tests/StatsClientAwareServiceProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Cosmastech\LaravelStatsDAdapter\Tests;

use Cosmastech\LaravelStatsDAdapter\Tests\Fixtures\WantsStatsClient;
use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter;
use Illuminate\Support\Facades\Config;
use PHPUnit\Framework\Attributes\Test;

class StatsClientAwareServiceProviderTest extends AbstractTestCase
{
#[Test]
public function appResolvesStatsClientAwareInterfaceClass_classHasStatsClientSet(): void
{
// Given
Config::set("statsd-adapter.default", "memory");

// When
$wantsStatsClient = $this->app->make(WantsStatsClient::class);

// Then
self::assertSame(
$this->app->make(StatsDClientAdapter::class),
$wantsStatsClient->getStatsClient()
);
}
}

0 comments on commit d9b1f6c

Please sign in to comment.