-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a5bd83
commit 07fcc17
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace Cosmastech\LaravelStatsDAdapter\Tests\Adapters; | ||
|
||
use Cosmastech\LaravelStatsDAdapter\Adapters\EventDispatchingStatsRecord; | ||
use Cosmastech\LaravelStatsDAdapter\Events\TimingRecordedEvent; | ||
use Cosmastech\LaravelStatsDAdapter\Tests\AbstractTestCase; | ||
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\InMemoryClientAdapter; | ||
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\Models\InMemoryTimingRecord; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\Event; | ||
use Illuminate\Support\Testing\Fakes\EventFake; | ||
use PHPUnit\Framework\Attributes\Test; | ||
|
||
class EventDispatchingStatsRecordTest extends AbstractTestCase | ||
{ | ||
private EventFake $dispatcher; | ||
private EventDispatchingStatsRecord $eventDispatchingStatsRecord; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->dispatcher = Event::fake(); | ||
|
||
$this->eventDispatchingStatsRecord = new EventDispatchingStatsRecord($this->dispatcher); | ||
} | ||
|
||
#[Test] | ||
public function time_dispatchesTimingRecordedEvent(): void | ||
{ | ||
// Given | ||
$inMemoryClient = new InMemoryClientAdapter([], $this->eventDispatchingStatsRecord); | ||
|
||
// And | ||
$closure = fn() => ["hello" => "world"]; | ||
|
||
// When | ||
$output = $inMemoryClient->time($closure, "my-timing-stat", 0.92, [ | ||
"my-tag" => "my-value", | ||
]); | ||
|
||
// Then output should be returned from the client | ||
self::assertEquals(["hello" => "world"], $output); | ||
|
||
// And an event should be dispatched | ||
/** @var Collection<int, array<int, TimingRecordedEvent>> $eventsCollection */ | ||
$eventsCollection = $this->dispatcher->dispatched(TimingRecordedEvent::class); | ||
self::assertCount(1, $eventsCollection); | ||
self::assertCount(1, $eventsCollection->first()); | ||
|
||
/** @var TimingRecordedEvent $event */ | ||
$event = $eventsCollection->first()[0]; | ||
|
||
// And the record should have expected properties | ||
$record = $event->record; | ||
self::assertInstanceOf(InMemoryTimingRecord::class, $record); | ||
self::assertEquals("my-timing-stat", $record->stat); | ||
self::assertEquals(0.92, $record->sampleRate); | ||
self::assertEquals(["my-tag" => "my-value"], $record->tags); | ||
} | ||
} |