<?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\Form\SimpleType;

use Contao\CoreBundle\Csrf\ContaoCsrfTokenManager;
use Ferienpass\CmsBundle\Form\SimpleType\ContaoRequestTokenType;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;

final class ContaoRequestTokenTypeTest extends TestCase
{
    public function testGetParentReturnsHiddenType(): void
    {
        $manager = $this->createMock(ContaoCsrfTokenManager::class);

        self::assertSame(HiddenType::class, new ContaoRequestTokenType($manager)->getParent());
    }

    public function testBuildViewSetsFullNameToRequestToken(): void
    {
        $manager = $this->createMock(ContaoCsrfTokenManager::class);
        $manager->method('getDefaultTokenValue')->willReturn('tok123');

        $view = new FormView();
        $form = $this->createMock(FormInterface::class);

        new ContaoRequestTokenType($manager)->buildView($view, $form, []);

        self::assertSame('REQUEST_TOKEN', $view->vars['full_name']);
    }

    public function testBuildViewSetsValueFromTokenManager(): void
    {
        $manager = $this->createMock(ContaoCsrfTokenManager::class);
        $manager->method('getDefaultTokenValue')->willReturn('tok123');

        $view = new FormView();
        $form = $this->createMock(FormInterface::class);

        new ContaoRequestTokenType($manager)->buildView($view, $form, []);

        self::assertSame('tok123', $view->vars['value']);
    }
}
