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

use Ferienpass\CoreBundle\Form\Extension\AutoSubmitExtension;
use Ferienpass\CoreBundle\Form\Extension\UiThemeExtension;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class FormExtensionsTest extends TestCase
{
    // ---- AutoSubmitExtension ----

    public function testAutoSubmitExtendsFormType(): void
    {
        self::assertContains(FormType::class, iterator_to_array(AutoSubmitExtension::getExtendedTypes()));
    }

    public function testAutoSubmitBuildViewSetsTrueWhenEnabled(): void
    {
        $ext = new AutoSubmitExtension();
        $view = new FormView();
        $form = $this->createMock(FormInterface::class);

        $ext->buildView($view, $form, ['auto_submit' => true]);

        self::assertTrue($view->vars['auto_submit']);
    }

    public function testAutoSubmitBuildViewDefaultsFalseWhenMissing(): void
    {
        $ext = new AutoSubmitExtension();
        $view = new FormView();
        $form = $this->createMock(FormInterface::class);

        $ext->buildView($view, $form, []);

        self::assertFalse($view->vars['auto_submit']);
    }

    public function testAutoSubmitConfigureOptionsDefinesOption(): void
    {
        $ext = new AutoSubmitExtension();
        $resolver = new OptionsResolver();

        $ext->configureOptions($resolver);

        $resolved = $resolver->resolve(['auto_submit' => true]);
        self::assertTrue($resolved['auto_submit']);
    }

    // ---- UiThemeExtension ----

    public function testUiThemeExtendsFormType(): void
    {
        self::assertContains(FormType::class, iterator_to_array(UiThemeExtension::getExtendedTypes()));
    }

    public function testUiThemeBuildViewSetsUiOption(): void
    {
        $ext = new UiThemeExtension();
        $view = new FormView();
        $form = $this->createMock(FormInterface::class);

        $ext->buildView($view, $form, ['ui' => 'compact']);

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

    public function testUiThemeBuildViewDefaultsNullWhenMissing(): void
    {
        $ext = new UiThemeExtension();
        $view = new FormView();
        $form = $this->createMock(FormInterface::class);

        $ext->buildView($view, $form, []);

        self::assertNull($view->vars['ui']);
    }
}
