测试Symfony2表单导致无法加载类型“实体". [英] Testing Symfony2 Forms causes Could not load type "entity"

查看:80
本文介绍了测试Symfony2表单导致无法加载类型“实体".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试为应用程序定义的表单类型.在测试表单类型期间,使用symfony的TypeTestCase类,将显示一条消息无法加载类型实体"".我该怎么办才能解决问题?

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. What can I do to solve the problem??

class MyType extends AbstractType {
  public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder->add('otherType', 'entity', array('class' => 'Bundle:OtherType'));
  }
}

class MyTypeTest extends TypeTestCase {
  public function testSth() {
    $type = new MyType();
  }
}

推荐答案

在测试某些自定义类型时,我已经遇到了相同的问题.

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

这是我弄清楚的方式(通过模拟EntityType)

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

首先,确保您的测试类扩展 TypeTestCase

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()));
}

但是,您可能需要...

嘲笑注册表,即 EntityType 扩展了 DoctrineType )考虑到属性选项href ="http://symfony.com/doc/current/reference/forms/types/ent ity.html"rel =" noreferrer>实体字段.

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'));

这篇关于测试Symfony2表单导致无法加载类型“实体".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆