4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form\Tests\Extension\Core\Type
;
14 use Symfony\Component\Form\Form
;
16 class CollectionTypeTest
extends \Symfony\Component\Form\Test\TypeTestCase
18 public function testContainsNoChildByDefault()
20 $form = $this->factory
->create('collection', null, array(
24 $this->assertCount(0, $form);
27 public function testSetDataAdjustsSize()
29 $form = $this->factory
->create('collection', null, array(
35 $form->setData(array('foo@foo.com', 'foo@bar.com'));
37 $this->assertInstanceOf('Symfony\Component\Form\Form', $form[0]);
38 $this->assertInstanceOf('Symfony\Component\Form\Form', $form[1]);
39 $this->assertCount(2, $form);
40 $this->assertEquals('foo@foo.com', $form[0]->getData());
41 $this->assertEquals('foo@bar.com', $form[1]->getData());
42 $this->assertEquals(20, $form[0]->getConfig()->getOption('max_length'));
43 $this->assertEquals(20, $form[1]->getConfig()->getOption('max_length'));
45 $form->setData(array('foo@baz.com'));
46 $this->assertInstanceOf('Symfony\Component\Form\Form', $form[0]);
47 $this->assertFalse(isset($form[1]));
48 $this->assertCount(1, $form);
49 $this->assertEquals('foo@baz.com', $form[0]->getData());
50 $this->assertEquals(20, $form[0]->getConfig()->getOption('max_length'));
53 public function testThrowsExceptionIfObjectIsNotTraversable()
55 $form = $this->factory
->create('collection', null, array(
58 $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
59 $form->setData(new \
stdClass());
62 public function testNotResizedIfSubmittedWithMissingData()
64 $form = $this->factory
->create('collection', null, array(
67 $form->setData(array('foo@foo.com', 'bar@bar.com'));
68 $form->submit(array('foo@bar.com'));
70 $this->assertTrue($form->has('0'));
71 $this->assertTrue($form->has('1'));
72 $this->assertEquals('foo@bar.com', $form[0]->getData());
73 $this->assertEquals('', $form[1]->getData());
76 public function testResizedDownIfSubmittedWithMissingDataAndAllowDelete()
78 $form = $this->factory
->create('collection', null, array(
80 'allow_delete' => true,
82 $form->setData(array('foo@foo.com', 'bar@bar.com'));
83 $form->submit(array('foo@foo.com'));
85 $this->assertTrue($form->has('0'));
86 $this->assertFalse($form->has('1'));
87 $this->assertEquals('foo@foo.com', $form[0]->getData());
88 $this->assertEquals(array('foo@foo.com'), $form->getData());
91 public function testNotResizedIfSubmittedWithExtraData()
93 $form = $this->factory
->create('collection', null, array(
96 $form->setData(array('foo@bar.com'));
97 $form->submit(array('foo@foo.com', 'bar@bar.com'));
99 $this->assertTrue($form->has('0'));
100 $this->assertFalse($form->has('1'));
101 $this->assertEquals('foo@foo.com', $form[0]->getData());
104 public function testResizedUpIfSubmittedWithExtraDataAndAllowAdd()
106 $form = $this->factory
->create('collection', null, array(
110 $form->setData(array('foo@bar.com'));
111 $form->submit(array('foo@bar.com', 'bar@bar.com'));
113 $this->assertTrue($form->has('0'));
114 $this->assertTrue($form->has('1'));
115 $this->assertEquals('foo@bar.com', $form[0]->getData());
116 $this->assertEquals('bar@bar.com', $form[1]->getData());
117 $this->assertEquals(array('foo@bar.com', 'bar@bar.com'), $form->getData());
120 public function testAllowAddButNoPrototype()
122 $form = $this->factory
->create('collection', null, array(
125 'prototype' => false,
128 $this->assertFalse($form->has('__name__'));
131 public function testPrototypeMultipartPropagation()
133 $form = $this->factory
134 ->create('collection', null, array(
141 $this->assertTrue($form->createView()->vars
['multipart']);
144 public function testGetDataDoesNotContainsPrototypeNameBeforeDataAreSet()
146 $form = $this->factory
->create('collection', array(), array(
152 $data = $form->getData();
153 $this->assertFalse(isset($data['__name__']));
156 public function testGetDataDoesNotContainsPrototypeNameAfterDataAreSet()
158 $form = $this->factory
->create('collection', array(), array(
164 $form->setData(array('foobar.png'));
165 $data = $form->getData();
166 $this->assertFalse(isset($data['__name__']));
169 public function testPrototypeNameOption()
171 $form = $this->factory
->create('collection', null, array(
177 $this->assertSame('__name__', $form->getConfig()->getAttribute('prototype')->getName(), '__name__ is the default');
179 $form = $this->factory
->create('collection', null, array(
183 'prototype_name' => '__test__',
186 $this->assertSame('__test__', $form->getConfig()->getAttribute('prototype')->getName());
189 public function testPrototypeDefaultLabel()
191 $form = $this->factory
->create('collection', array(), array(
195 'prototype_name' => '__test__',
198 $this->assertSame('__test__label__', $form->createView()->vars
['prototype']->vars
['label']);