<?php

namespace App\Tests\Security;

use App\Security\TwoFactorCodeMailer;
use PHPUnit\Framework\TestCase;
use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Twig\Environment;

class TwoFactorCodeMailerTest extends TestCase
{
    private MailerInterface $mailer;
    private Environment $twig;
    private TwoFactorCodeMailer $twoFactorCodeMailer;

    protected function setUp(): void
    {
        $this->mailer = $this->createMock(MailerInterface::class);
        $this->twig = $this->createMock(Environment::class);
        $this->twoFactorCodeMailer = new TwoFactorCodeMailer($this->mailer, $this->twig);
    }

    public function testSendAuthCode(): void
    {
        $user = $this->createStub(TwoFactorInterface::class);
        $user->method('getEmailAuthCode')->willReturn('123456');
        $user->method('getEmailAuthRecipient')->willReturn('test@example.com');

        $this->twig->expects($this->once())
            ->method('render')
            ->with('emails/2fa_code.html.twig', ['code' => '123456'])
            ->willReturn('<html>123456</html>');

        $this->mailer->expects($this->once())
            ->method('send')
            ->with($this->callback(function (Email $email) {
                return $email->getFrom()[0]->getAddress() === 'contact@e-cosplay.fr'
                    && $email->getTo()[0]->getAddress() === 'test@example.com'
                    && $email->getSubject() === 'CRM E-Cosplay - Code de verification'
                    && $email->getHtmlBody() === '<html>123456</html>';
            }));

        $this->twoFactorCodeMailer->sendAuthCode($user);
    }
}
