<?php

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Tests\Form;

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Site\ContactFormType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\PreloadedExtension;
use Symfony\Component\Form\Test\TypeTestCase;

final class ContactFormTypeTest extends TypeTestCase
{
    public function testItPreservesTheExistingRequiredFieldsContract(): void
    {
        $form = $this->factory->create(ContactFormType::class);

        self::assertTrue($form->get('lastname')->getConfig()->getRequired());
        self::assertFalse($form->get('firstname')->getConfig()->getRequired());
        self::assertFalse($form->get('object')->getConfig()->getRequired());
        self::assertTrue($form->get('email')->getConfig()->getRequired());
        self::assertTrue($form->get('phone')->getConfig()->getRequired());
        self::assertTrue($form->get('message')->getConfig()->getRequired());
    }

    protected function getExtensions(): array
    {
        $withoutCaptcha = new class extends AbstractTypeExtension {
            public function buildForm(FormBuilderInterface $builder, array $options): void
            {
                $builder->remove('captcha');
            }

            public static function getExtendedTypes(): iterable
            {
                yield ContactFormType::class;
            }
        };

        return [
            new PreloadedExtension([], [ContactFormType::class => [$withoutCaptcha]]),
        ];
    }
}
