<?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
входит в состав набора Jawk .
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
форм, используемых в форме: 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())); } }
Надеюсь, эта помощь. Дай мне знать.
Это в дополнение к решению @ Matteo – когда вы используете Mockery , вы можете получить EntityType:
$mockEntityManager = \Mockery::mock('\Doctrine\ORM\EntityManager'); $mockRegistry = \Mockery::mock('Doctrine\Bundle\DoctrineBundle\Registry'); $mockRegistry->shouldReceive('getManagerForClass')->andReturn($mockEntityManager); $mockEntityManager->shouldReceive('getClassMetadata')->andReturn(new ClassMetadata('entity')); $repo = \Mockery::mock('Doctrine\ORM\EntityRepository'); $mockEntityManager->shouldReceive('getRepository')->andReturn($repo); $repo->shouldReceive('findAll')->andReturn(new ArrayCollection()); $entityType = new EntityType($mockRegistry);