diff --git a/tests/Adapters/EventDispatchingStatsRecordTest.php b/tests/Adapters/EventDispatchingStatsRecordTest.php new file mode 100644 index 0000000..13852d2 --- /dev/null +++ b/tests/Adapters/EventDispatchingStatsRecordTest.php @@ -0,0 +1,62 @@ +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> $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); + } +}