Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

php - How to load form types in tests

<?php
namespace TablVenueBundleTestsForm;

use SymfonyComponentFormTestTypeTestCase;
use TablVenueBundleEntityVenue;
use TablVenueBundleFormVenueType;

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:

SymfonyComponentFormExceptionInvalidArgumentException: 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 is a part of the Ivory GMaps Bundle.

VenueType.php:

namespace AcmeVenueBundleForm;

use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolverInterface;
use SymfonyBundleFrameworkBundleRoutingRouter;
use AcmeVenueBundleEntityAttribute;
use AcmeVenueBundleFormAttributeType;

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' => 'AcmeVenueBundleEntityVenue'
        ));
    }

    public function getName()
    {
        return 'venue';
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

This solutions work for me:

<?php


namespace AcmeDemoBundleTestsForm;


use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMappingClassMetadata;
use IvoryGoogleMapBundleFormTypePlacesAutocompleteType;
use SymfonyBridgeDoctrineFormTypeEntityType;
use SymfonyComponentFormPreloadedExtension;
use SymfonyComponentFormTestTypeTestCase;
use AcmeDemoBundleEntityVenue;
use AcmeDemoBundleFormVenueType;

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 IvoryGoogleMapBundleTestsFormTypePlacesAutocompleteTypeTest
    $placesAutocompleteHelperMock = $this->getMockBuilder('IvoryGoogleMapHelperPlacesAutocompleteHelper')
        ->disableOriginalConstructor()
        ->getMock();

    $requestMock = $this->getMock('SymfonyComponentHttpFoundationRequest');
    $requestMock
        ->expects($this->any())
        ->method('getLocale')
        ->will($this->returnValue('en'));

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

    // Mock the FormType: entity
    $mockEntityManager = $this->getMockBuilder('DoctrineORMEntityManager')
        ->disableOriginalConstructor()
        ->getMock();

    $mockRegistry = $this->getMockBuilder('DoctrineBundleDoctrineBundleRegistry')
        ->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('DoctrineORMEntityRepository')
        ->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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...