【问题标题】:Testing Symfony2 Forms causes Could not load type "entity"测试 Symfony2 Forms 导致无法加载类型“实体”
【发布时间】:2013-05-03 09:26:59
【问题描述】:

我正在测试我为应用程序定义的表单类型。在测试表单类型期间,使用 symfony 的 TypeTestCase 类会出现一条消息“无法加载类型“实体””。我能做些什么来解决这个问题??

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

【问题讨论】:

  • 请提供相关代码。谢谢
  • 添加代码,抱歉忘记了!
  • 对我来说似乎没有任何错误,但肯定我们错过了什么......
  • 我怎样才能测试这个表单类型,可能使用模拟??

标签: forms testing symfony phpunit


【解决方案1】:

在测试我的一些自定义类型时,我已经遇到了同样的问题。

这是我解决的方法(通过模拟 EntityType),

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

class MyTypeTest extends TypeTestCase
{
    // ... 
}

然后,将preloaded extension 添加到您的form factory 以考虑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)    
}

最后,将 Entity Type 模拟添加到您的 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()));
}

但是,您可能需要...

模拟DoctrineType 在调用其默认构造函数时将其作为参数的registry,因为setDefaultOptions() 使用它(请记住EntityType 扩展DoctrineType)以考虑class 和@987654332 @你的Entity field的选项。

然后您可能需要按如下方式模拟 entityType:

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

【讨论】:

  • 您好,我按照您发布的内容进行操作,但出现错误:Call to protected Doctrine\ORM\EntityManager::__construct() from context 'PHPUnit_Framework_MockObject_Generator' in /usr/share/php/PHPUnit /Framework/MockObject/Generator.php 第 237 行
  • @Ahmed Siouani 你能回答上面的评论吗
  • 我得到与@skonsoft 相同的错误。请告诉我们可能的解决方案吗?
【解决方案2】:

Ahmed Siouani 的回答写得很好,让我了解如何在TypeTestCase 中添加Extension

但是如果你想做一个比单元测试简单得多的集成测试,你可以这样做:

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

如本文档所述:http://symfony.com/doc/current/cookbook/form/unit_testing.html#adding-a-type-your-form-depends-on

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-19
    • 2013-08-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2023-03-09
    • 2015-01-29
    相关资源
    最近更新 更多