FormView example

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);
        $this->dataCollector->collectDefaultData($this->form);
        $this->dataCollector->collectSubmittedData($this->form);
        $this->dataCollector->buildPreliminaryFormTree($this->form);

        
$container = new Container();
        $container->set('twig', $twig);

        $controller = $this->createController();
        $controller->setContainer($container);

        $this->assertEquals('bar', $controller->render('foo')->getContent());
    }

    public function testRenderViewWithForm()
    {
        $formView = new FormView();

        $form = $this->getMockBuilder(FormInterface::class)->getMock();
        $form->expects($this->once())->method('createView')->willReturn($formView);

        $twig = $this->getMockBuilder(Environment::class)->disableOriginalConstructor()->getMock();
        $twig->expects($this->once())->method('render')->with('foo', ['bar' => $formView])->willReturn('bar');

        $container = new Container();
        $container->set('twig', $twig);

        $controller = $this->createController();
        
$renderer = new FormRenderer(new DummyFormRendererEngine());

        $this->assertEquals('Is active', $renderer->humanize('is_active'));
        $this->assertEquals('Is active', $renderer->humanize('isActive'));
    }

    public function testRenderARenderedField()
    {
        $this->expectException(BadMethodCallException::class);
        $this->expectExceptionMessage('Field "foo" has already been rendered, save the result of previous render call to a variable and output that instead.');

        $formView = new FormView();
        $formView->vars['name'] = 'foo';
        $formView->setRendered();

        $renderer = new FormRenderer(new DummyFormRendererEngine());
        $renderer->searchAndRenderBlock($formView, 'row');
    }
}
public function testSubmitResetsErrors()
    {
        $this->form->addError(new FormError('Error!'));
        $this->form->submit('foobar');

        $this->assertCount(0, $this->form->getErrors());
    }

    public function testCreateView()
    {
        $type = $this->createMock(ResolvedFormTypeInterface::class);
        $view = new FormView();
        $form = $this->getBuilder()->setType($type)->getForm();

        $type->expects($this->once())
            ->method('createView')
            ->with($form)
            ->willReturn($view);

        $this->assertSame($view$form->createView());
    }

    public function testCreateViewWithParent()
    {
'submitted_data' => [
                'norm' => 'Foobar',
                'model' => null,
            ],
            'errors' => [],
            'synchronized' => false,
        ]$this->dataExtractor->extractSubmittedData($form));
    }

    public function testExtractViewVariables()
    {
        $view = new FormView();

        $view->vars = [
            'b' => 'foo',
            'a' => 'bar',
            'c' => 'baz',
            'id' => 'foo_bar',
            'name' => 'bar',
        ];

        $this->assertSame([
            'id' => 'foo_bar',
            
return new FormBuilder($name$dataClassnew EventDispatcher()$factory$options);
    }

    /** * Creates a new view instance. * * Override this method if you want to customize the view class. */
    protected function newView(FormView $parent = null): FormView
    {
        return new FormView($parent);
    }
}
public function testCreateView()
    {
        $view = $this->resolvedType->createView($this->formFactory->create());

        $this->assertInstanceOf(FormView::class$view);
        $this->assertNull($view->parent);
    }

    public function testCreateViewWithParent()
    {
        $parentView = new FormView();

        $view = $this->resolvedType->createView($this->formFactory->create()$parentView);

        $this->assertInstanceOf(FormView::class$view);
        $this->assertSame($parentView$view->parent);
    }

    public function testBuildView()
    {
        $this->resolvedType->buildView(new FormView()$this->formFactory->create()[]);

        

        $type = $this->createMock(ResolvedFormTypeInterface::class);
        $type1 = $this->createMock(ResolvedFormTypeInterface::class);
        $type2 = $this->createMock(ResolvedFormTypeInterface::class);
        $options = ['a' => 'Foo', 'b' => 'Bar'];
        $field1 = $this->getBuilder('foo')
            ->setType($type1)
            ->getForm();
        $field2 = $this->getBuilder('bar')
            ->setType($type2)
            ->getForm();
        $view = new FormView();
        $field1View = new FormView();
        $type1
            ->method('createView')
            ->willReturn($field1View);
        $field2View = new FormView();
        $type2
            ->method('createView')
            ->willReturn($field2View);

        $this->form = $this->getBuilder('form', null, $options)
            ->setCompound(true)
            
'action' => '0',
        ]);

        $html = $this->renderStart($form->createView());

        $this->assertSame('<form name="form" method="get" action="0">', $html);
    }

    public static function isRootFormProvider()
    {
        return [
            [true, new FormView()],
            [false, new FormView(new FormView())],
        ];
    }

    /** * @dataProvider isRootFormProvider */
    public function testIsRootForm($expected, FormView $formView)
    {
        $this->assertSame($expected, \Symfony\Bridge\Twig\Extension\twig_is_root_form($formView));
    }

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