<?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\CmsBundle\Tests\Dto;

use Ferienpass\CmsBundle\Dto\AccessCodeDto;
use Ferienpass\CoreBundle\Entity\AccessCode;
use Ferienpass\CoreBundle\Entity\AccessCodeStrategy;
use PHPUnit\Framework\TestCase;

final class AccessCodeDtoTest extends TestCase
{
    public function testGetAccessCodeStrategyReturnsStrategy(): void
    {
        $strategy = new AccessCodeStrategy();
        $dto = new AccessCodeDto($strategy);

        self::assertSame($strategy, $dto->getAccessCodeStrategy());
    }

    public function testGetCodeIsNullByDefault(): void
    {
        $strategy = new AccessCodeStrategy();
        $dto = new AccessCodeDto($strategy);

        self::assertNull($dto->getCode());
    }

    public function testSetCodeAndGetCode(): void
    {
        $strategy = new AccessCodeStrategy();
        $dto = new AccessCodeDto($strategy, 'ABC123');

        self::assertSame('ABC123', $dto->getCode());
    }

    public function testSetCodeUpdatesCode(): void
    {
        $strategy = new AccessCodeStrategy();
        $dto = new AccessCodeDto($strategy);
        $dto->setCode('XYZ789');

        self::assertSame('XYZ789', $dto->getCode());
    }

    public function testToEntityReturnsNullWhenCodeNotFound(): void
    {
        $strategy = new AccessCodeStrategy();
        $dto = new AccessCodeDto($strategy, 'NONEXISTENT');

        self::assertNull($dto->toEntity());
    }

    public function testToEntityReturnsAccessCodeWhenFound(): void
    {
        $strategy = new AccessCodeStrategy();
        $code = $this->createMock(AccessCode::class);
        $code->method('getCode')->willReturn('VALID123');
        $strategy->addCode($code);

        $dto = new AccessCodeDto($strategy, 'VALID123');

        self::assertSame($code, $dto->toEntity());
    }
}
