<?php

namespace Tests\Feature;

use App\Events\NewArticleFetched;
use App\Models\Article;
use App\Models\Feed;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Event;
use Tests\TestCase;

class NewArticleFetchedEventTest extends TestCase
{
    use RefreshDatabase;

    public function test_new_article_fetched_event_dispatched_via_dispatch_method(): void
    {
        Event::fake([NewArticleFetched::class]);

        $feed = Feed::factory()->create();

        $article = Article::create([
            'url' => 'https://www.google.com',
            'feed_id' => $feed->id,
            'title' => 'Test Article',
        ]);

        $article->dispatchFetchedEvent();

        Event::assertDispatched(NewArticleFetched::class, function (NewArticleFetched $event) use ($article) {
            return $event->article->id === $article->id;
        });
    }
}
