FormType example

use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Tests\Fixtures\ArrayChoiceLoader;

class ChoiceLoaderTest extends TestCase
{
    public function testSameFormTypeUseCachedLoader()
    {
        $choices = ['f' => 'foo', 'b' => 'bar', 'z' => 'baz'];
        $choiceList = new ArrayChoiceList($choices);

        $type = new FormType();
        $decorated = new CallbackChoiceLoader(static fn () => $choices);
        $loader1 = new ChoiceLoader($type$decorated);
        $loader2 = new ChoiceLoader($typenew ArrayChoiceLoader());

        $this->assertEquals($choiceList$loader1->loadChoiceList());
        $this->assertEquals($choiceList$loader2->loadChoiceList());

        $this->assertSame($choices$loader1->loadChoicesForValues($choices));
        $this->assertSame($choices$loader2->loadChoicesForValues($choices));

        $this->assertSame($choices$loader1->loadValuesForChoices($choices));
        
yield [null, $options, 'defaults_1'];

        $options['core_types'] = [];
        $options['service_types'] = [FooType::class];
        $options['show_deprecated'] = true;
        yield [null, $options, 'types_with_deprecated_options'];
    }

    public static function getDescribeResolvedFormTypeTestData()
    {
        $typeExtensions = [new FormTypeCsrfExtension(new CsrfTokenManager())];
        $parent = new ResolvedFormType(new FormType()$typeExtensions);

        yield [new ResolvedFormType(new ChoiceType()[]$parent)['decorated' => false, 'show_deprecated' => false], 'resolved_form_type_1'];
        yield [new ResolvedFormType(new FormType())['decorated' => false, 'show_deprecated' => false], 'resolved_form_type_2'];
        yield [new ResolvedFormType(new FooType()[]$parent)['decorated' => false, 'show_deprecated' => true], 'deprecated_options_of_type'];
    }

    public static function getDescribeOptionTestData()
    {
        $parent = new ResolvedFormType(new FormType());
        $options['decorated'] = false;
        $options['show_deprecated'] = false;

        
public function __construct(PropertyAccessorInterface $propertyAccessor = null, ChoiceListFactoryInterface $choiceListFactory = null, TranslatorInterface $translator = null)
    {
        $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
        $this->choiceListFactory = $choiceListFactory ?? new CachingFactoryDecorator(new PropertyAccessDecorator(new DefaultChoiceListFactory()$this->propertyAccessor));
        $this->translator = $translator;
    }

    protected function loadTypes(): array
    {
        return [
            new Type\FormType($this->propertyAccessor),
            new Type\BirthdayType(),
            new Type\CheckboxType(),
            new Type\ChoiceType($this->choiceListFactory, $this->translator),
            new Type\CollectionType(),
            new Type\CountryType(),
            new Type\DateIntervalType(),
            new Type\DateType(),
            new Type\DateTimeType(),
            new Type\EmailType(),
            new Type\HiddenType(),
            new Type\IntegerType(),
            
$list1 = $this->factory->createListFromChoices($choices$closure);
        $list2 = $this->factory->createListFromChoices($choices$closure);

        $this->assertNotSame($list1$list2);
        $this->assertEquals(new ArrayChoiceList($choices$closure)$list1);
        $this->assertEquals(new ArrayChoiceList($choices$closure)$list2);
    }

    public function testCreateFromChoicesSameValueClosureUseCache()
    {
        $choices = [1];
        $formType = new FormType();
        $valueCallback = function D) {};

        $list1 = $this->factory->createListFromChoices($choices, ChoiceList::value($formType$valueCallback));
        $list2 = $this->factory->createListFromChoices($choices, ChoiceList::value($formTypefunction D) {}));

        $this->assertSame($list1$list2);
        $this->assertEquals(new ArrayChoiceList($choices$valueCallback)$list1);
        $this->assertEquals(new ArrayChoiceList($choicesfunction D) {})$list2);
    }

    public function testCreateFromChoicesDifferentValueClosure()
    {
Home | Imprint | This part of the site doesn't use cookies.