2015-04-20 117 views
0

我在单元测试方面几乎没有经验。我阅读这个symfony食谱章节来测试一个表单类型。收集表单的单元测试

http://symfony.com/doc/current/cookbook/form/unit_testing.html

我的形式是这样的:

public function __construct(SecurityContext $securityContext, \Doctrine\ORM\EntityManager $em) 
{ 
    $this->securityContext = $securityContext; 
    $this->entityManager = $em; 
} 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('title', 'text', array('label' => 'title', 'translation_domain' => 'messages', 'attr' => array('maxlength' => 255))) 
       ->add('comments', 'collection', array(
         'type'   => new CommentType() , 
         'allow_add' => false, 
         'allow_delete' => false, 
         'label'  => false, 
         'options'  => array(
          'label' => false, 
         ) 
        ) 
       ) 
       ->add('translations', 'a2lix_translations', array(
         'fields' => array(
          'coverLetter' => array(
           'label' => 'msg.coverLetter', 
           'field_type' => 'textarea', 
           'attr' => array('class' => 'rte') 
          ) 
         ) 
       )); 
    } 

现在我写一个类来测试我的形式。

class QuestionnaireControllerTest extends TypeTestCase 
{ 



    public function testAddQuestionnaire() 
    { 

     $kernel = new \AppKernel('dev', true); 
     $kernel->boot(); 

     $container = $kernel->getContainer(); 

     $securityContext = $container->get('security.context'); 
     $entityManager = $container->get('doctrine.orm.entity_manager'); 


     $formData = array('title' => 'Exp. title'); 

     $type = new QuestionnaireType($securityContext, $entityManager); 
     $form = $this->factory->create($type); 



     $form->submit($formData); 

     $this->assertTrue($form->isSynchronized()); 


     $view = $form->createView(); 
     $children = $view->children; 

     foreach (array_keys($formData) as $key) { 
      $this->assertArrayHasKey($key, $children); 
     } 

    } 
} 

但我对我的测试类有一些问题。

  1. 这是获取内核的正确方法吗?
  2. 如何测试表单项目“评论(集合)”,“翻译(a2lix_translations)”?

不幸的是我不会为这些问题找到有用的教程。

回答

0

当谈到TypeTest的时候,它可能会变得有点奇怪,因为它似乎不在循环中。我遇到了一些类似的问题,实际上做了以下事情:

1)关于内核:扩展KernelTestCase并在测试中包含TypeTestCase(和超级)逻辑(或创建一个抽象)。这样你会有一个更一致的内核初始化方式和TypeTestCase是不是那么大,除了表单初始化。实际上可以自定义这可能会对以后有所帮助。

2)为了得到特别是加载扩展的A2lix东西到你的考试形式,你必须覆盖

protected function getExtensions() 

,并返回一个数组

return array(
     new PreloadedExtension(..., array()) 
); 

对于A2lix ......看起来像

$gedmoTranslationsType = new GedmoTranslationsType($this->container->get('a2lix_translation_form.gedmo.listener.translations'), $this->container->get('a2lix_translation_form.gedmo.service.translation'), $this->getLocales(), false); 
$gedmoTranslationsLocalesType = new GedmoTranslationsLocalesType(); 
$translationsFields = new TranslationsFieldsType(); 

return array(
    $gedmoTranslationsType->getName() => $gedmoTranslationsType, 
    $gedmoTranslationsLocalesType->getName() => $gedmoTranslationsLocalesType, 
    $translationsFields->getName() => $translationsFields, 
); 

我已经让自己的容器和区域设置方便的成员和功能。你应该这样做。

一般来说,扩展也将变得重要,例如,测试实体类型。因此,概括这些内容可能会有所帮助。

实际上有很多东西可以用来进行类型测试,你会发现一段时间以后。无论如何,我希望这有助于。

最好