2013-05-03 71 views
8

我正在測試爲應用程序定義的表單類型。在測試表單類型的過程中,使用symfony的TypeTestCase類會出現消息「無法加載類型」實體「」。我能做些什麼來解決問題?測試Symfony2表單的原因無法加載類型「實體」

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

請,提供相關的代碼。謝謝 – DonCallisto 2013-05-03 09:29:19

+0

添加代碼,忘記它吧! – Benny 2013-05-03 09:36:01

+0

對我來說似乎沒有任何錯誤,但肯定我們錯過了一些東西... – DonCallisto 2013-05-03 09:45:17

回答

13

我在測試一些自定義類型時已經遇到同樣的問題。

這裏是我弄明白(由嘲諷的EntityType)的方式,

首先,確保你的測試類擴展TypeTestCase

class MyTypeTest extends TypeTestCase 
{ 
    // ... 
} 

然後,添加一個preloaded extensionform 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())); 
} 

但是,你可能需要...

嘲笑registryDoctrineType調用它的默認構造函數時,因爲它使用的setDefaultOptions()需要的參數(請記住,EntityType延伸DoctrineType)考慮您的Entity fieldclassproperty選項。

你可能會需要嘲笑的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')); 
+13

你好,我跟着你發佈的,但是,我得到一個錯誤:調用受保護的Doctrine \ ORM \ EntityManager :: __構造()從上下文'PHPUnit_Framework_MockObject_Generator'在/ usr/share/php/PHPUnit/Framework/MockObject/Generator。 php 237行 – skonsoft 2013-12-23 19:57:46

+0

@Ahmed Siouani你能否回答上述評論 – 2016-12-28 14:58:29

+0

我收到與@skonsoft相同的錯誤。你能告訴我們可能的解決方案嗎? – 2017-01-10 06:10:28

0

艾哈邁德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

相關問題