2014-11-19 16 views
5

Tôi đã có một hình thức mà không được kết nối với một thực thể nhưng không có những hạn chế như đã đề cập ở đây: http://symfony.com/doc/current/book/forms.html#adding-validationSymfony2 Đơn vị kiểm tra Forms với các ràng buộc

ContactBundle \ thử nghiệm \ Mẫu \ Loại \ TestedTypeTest :: testSubmitValidData Symfony \ Component \ OptionsResolver \ Exception \ InvalidOptionsException: Tùy chọn "ràng buộc" không tồn tại. Các tùy chọn đã biết là: "hành động", "attr", "auto_initialize", "block_name", "by_reference", "hợp chất", "dữ liệu", "data_class", "bị tắt", "empty_data", "error_bubbling", "inherit_data "," label "," label_attr "," ánh xạ "," max_length "," phương thức "," mẫu "," post_max_size_message "," property_path "," read_only "," required "," translation_domain "," trim ", "ảo"

Dưới đây là một phần của các hình thức:

class ContactType extends AbstractType 
{ 

/** 
* Build the form 
* @param \Symfony\Component\Form\FormBuilderInterface $builder BuilderInterface 
* @param array $aOption Array of options 
*/ 
public function buildForm(FormBuilderInterface $builder, array $aOption) 
{ 
    ///.. 
    $builder->add('name', 'text', array(
       'constraints' => array(
        new NotBlank(), 
        new Length(array('min' => 3)), 
       ), 
      )) 
      ->add('address', 'textarea', array(
       'required' => false 
      )) 
      //.. 
      ; 
    //.. 

đây là bài kiểm tra đơn vị

class TestedTypeTest extends TypeTestCase 
{ 

public function testSubmitValidData() 
{ 
    $formData = array(
     'name' => 'Test Name', 
     'address' => '', 
    ); 
    $type = new ContactType(); 
    $form = $this->factory->create($type, $formData); 
    $form->submit($formData); 

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

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

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

Tôi đoán rằng đây là vấn đề với bài kiểm tra thay vì biểu mẫu dưới dạng biểu mẫu hoạt động như mong đợi. Tôi không chắc mình nên thay đổi điều gì. Bất kỳ giúp đỡ hoặc tư vấn sẽ được thuận tiện

Cảm ơn

Trả lời

2

Tôi nghĩ vấn đề là bạn cũng vượt qua formData để tạo ra phương pháp của nhà máy.

Nó sẽ được tốt để chỉ cần vượt qua các dữ liệu biểu mẫu qua $form->submit($formData) như bạn đã có trong mã của bạn

Nhiều tài liệu: http://symfony.com/doc/current/cookbook/form/unit_testing.html

EDIT:

Cũng có vẻ như bạn phải thêm tiện ích mở rộng validator/constaint vào đối tượng factory trong thiết lập thử nghiệm của bạn như đã nêu ở đây http://symfony.com/doc/current/cookbook/form/unit_testing.html#adding-custom-extensions

+0

Vâng tôi đã thử rằng: $ type = new ContactType(); $ form = $ this-> factory-> create ($ type); nhưng nó vẫn là vấn đề tương tự. Theo tài liệu, tham số thứ hai của việc tạo có thể là một đối tượng, mảng hoặc không có gì. tạo ra hàm public ($ type = 'form', $ data = null, mảng $ options = array()); Tài liệu Api cho FormFactoryInterface: http://api.symfony.com/2.3/Symfony/Component/Form/FormFactoryInterface.html – pfwd

+0

Dòng nào gây ra sự thực thi? (Và các đối số khác của create() cuối cùng cũng được chuyển thành tham số thứ hai trong FormForm của bạn (FormBuilderInterface $ builder, array $ aOption)) – Rob

+0

Từ stack trace: symfony/symfony/src/Symfony/Component/OptionsResolver/OptionsResolver.php : 255 symfony/symfony/src/Symfony/Component/OptionsResolver/OptionsResolver.php: 219 symfony/symfony/src/Symfony/Component/Form/ResolvedFormType.php: 109 symfony/symfony/src/Symfony/Thành phần/Biểu mẫu /FormFactory.php:87 symfony/symfony/src/Symfony/Component/Form/FormBuilder.php: 106 symfony/symfony/src/Symfony/Component/Form/FormBuilder.php: 268 symfony/symfony/src/Symfony /Component/Form/FormBuilder.php:216 symfony/symfony/src/Symfony/Component/Form/FormFactory.php: 39 /Kiểm tra s/Form/Type/ContactTypeTest.php: 34 – pfwd

5

Có Rob đã đúng Tôi cần thêm các phần mở rộng validator/constraint. Cũng giống như vậy:

class TestedTypeTest extends TypeTestCase 
{ 

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

    $validator = $this->getMock('\Symfony\Component\Validator\Validator\ValidatorInterface'); 
    $validator->method('validate')->will($this->returnValue(new ConstraintViolationList())); 
    $formTypeExtension = new FormTypeValidatorExtension($validator); 
    $coreExtension = new CoreExtension(); 

    $this->factory = Forms::createFormFactoryBuilder() 
      ->addExtensions($this->getExtensions()) 
      ->addExtension($coreExtension) 
      ->addTypeExtension($formTypeExtension) 
      ->getFormFactory(); 
} 
//.. 
+0

Điều này không chỉ giải quyết những khó khăn? Vì vậy, thật khó để kiểm tra xem liệu dữ liệu được gửi có vượt qua hay thất bại các ràng buộc không? –

+0

Thật vậy, có vẻ như Victor S là chính xác. Tôi sẽ cho phép bạn hoàn thành bài kiểm tra, nhưng nó sẽ không kiểm tra các ràng buộc. Tôi đã tạo một phương thức testSubmitInvalidData với đầu vào không hợp lệ, nhưng $ form-> isValid() sẽ luôn trả về true. Gần, nhưng không có xì gà. – onok

+0

Danh sách ConstraintViolationList của bạn trống, biểu mẫu của bạn luôn hợp lệ – Arno

1

tôi đề nghị bạn để ghi đè lên TypeTestCase :: getExtensions() phương pháp

use Symfony\Component\Form\Extension\Core\CoreExtension; 
use Symfony\Component\Form\Extension\Core\Type\FormType; 
use Symfony\Component\Form\Extension\Validator\ValidatorExtension; 
use Symfony\Component\Form\Form; 
use Symfony\Component\Translation\IdentityTranslator; 
use Symfony\Component\Validator\ConstraintValidatorFactory; 
use Symfony\Component\Validator\Context\ExecutionContextFactory; 
use Symfony\Component\Validator\Mapping\ClassMetadata; 
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface; 
use Symfony\Component\Validator\Tests\Fixtures\FakeMetadataFactory; 
use Symfony\Component\Validator\Validator\RecursiveValidator; 

.... 

    public function getExtensions() 
    { 
     $extensions = parent::getExtensions(); 
     $metadataFactory = new FakeMetadataFactory(); 
     $metadataFactory->addMetadata(new ClassMetadata( Form::class)); 
     $validator = $this->createValidator($metadataFactory); 

     $extensions[] = new CoreExtension(); 
     $extensions[] = new ValidatorExtension($validator); 

     return $extensions; 
    } 

thêm phương pháp createValidator() trong lớp thử nghiệm của bạn:

protected function createValidator(MetadataFactoryInterface $metadataFactory, array $objectInitializers = array()) 
    { 
     $translator = new IdentityTranslator(); 
     $translator->setLocale('en'); 
     $contextFactory = new ExecutionContextFactory($translator); 
     $validatorFactory = new ConstraintValidatorFactory(); 
     return new RecursiveValidator($contextFactory, $metadataFactory, $validatorFactory, $objectInitializers); 
    } 

Không cần để ghi đè TypeTestCase :: getTypeExtensions() vì lớp ValidatorExtension đã có phương thức loadTypeExtensions().

Sau đó, phương pháp thử nghiệm của bạn có thể như thế này:

public function testSubmitValidData() 
{ 
    $formData = array(
     'name' => 'Test Name', 
     'address' => '', 
    ); 
    $form = $this->factory->create(ContactType::class); 
    $form->submit($formData); 
    $this->assertTrue($form->isValid()); 
    $this->assertTrue($form->isSynchronized()); 

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

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

Bạn có thể kiểm tra dữ liệu không hợp lệ:

public function testSubmitInvalidValidData() 
{ 
    $formData = array(
     'name' => 'T', 
    ); 
    $form = $this->factory->create(ContactType::class); 
    $form->submit($formData); 
    $this->assertFalse($form->isValid()); 
    $this->assertFalse($view->children['name']->vars['valid']); 

} 

nguồn: