<?php

namespace Tests\Feature;

use App\Events\ActionPerformed;
use App\Events\ActivityLogged;
use App\Events\ExceptionOccurred;
use App\Events\NewArticleFetched;
use App\Events\RouteActivated;
use App\Events\RouteArticleApproved;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Support\Facades\Event;
use PHPUnit\Framework\Attributes\DataProvider;
use RuntimeException;
use Tests\TestCase;

class EventListenerRegistrationTest extends TestCase
{
    /**
     * @return array<string, array{class-string}>
     */
    public static function eventProvider(): array
    {
        return [
            'ActionPerformed' => [ActionPerformed::class],
            'ActivityLogged' => [ActivityLogged::class],
            'ExceptionOccurred' => [ExceptionOccurred::class],
            'NewArticleFetched' => [NewArticleFetched::class],
            'RouteActivated' => [RouteActivated::class],
            'RouteArticleApproved' => [RouteArticleApproved::class],
        ];
    }

    /**
     * @param  class-string  $event
     */
    #[DataProvider('eventProvider')]
    public function test_event_has_exactly_one_listener(string $event): void
    {
        $this->assertCount(1, Event::getListeners($event));
    }

    public function test_reporting_an_exception_dispatches_one_event(): void
    {
        Event::fake([ExceptionOccurred::class]);

        app(ExceptionHandler::class)->report(new RuntimeException('boom'));

        Event::assertDispatchedTimes(ExceptionOccurred::class, 1);
    }
}
