2017-05-08 68 views
0

形式測試返回錯誤symfony3 + PHPUnit的形式測試問題

測試\的appbundle \表格\型號\ UserPreferencesTypeTest :: testUserPreferencesForm 的Symfony \分量\ OptionsResolver \異常\ UndefinedOptionsException:選項 「約束」 不存在。定義的選項是:「action」,「attr」,「auto_initialize」,「block_name」,「by_reference」,「choice_attr」,「choice_label」,「choice_loader」,「choice_name」,「choice_translation_domain」,「choice_value」 「,」choices_as_values「,」compound「,」data「,」data_class「,」disabled「,」empty_data「,」error_bubbling「,」expanded「,」group_by「,」inherit_data「,」label「,」label_attr「, label_format,mapped,method,multiple,placeholder,post_max_size_message,preferred_choices,property_path,required,translation_domain,trim,upload_max_size_message

這裏是我的形式

public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('global_review_count_threshold', ChoiceType::class, array(
      'choices' => array(
       '5%' => 5, 
       '10%' => 10, 
       '15%' => 15, 
       '20%' => 20, 
       '25%' => 25, 
       '30%' => 30, 
       '35%' => 35, 
       '40%' => 40, 
       '45%' => 45, 
       '50%' => 50, 
      ), 'constraints' => array(new NotBlank(),), 'label' => "Global Review Count Threshold", 
      'data' => $options['review_count_choice'] 
     )); 
    } 

    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults(array(
      'review_count_choice' => null, 
     )); 
    } 

    public function getName() 
    { 
     return 'app_bundle_user_preferences_form'; 
    } 

和我的形式測試

public function testUserPreferencesForm() 
    { 
     $formData = array(
      'global_review_count_threshold' => '10%', 
     ); 

     $form = $this->factory->create(UserPreferencesType::class); 

     //$object = TestObject::fromArray($formData); 

     $form->submit($formData); 

     $this->assertTrue($form->isSynchronized()); 
     //$this->assertEquals($object, $form->getData()); 

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

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

回答

0

我也在尋找這個問題的解決方案。

現在我建議使用TestObject制定者。 例子:

$formData = array(
     'email' => '[email protected]', 
     'surname' => 'Doe', 
    ); 

    $form = $this->factory->create(RecipientType::class); 

    $object = new Recipient(); 
    $object->setEmail($formData['email']); 
    $object->setSurname($formData['surname']); 

這讓我通過斷言

$this->assertEquals($object, $form->getData()); 

也許這是不是最好的解決方案,但做了它應該

+0

FYI當然,我們可以離開執行$ FORMDATA陣列,但在我的情況下,這個數組是其他斷言的必要條件 – Leszek