Przeglądaj źródła

[Form] add tests for type_options in collectionType

Jeremie Augustin 14 lat temu
rodzic
commit
fb0a37f9ba

+ 87 - 0
tests/Symfony/Tests/Component/Form/Extension/Core/Type/CollectionTypeTest.php

@@ -143,4 +143,91 @@ class CollectionFormTest extends TypeTestCase
 
         $this->assertFalse($form->has('$$name$$'));
     }
+
+    public function testSetTypeOptions()
+    {
+        $form = $this->factory->create('collection', null, array(
+            'type' => 'field',
+            'type_options' => array(
+                'required' => false,
+                'max_length' => 20
+            ),
+        ));
+        $form->setData(array('foo@foo.com', 'foo@bar.com'));
+
+        $this->assertEquals(false, $form[0]->isRequired());
+        $this->assertEquals(false, $form[1]->isRequired());
+        $this->assertEquals(20, $form[0]->getAttribute('max_length'));
+        $this->assertEquals(20, $form[1]->getAttribute('max_length'));
+
+        $form->bind(array('foo@bar.com', 'bar@foo.com'));
+
+        $this->assertEquals(false, $form[0]->isRequired());
+        $this->assertEquals(false, $form[1]->isRequired());
+        $this->assertEquals(20, $form[0]->getAttribute('max_length'));
+        $this->assertEquals(20, $form[1]->getAttribute('max_length'));
+
+        //Test with prototype and extra field
+        $form = $this->factory->create('collection', null, array(
+            'allow_add' => true,
+            'prototype' => true,
+            'type' => 'field',
+            'type_options' => array(
+                'required' => false,
+                'max_length' => 20
+            ),
+        ));
+
+        $form->setData(array('foo@foo.com'));
+
+        $this->assertEquals(false, $form[0]->isRequired());
+        $this->assertEquals(20, $form[0]->getAttribute('max_length'));
+
+        $form->bind(array('foo@bar.com', 'bar@foo.com'));
+
+        $this->assertEquals(false, $form[0]->isRequired());
+        $this->assertEquals(false, $form[1]->isRequired());
+        $this->assertEquals(20, $form[0]->getAttribute('max_length'));
+        $this->assertEquals(20, $form[1]->getAttribute('max_length'));
+
+    }
+
+    public function testSetTypeOptionsWithoutOptions()
+    {
+        $form = $this->factory->create('collection', null, array(
+            'type' => 'field',
+        ));
+        $form->setData(array('foo@foo.com', 'foo@bar.com'));
+
+        $this->assertEquals(true, $form[0]->isRequired());
+        $this->assertEquals(true, $form[1]->isRequired());
+        $this->assertEquals(null, $form[0]->getAttribute('max_length'));
+        $this->assertEquals(null, $form[1]->getAttribute('max_length'));
+
+        $form->bind(array('foo@bar.com', 'bar@foo.com'));
+
+        $this->assertEquals(true, $form[0]->isRequired());
+        $this->assertEquals(true, $form[1]->isRequired());
+        $this->assertEquals(null, $form[0]->getAttribute('max_length'));
+        $this->assertEquals(null, $form[1]->getAttribute('max_length'));
+
+        //Test with prototype and extra field
+        $form = $this->factory->create('collection', null, array(
+            'allow_add' => true,
+            'prototype' => true,
+            'type' => 'field',
+        ));
+
+        $form->setData(array('foo@foo.com'));
+
+        $this->assertEquals(true, $form[0]->isRequired());
+        $this->assertEquals(null, $form[0]->getAttribute('max_length'));
+
+        $form->bind(array('foo@bar.com', 'bar@foo.com'));
+
+        $this->assertEquals(true, $form[0]->isRequired());
+        $this->assertEquals(true, $form[1]->isRequired());
+        $this->assertEquals(null, $form[0]->getAttribute('max_length'));
+        $this->assertEquals(null, $form[1]->getAttribute('max_length'));
+    }
 }