Testing Symfony2 Forms causes Could not load type “entity”

前端 未结 2 1960
日久生厌
日久生厌 2021-02-03 12:21

I am testing a Form Type I defined for an application. During testing the form type, using symfony\'s TypeTestCase class a message \"Could not load type \"entity\"\" appears. Wh

相关标签:
2条回答
  • 2021-02-03 12:47

    I already got the same problem when testing some of my customized Types.

    Here's the way I figure it out (by mocking EntityType),

    First, make sure your test class extends TypeTestCase,

    class MyTypeTest extends TypeTestCase
    {
        // ... 
    }
    

    Then, add a preloaded extension to your form factory in order to take into account the EntityType

    protected function setUp()
    {
        parent::setUp();
    
        $this->factory = Forms::createFormFactoryBuilder()
          ->addExtensions($this->getExtensions())
          ->getFormFactory();
    }
    // Where this->getExtensions() returns the EntityType preloaded extension 
    // (see the last step)    
    }
    

    And finally, add an Entity Type mock to your preloaded extension.

    protected function getExtensions()
    {
        $mockEntityType = $this->getMockBuilder('Symfony\Bridge\Doctrine\Form\Type\EntityType')
            ->disableOriginalConstructor()
            ->getMock();
    
        $mockEntityType->expects($this->any())->method('getName')
                       ->will($this->returnValue('entity'));
    
        return array(new PreloadedExtension(array(
                $mockEntityType->getName() => $mockEntityType,
        ), array()));
    }
    

    But, you may need to ...

    Mock the registry that DoctrineType takes as parameter when calling its default constructor because it's used by setDefaultOptions() (Keep in mind that EntityType extends DoctrineType) to take into account class and property options of your Entity field.

    Your may then need to mock the entityType as follow:

    $mockEntityManager = $this->getMockBuilder('\Doctrine\ORM\EntityManager')->getMock();
    
    $mockRegistry = $this->getMockBuilder('Doctrine\Bundle\DoctrineBundle\Registry')
        ->disableOriginalConstructor()
        ->setMethods(array('getManagerForClass'))
        ->getMock();
    
    $mockRegistry->expects($this->any())->method('getManagerForClass')
                 ->will($this->returnValue($mockEntityManager));
    
    $mockEntityType = $this->getMockBuilder('Symfony\Bridge\Doctrine\Form\Type\EntityType')
        ->setMethods(array('getName'))
        ->setConstructorArgs(array($mockRegistry))
        ->getMock();
    
    $mockEntityType->expects($this->any())->method('getName')
                   ->will($this->returnValue('entity'));
    
    0 讨论(0)
  • 2021-02-03 12:52

    The Ahmed Siouani's answer is well writted and allowed me to understand how to add an Extension in the TypeTestCase.

    But if you want to make an Intergration test, who is much simpler than a Unit test in this case, you can do as follow:

    protected function getExtensions()
    {
        $childType = new TestChildType();
        return array(new PreloadedExtension(array(
            $childType->getName() => $childType,
        ), array()));
    }
    

    As described in this documentation: http://symfony.com/doc/current/cookbook/form/unit_testing.html#adding-a-type-your-form-depends-on

    0 讨论(0)
提交回复
热议问题