2012-10-22 43 views
3

我想在自定義表單類型的綁定事件上測試方法。關於綁定事件的Symfony 2.1表單元測試

下面是代碼

public function bind(DataEvent $event) 
{ 
    $form = $event->getForm(); 

    if($form->getNormData() instanceof BaseFileInterface && !$event->getData() instanceof UploadedFile) { 
     $event->setData($form->getNormData()); 
    } 

    if($event->getData() instanceof UploadedFile) { 
     $hander = $this->handlerManager->getHandler(
      $form->getParent()->getConfig()->getDataClass(), 
      $form->getName() 
     ); 

     $datas = $hander->createBaseFile($event->getData()); 
     $event->setData($datas); 
    } 

    if(is_string($event->getData())) { 
     $event->setData(null); 
    } 
} 

和類型的表單生成器:

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->addEventSubscriber(new BaseFileListener($this->handlerManager)) 
     ->addViewTransformer(new BaseFileToStringTransformer()) 
    ; 
} 

我的測試類從Symfony的\分量\表格\測試繼承\ FormIntegrationTestCase並且這樣做:

protected function setUp() 
{ 
    parent::setUp(); 

    $this->handlerManager = $this->getHandlerManagerMock(); 

    $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); 
    $this->builder = new FormBuilder(null, null, $this->dispatcher, $this->factory); 

    $this->form = $this->factory->create('my_file'); 
    $this->form->setParent($this->getFormMock())->setData(new DummyEntity()); 
} 

protected function getFormMock() 
{ 

    $mock = $this 
     ->getMockBuilder('Symfony\Component\Form\Tests\FormInterface') 
     ->disableOriginalConstructor() 
     ->getMock() 
    ; 

    return $mock; 
} 

public function testBindHandleNewFileWithNonEmptyField() 
{ 
    $data = $file = new UploadedFile(
     __DIR__.'/../Fixtures/test.gif', 
     'original.gif', 
     'image/gif', 
     filesize(__DIR__.'/../Fixtures/test.gif'), 
     null 
    ); 

    $event = new FormEvent($this->form, $data); 

    $listener = new BaseFileListener($this->handlerManager); 
    $listener->bind($event); 

    $this->assertInstanceOf('My\FooBundle\Entity\BaseFileInterface', $event->getData()); 
    $this->assertNotEquals($event->getData(), $this->form->getNormData()); 
} 

問題在於$ form-> getParent() - > getConfig() - > getDataClass()thro在getDataClass()方面是個例外。

問題是如何正確構建表單來執行綁定測試?

回答

3

好吧,回答我的自我:)

這裏是PHPUnit的好嘲弄:

protected function setUp() 
{ 
    parent::setUp(); 

    $this->handlerManager = $this->getHandlerManagerMock(); 

    $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); 
    $this->builder = new FormBuilder(null, null, $this->dispatcher, $this->factory); 

    $this->form = $this->factory->create('my_file'); 
    $this->form->setParent($this->getFormMock()); 
} 

protected function getFormMock() 
{ 
    $mock = $this->getMock('Symfony\Component\Form\Tests\FormInterface'); 

    $mock 
     ->expects($this->any()) 
     ->method('getConfig') 
     ->will($this->returnValue($this->getFormConfigMock())) 
    ; 

    return $mock; 
} 

protected function getFormConfigMock() 
{ 
    $mock = $this->getMockBuilder('Symfony\Component\Form\FormConfigInterface') 
     ->disableOriginalConstructor() 
     ->getMock(); 

    $mock 
     ->expects($this->any()) 
     ->method('getDataClass') 
     ->will($this->returnValue('My\FooBundle\Tests\DummyEntity')) 
    ; 

    return $mock; 
} 

我想我可以設法重建整個表格,而無需使用模擬,但它似乎是不可能的。

如果有人有更好的解決方案提供我感興趣。