<?php

namespace App\Tests\Service\Mailer;

use App\Entity\Account;
use App\Service\Mailer\Event\CreatedAdminEvent;
use App\Service\Mailer\Mailer;
use App\Service\Mailer\MailerSubscriber;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class MailerSubscriberTest extends TestCase
{
    public function testOnAdminEvent(): void
    {
        $urlGenerator = $this->createMock(UrlGeneratorInterface::class);
        $mailer = $this->createMock(Mailer::class);

        $subscriber = new MailerSubscriber($urlGenerator, $mailer);

        $account = $this->createStub(Account::class);
        $account->method('getEmail')->willReturn('test@example.com');
        $account->method('getUsername')->willReturn('testuser');

        $password = 'secret';

        $event = new CreatedAdminEvent($account, $password);

        $urlGenerator->expects($this->once())
            ->method('generate')
            ->with('app_home')
            ->willReturn('http://example.com/home');

        $mailer->expects($this->once())
            ->method('send')
            ->with(
                'test@example.com',
                'testuser',
                "[LudikEvent] - Création d'un compte administrateur",
                "mails/new_admin.twig",
                [
                    'username' => 'testuser',
                    'password' => 'secret',
                    'url' => 'http://example.com/home',
                ]
            );

        $subscriber->onAdminEvent($event);
    }
}
