<?php

declare(strict_types=1);

namespace Ferienpass\AdminBundle\Tests\Form\Extension;

use Ferienpass\AdminBundle\Form\Extension\Type\OrderExtension;
use Ferienpass\AdminBundle\Form\Extension\Type\SectionExtension;
use Ferienpass\AdminBundle\Form\Extension\Type\WidthExtension;
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 FormTypeExtensionsTest extends TestCase
{
    // ---- SectionExtension ----

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

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

        $ext->buildView($view, $form, ['section' => 'my_section']);

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

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

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

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

    public function testSectionExtensionConfigureOptionsDefinesSectionOption(): void
    {
        $ext = new SectionExtension();
        $resolver = new OptionsResolver();

        $ext->configureOptions($resolver);

        // 'section' should be defined (no exception when set to string)
        $resolved = $resolver->resolve(['section' => 'test']);
        self::assertSame('test', $resolved['section']);
    }

    // ---- WidthExtension ----

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

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

        $ext->buildView($view, $form, ['width' => 6]);

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

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

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

        self::assertArrayNotHasKey('width', $view->vars);
    }

    // ---- OrderExtension ----

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

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

        $ext->buildView($view, $form, ['order' => 5]);

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

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

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

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