How to load form types in tests

前端 未结 3 1278
长发绾君心
长发绾君心 2020-12-19 09:14


        
3条回答
  •  不知归路
    2020-12-19 09:52

    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:

     '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.

提交回复
热议问题