<?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);
}
}
}
我不断收到此错误消息:
Symfony\Component\Form\Exception\InvalidArgumentException: Could not load type "places_autocomplete"
如何解决?如何加载此类型,以便可以对表单执行功能测试?
places_autocomplete
是Ivory GMaps Bundle的一部分。
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
用于构建表单中使用的两种模拟表单类型的方法:PlacesAutocompleteType
和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()));
}
}
希望对您有所帮助。让我知道。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句