Skip to content

Commit

Permalink
bind record as a singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmastech committed Jul 14, 2024
1 parent 02deb0f commit e449cab
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/StatsDAdapterServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cosmastech\LaravelStatsDAdapter;

use Cosmastech\LaravelStatsDAdapter\Adapters\EventDispatchingStatsRecord;
use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Support\DeferrableProvider;
Expand All @@ -25,14 +26,21 @@ public function register(): void
StatsDClientAdapter::class,
fn (Application $app) => $app->make(AdapterManager::class)->instance()
);

$this->app->singleton(
EventDispatchingStatsRecord::class,
function (Application $app): EventDispatchingStatsRecord {
return new EventDispatchingStatsRecord($app->make('events'));
}
);
}

/**
* @return array<int, string|class-string>
*/
public function provides(): array
{
return [AdapterManager::class, StatsDClientAdapter::class];
return [AdapterManager::class, StatsDClientAdapter::class, EventDispatchingStatsRecord::class];
}

protected function offerPublishing(): void
Expand Down
16 changes: 16 additions & 0 deletions tests/AdapterManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cosmastech\LaravelStatsDAdapter\Tests;

use Cosmastech\LaravelStatsDAdapter\Adapters\EventDispatchingAdapter;
use Cosmastech\StatsDClientAdapter\Adapters\Datadog\DatadogStatsDClientAdapter;
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\InMemoryClientAdapter;
use Cosmastech\StatsDClientAdapter\Adapters\League\LeagueStatsDClientAdapter;
Expand Down Expand Up @@ -144,4 +145,19 @@ public function setDefaultTags_passesToNewInstance(): void
// Then
self::assertEquals(["abc" => "123"], $adapterManager->instance("memory")->getDefaultTags());
}

#[Test]
public function instance_event_returnsConfiguredEventDispatchingAdapter(): void
{
// Given
$adapterManager = $this->createAdapterManager();

// And events is configured

// When
$eventDispatchingAdapter = $adapterManager->instance('event');

// Then
self::assertInstanceOf(EventDispatchingAdapter::class, $eventDispatchingAdapter);
}
}
14 changes: 14 additions & 0 deletions tests/StatsDAdapterServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cosmastech\LaravelStatsDAdapter\Tests;

use Cosmastech\LaravelStatsDAdapter\Adapters\EventDispatchingStatsRecord;
use Cosmastech\StatsDClientAdapter\Adapters\Datadog\DatadogStatsDClientAdapter;
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\InMemoryClientAdapter;
use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter;
Expand Down Expand Up @@ -59,4 +60,17 @@ public function passesDefaultTagsToAdapterManager(): void
// Then
self::assertEqualsCanonicalizing($defaultTags, $clientAdapter->getDefaultTags());
}

#[Test]
public function eventDispatchingStatsRecord_isSingleton(): void
{
// Given
$firstRecord = $this->app->make(EventDispatchingStatsRecord::class);

// When
$secondRecord = $this->app->make(EventDispatchingStatsRecord::class);

// Then
self::assertSame($firstRecord, $secondRecord);
}
}

0 comments on commit e449cab

Please sign in to comment.