如何在测试中加载表单类型 [英] How to load form types in tests

查看:82
本文介绍了如何在测试中加载表单类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?php
namespace Tabl\VenueBundle\Tests\Form;

use Symfony\Component\Form\Test\TypeTestCase;
use Tabl\VenueBundle\Entity\Venue;
use Tabl\VenueBundle\Form\VenueType;

class VenueTypeTest extends TypeTestCase
{

    public function testSubmitValidData() {
        $formData = array(
            'title' => 'Hello World',
        );

        $type = new VenueType();
        $form = $this->factory->create($type);

        $object = new Venue();
        $object->setTitle('Hello World');

        // submit the data to the form directly
        $form->submit($formData);

        $this->assertTrue($form->isSynchronized());
        $this->assertEquals($object, $form->getData());

        $view = $form->createView();
        $children = $view->children;

        foreach (array_keys($formData) as $key) {
            $this->assertArrayHasKey($key, $children);
        }
    }

}

我不断收到此错误消息:

I keep getting this error message:

Symfony\Component\Form\Exception\InvalidArgumentException: Could not load type "places_autocomplete"

如何解决?如何加载此类型,以便可以在表单上执行功能测试?

How can this be fixed? How do I load this type so I could perform a functional test on form?

places_autocomplete的一部分象牙GMaps捆绑包.

VenueType.php:

VenueType.php:

namespace Acme\VenueBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Acme\VenueBundle\Entity\Attribute;
use Acme\VenueBundle\Form\AttributeType;

class VenueType extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title')
            ->add('address', 'places_autocomplete' , ['attr' => ['placeholder' => 'Start typing for suggestions'], 'label'=>'Location'])
            ->add('attributes', 'entity', array(
                'multiple'      => true,
                'expanded'      => true,
                'property'      => 'icon',
                'class'         => 'AcmeVenueBundle:Attribute',
            ))
            ->add('finish', 'submit');
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\VenueBundle\Entity\Venue'
        ));
    }

    public function getName()
    {
        return 'venue';
    }
}

推荐答案

您必须实现getExtensions方法,该方法可以构建在表单中使用的两种模拟表单类型:PlacesAutocompleteTypeEntityType

You must implements the getExtensions methods that build the two mocked form types used in the form: PlacesAutocompleteType and EntityType

此解决方案对我有用:

<?php


namespace Acme\DemoBundle\Tests\Form;


use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping\ClassMetadata;
use Ivory\GoogleMapBundle\Form\Type\PlacesAutocompleteType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\PreloadedExtension;
use Symfony\Component\Form\Test\TypeTestCase;
use Acme\DemoBundle\Entity\Venue;
use Acme\DemoBundle\Form\VenueType;

class VenueTypeTest extends TypeTestCase
{

public function testSubmitValidData() {
    $formData = array(
        'title' => 'Hello World',
    );

    $type = new VenueType();
    $form = $this->factory->create($type);

    $object = new Venue();
    $object->setTitle('Hello World');

    // submit the data to the form directly
    $form->submit($formData);

    $this->assertTrue($form->isSynchronized());
    $this->assertEquals($object, $form->getData());

    $view = $form->createView();
    $children = $view->children;

    foreach (array_keys($formData) as $key) {
        $this->assertArrayHasKey($key, $children);
    }
}


protected function getExtensions()
{

    // Mock the FormType: places_autocomplete

    // See Ivory\GoogleMapBundle\Tests\Form\Type\PlacesAutocompleteTypeTest
    $placesAutocompleteHelperMock = $this->getMockBuilder('Ivory\GoogleMap\Helper\Places\AutocompleteHelper')
        ->disableOriginalConstructor()
        ->getMock();

    $requestMock = $this->getMock('Symfony\Component\HttpFoundation\Request');
    $requestMock
        ->expects($this->any())
        ->method('getLocale')
        ->will($this->returnValue('en'));

    $placesAutocompleteType = new PlacesAutocompleteType(
        $placesAutocompleteHelperMock,
        $requestMock
    );

    // Mock the FormType: entity
    $mockEntityManager = $this->getMockBuilder('\Doctrine\ORM\EntityManager')
        ->disableOriginalConstructor()
        ->getMock();

    $mockRegistry = $this->getMockBuilder('Doctrine\Bundle\DoctrineBundle\Registry')
        ->disableOriginalConstructor()
        ->getMock();

    $mockRegistry->expects($this->any())->method('getManagerForClass')
        ->will($this->returnValue($mockEntityManager));

    $mockEntityManager ->expects($this->any())->method('getClassMetadata')
        ->withAnyParameters()
        ->will($this->returnValue(new ClassMetadata('entity')));

    $repo = $this->getMockBuilder('Doctrine\ORM\EntityRepository')
        ->disableOriginalConstructor()
        ->getMock();

    $mockEntityManager ->expects($this->any())->method('getRepository')
        ->withAnyParameters()
        ->will($this->returnValue($repo));

    $repo->expects($this->any())->method('findAll')
        ->withAnyParameters()
        ->will($this->returnValue(new ArrayCollection()));


    $entityType = new EntityType($mockRegistry);



    return array(new PreloadedExtension(array(
        'places_autocomplete' => $placesAutocompleteType,
        'entity' => $entityType,
    ), array()));
}
}

希望获得帮助. 让我知道.

Hope this help. Let me know.

这篇关于如何在测试中加载表单类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆