Skip to content

Commit

Permalink
test timing
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmastech committed Jul 13, 2024
1 parent 6a5bd83 commit 07fcc17
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions tests/Adapters/EventDispatchingStatsRecordTest.php
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);
}
}

0 comments on commit 07fcc17

Please sign in to comment.