<?php

declare(strict_types=1);

/*
 * This file is part of the Ferienpass package.
 *
 * (c) Richard Henkenjohann <richard@ferienpass.online>
 *
 * For more information visit the project website <https://ferienpass.online>
 * or the documentation under <https://docs.ferienpass.online>.
 */

namespace Ferienpass\CoreBundle\Tests\MessageHandler;

use Doctrine\ORM\EntityManagerInterface;
use Ferienpass\CoreBundle\Entity\User;
use Ferienpass\CoreBundle\Message\BlockEmail as BlockEmailCommand;
use Ferienpass\CoreBundle\MessageHandler\BlockEmail;
use Ferienpass\CoreBundle\Repository\UserRepository;
use PHPUnit\Framework\TestCase;

final class BlockEmailTest extends TestCase
{
    public function testBlocksUserAndPersistsBlockedContact(): void
    {
        $user = new User();
        $user->setEmail('test@example.com');

        $userRepository = $this->createMock(UserRepository::class);
        $userRepository->method('findOneBy')->willReturn($user);

        $em = $this->createMock(EntityManagerInterface::class);
        $em->expects(self::once())->method('persist');

        $handler = new BlockEmail($userRepository, $em, 'test-secret');
        $handler(new BlockEmailCommand('test@example.com'));

        self::assertTrue($user->isBlockedEmail());
    }

    public function testDoesNotThrowWhenUserNotFound(): void
    {
        $userRepository = $this->createMock(UserRepository::class);
        $userRepository->method('findOneBy')->willReturn(null);

        $em = $this->createMock(EntityManagerInterface::class);
        $em->expects(self::once())->method('persist');

        $handler = new BlockEmail($userRepository, $em, 'test-secret');
        $handler(new BlockEmailCommand('unknown@example.com'));

        $this->addToAssertionCount(1);
    }
}
