Symfony2 Restricted Testing Modules

I have a form that is not related to an entity, but has limitations, as mentioned here: http://symfony.com/doc/current/book/forms.html#adding-validation

ContactBundle \ Tests \ Form \ Type \ TestedTypeTest :: testSubmitValidData Symfony \ Component \ OptionsResolver \ Exception \ InvalidOptionsException: the "restrictions" option does not exist. The following options are known: "action", "attr", "auto_initialize", "block_name", "by_reference", "compound", "data", "data_class", "disabled", "empty_data", "error_bubbling", "inherit_data "" label "," label_attr "," mapped "," max_length "," method "," pattern "," post_max_size_message "," property_path "," read_only "," required "," translation_domain "," trim "," virtual "

Here is the part of the form:

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 )) //.. ; //.. 

Here is the unit test

 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); } } } 

I assume this is a problem with the test instead of the form, as the form works as expected. I am not sure what I should change. Any help or advice would be convenient.

thanks

+5
source share
3 answers

I think the problem is that you also pass formData to the factory create method.

It should be nice to just pass the form data through $form->submit($formData) , as you already have in your code

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

EDIT:

Well, it looks like you need to add validator / constaint extensions to the factory object in your test setup, as indicated here http://symfony.com/doc/current/cookbook/form/unit_testing.html#adding-custom-extensions

+2
source

Yes, Rob was right, I needed to add validator / restriction extensions. For instance:

  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(); } //.. 
+5
source

I suggest you override the TypeTestCase :: getExtensions () method

 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; } 

add the createValidator () method to your test class:

  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); } 

You do not need to override TypeTestCase :: getTypeExtensions (), because the ValidatorExtension class already has a loadTypeExtensions () method.

Then your test method might look like this:

 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); } } 

You can check invalid data:

 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']); } 
Sources

:

+1
source

Source: https://habr.com/ru/post/1207229/


All Articles