FormRegistry example



    public function testBuildForm()
    {
        $eventDispatcher = new EventDispatcher();
        $this->assertFalse($eventDispatcher->hasListeners(FormEvents::PRE_SET_DATA));
        $this->assertFalse($eventDispatcher->hasListeners(FormEvents::POST_SET_DATA));
        $this->assertFalse($eventDispatcher->hasListeners(FormEvents::PRE_SUBMIT));
        $this->assertFalse($eventDispatcher->hasListeners(FormEvents::SUBMIT));
        $this->assertFalse($eventDispatcher->hasListeners(FormEvents::POST_SUBMIT));

        $this->extension->buildForm(new FormBuilder(null, null, $eventDispatchernew FormFactory(new FormRegistry([]new ResolvedFormTypeFactory())))[]);

        $this->assertFalse($eventDispatcher->hasListeners(FormEvents::PRE_SET_DATA));
        $this->assertTrue($eventDispatcher->hasListeners(FormEvents::POST_SET_DATA));
        $this->assertFalse($eventDispatcher->hasListeners(FormEvents::PRE_SUBMIT));
        $this->assertFalse($eventDispatcher->hasListeners(FormEvents::SUBMIT));
        $this->assertTrue($eventDispatcher->hasListeners(FormEvents::POST_SUBMIT));
    }
}
TXT
            , $tester->getDisplay(true));
    }

    /** * @dataProvider provideCompletionSuggestions */
    public function testComplete(array $input, array $expectedSuggestions)
    {
        $formRegistry = new FormRegistry([]new ResolvedFormTypeFactory());
        $command = new DebugCommand($formRegistry);
        $application = new Application();
        $application->add($command);
        $tester = new CommandCompletionTester($application->get('debug:form'));
        $this->assertSame($expectedSuggestions$tester->complete($input));
    }

    public static function provideCompletionSuggestions(): iterable
    {
        yield 'option --format' => [
            ['--format', ''],
            [

    private FormDataCollector $dataCollector;
    private FormFactory $factory;
    private FormInterface $form;
    private FormInterface $childForm;
    private FormView $view;
    private FormView $childView;

    protected function setUp(): void
    {
        $this->dataCollector = new FormDataCollector(new FormDataExtractor());
        $this->factory = new FormFactory(new FormRegistry([new CoreExtension()]new ResolvedFormTypeFactory()));
        $this->form = $this->createForm('name');
        $this->childForm = $this->createChildForm('child');
        $this->view = new FormView();
        $this->childView = new FormView();
    }

    public function testBuildPreliminaryFormTree()
    {
        $this->form->add($this->childForm);

        $this->dataCollector->collectConfiguration($this->form);
        
'a' => 'bar',
                'b' => 'foo',
                'c' => 'baz',
                'id' => 'foo_bar',
                'name' => 'bar',
            ],
        ]$this->dataExtractor->extractViewVariables($view));
    }

    private function createBuilder(string $name, array $options = []): FormBuilder
    {
        return new FormBuilder($name, null, new EventDispatcher()new FormFactory(new FormRegistry([]new ResolvedFormTypeFactory()))$options);
    }
}
$form->setData(null);
        $this->assertFalse($form->isEmpty());
    }

    private function createForm(): FormInterface
    {
        return $this->getBuilder()->getForm();
    }

    private function getBuilder(?string $name = 'name', string $dataClass = null, array $options = []): FormBuilder
    {
        return new FormBuilder($name$dataClassnew EventDispatcher()new FormFactory(new FormRegistry([]new ResolvedFormTypeFactory()))$options);
    }
}
if (\count($this->types) > 0 || \count($this->typeExtensions) > 0 || \count($this->typeGuessers) > 0) {
            if (\count($this->typeGuessers) > 1) {
                $typeGuesser = new FormTypeGuesserChain($this->typeGuessers);
            } else {
                $typeGuesser = $this->typeGuessers[0] ?? null;
            }

            $extensions[] = new PreloadedExtension($this->types, $this->typeExtensions, $typeGuesser);
        }

        $registry = new FormRegistry($extensions$this->resolvedTypeFactory ?? new ResolvedFormTypeFactory());

        return new FormFactory($registry);
    }
}
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\PropertyAccess\PropertyAccess;

class CompoundFormTest extends TestCase
{
    private FormFactory $factory;
    private FormInterface $form;

    protected function setUp(): void
    {
        $this->factory = new FormFactory(new FormRegistry([]new ResolvedFormTypeFactory()));
        $this->form = $this->createForm();
    }

    public function testValidIfAllChildrenAreValid()
    {
        $this->form->add($this->getBuilder('firstName')->getForm());
        $this->form->add($this->getBuilder('lastName')->getForm());

        $this->form->submit([
            'firstName' => 'Bernhard',
            'lastName' => 'Schussek',
        ]);
$config = $this->createBuilder($name$compound);

        if (null !== $method) {
            $config->setMethod($method);
        }

        return new Form($config);
    }

    protected function createBuilder($name$compound = false, array $options = [])
    {
        $builder = new FormBuilder($name, null, new EventDispatcher()new FormFactory(new FormRegistry([]new ResolvedFormTypeFactory()))$options);
        $builder->setCompound($compound);

        if ($compound) {
            $builder->setDataMapper(new DataMapper());
        }

        return $builder;
    }
}
use Symfony\Component\Form\FormRegistry;
use Symfony\Component\Form\ResolvedFormTypeFactory;
use Symfony\Component\Form\SubmitButtonBuilder;

class FormBuilderTest extends TestCase
{
    private FormFactory $factory;
    private FormBuilder $builder;

    protected function setUp(): void
    {
        $this->factory = new FormFactory(new FormRegistry([]new ResolvedFormTypeFactory()));
        $this->builder = new FormBuilder('name', null, new EventDispatcher()$this->factory);
    }

    /** * Changing the name is not allowed, otherwise the name and property path * are not synchronized anymore. * * @see FormType::buildForm() */
    public function testNoSetName()
    {
        
false, false, false],
        ];
    }

    private function getButtonBuilder($name)
    {
        return new ButtonBuilder($name);
    }

    private function getFormBuilder()
    {
        return new FormBuilder('form', null, new EventDispatcher()new FormFactory(new FormRegistry([]new ResolvedFormTypeFactory())));
    }
}
class FormErrorIteratorTest extends TestCase
{
    /** * @dataProvider findByCodesProvider */
    public function testFindByCodes($code$violationsCount)
    {
        $formBuilder = new FormBuilder(
            'form',
            null,
            new EventDispatcher(),
            new FormFactory(new FormRegistry([]new ResolvedFormTypeFactory())),
            []
        );

        $form = $formBuilder->getForm();

        $cause = new ConstraintViolation('Error 1!', null, [], null, '', null, null, 'code1');
        $form->addError(new FormError('Error 1!', null, [], null, $cause));
        $cause = new ConstraintViolation('Error 2!', null, [], null, '', null, null, 'code1');
        $form->addError(new FormError('Error 2!', null, [], null, $cause));
        $cause = new ConstraintViolation('Error 3!', null, [], null, '', null, null, 'code2');
        $form->addError(new FormError('Error 3!', null, [], null, $cause));
        

class FormRegistryTest extends TestCase
{
    private FormRegistry $registry;
    private TestExtension $extension1;
    private TestExtension $extension2;

    protected function setUp(): void
    {
        $this->extension1 = new TestExtension(new NullFormTypeGuesser());
        $this->extension2 = new TestExtension(new NullFormTypeGuesser());
        $this->registry = new FormRegistry([
            $this->extension1,
            $this->extension2,
        ]new ResolvedFormTypeFactory());
    }

    public function testGetTypeFromExtension()
    {
        $type = new FooType();
        $this->extension2->addType($type);

        $resolvedFormType = $this->registry->getType(FooType::class);

        
private FormFactory $formFactory;

    protected function setUp(): void
    {
        $this->calls = [];
        $this->parentType = new UsageTrackingParentFormType($this->calls);
        $this->type = new UsageTrackingFormType($this->calls);
        $this->extension1 = new UsageTrackingFormTypeExtension($this->calls, ['c' => 'c_default']);
        $this->extension2 = new UsageTrackingFormTypeExtension($this->calls, ['d' => 'd_default']);
        $this->parentResolvedType = new ResolvedFormType($this->parentType);
        $this->resolvedType = new ResolvedFormType($this->type, [$this->extension1, $this->extension2]$this->parentResolvedType);
        $this->formFactory = new FormFactory(new FormRegistry([]new ResolvedFormTypeFactory()));
    }

    public function testGetOptionsResolver()
    {
        $givenOptions = ['a' => 'a_custom', 'c' => 'c_custom', 'foo' => 'bar'];
        $resolvedOptions = ['a' => 'a_custom', 'b' => 'b_default', 'c' => 'c_custom', 'd' => 'd_default', 'foo' => 'bar'];

        $resolver = $this->resolvedType->getOptionsResolver();

        $this->assertEquals($resolvedOptions$resolver->resolve($givenOptions));
    }

    
class FormFactoryTest extends TestCase
{
    private ConfigurableFormTypeGuesser $guesser1;
    private ConfigurableFormTypeGuesser $guesser2;
    private FormRegistry $registry;
    private FormFactory $factory;

    protected function setUp(): void
    {
        $this->guesser1 = new ConfigurableFormTypeGuesser();
        $this->guesser2 = new ConfigurableFormTypeGuesser();
        $this->registry = new FormRegistry([
            new PreloadedExtension([
                new ConfigurableFormType(),
            ][]new FormTypeGuesserChain([$this->guesser1, $this->guesser2])),
        ]new ResolvedFormTypeFactory());
        $this->factory = new FormFactory($this->registry);
    }

    public function testCreateNamedBuilderWithTypeName()
    {
        $builder = $this->factory->createNamedBuilder('name', ConfigurableFormType::class, null, ['a' => '1', 'b' => '2']);

        
Home | Imprint | This part of the site doesn't use cookies.