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
;
14 use Symfony\Component\Form\FormBuilder
;
16 class FormBuilderTest
extends \PHPUnit_Framework_TestCase
24 protected function setUp()
26 if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
27 $this->markTestSkipped('The "EventDispatcher" component is not available');
30 $this->dispatcher
= $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
31 $this->factory
= $this->getMock('Symfony\Component\Form\FormFactoryInterface');
32 $this->builder
= new FormBuilder('name', null, $this->dispatcher
, $this->factory
);
35 protected function tearDown()
37 $this->dispatcher
= null;
38 $this->factory
= null;
39 $this->builder
= null;
43 * Changing the name is not allowed, otherwise the name and property path
44 * are not synchronized anymore
46 * @see FormType::buildForm
48 public function testNoSetName()
50 $this->assertFalse(method_exists($this->builder
, 'setName'));
53 public function testAddNameNoStringAndNoInteger()
55 $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
56 $this->builder
->add(true);
59 public function testAddTypeNoString()
61 $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
62 $this->builder
->add('foo', 1234);
65 public function testAddWithGuessFluent()
67 $this->builder
= new FormBuilder('name', 'stdClass', $this->dispatcher
, $this->factory
);
68 $builder = $this->builder
->add('foo');
69 $this->assertSame($builder, $this->builder
);
72 public function testAddIsFluent()
74 $builder = $this->builder
->add('foo', 'text', array('bar' => 'baz'));
75 $this->assertSame($builder, $this->builder
);
78 public function testAdd()
80 $this->assertFalse($this->builder
->has('foo'));
81 $this->builder
->add('foo', 'text');
82 $this->assertTrue($this->builder
->has('foo'));
85 public function testAddIntegerName()
87 $this->assertFalse($this->builder
->has(0));
88 $this->builder
->add(0, 'text');
89 $this->assertTrue($this->builder
->has(0));
92 public function testAll()
94 $this->factory
->expects($this->once())
95 ->method('createNamedBuilder')
97 ->will($this->returnValue(new FormBuilder('foo', null, $this->dispatcher
, $this->factory
)));
99 $this->assertCount(0, $this->builder
->all());
100 $this->assertFalse($this->builder
->has('foo'));
102 $this->builder
->add('foo', 'text');
103 $children = $this->builder
->all();
105 $this->assertTrue($this->builder
->has('foo'));
106 $this->assertCount(1, $children);
107 $this->assertArrayHasKey('foo', $children);
111 * https://github.com/symfony/symfony/issues/4693
113 public function testMaintainOrderOfLazyAndExplicitChildren()
115 $this->builder
->add('foo', 'text');
116 $this->builder
->add($this->getFormBuilder('bar'));
117 $this->builder
->add('baz', 'text');
119 $children = $this->builder
->all();
121 $this->assertSame(array('foo', 'bar', 'baz'), array_keys($children));
124 public function testAddFormType()
126 $this->assertFalse($this->builder
->has('foo'));
127 $this->builder
->add('foo', $this->getMock('Symfony\Component\Form\FormTypeInterface'));
128 $this->assertTrue($this->builder
->has('foo'));
131 public function testRemove()
133 $this->builder
->add('foo', 'text');
134 $this->builder
->remove('foo');
135 $this->assertFalse($this->builder
->has('foo'));
138 public function testRemoveUnknown()
140 $this->builder
->remove('foo');
141 $this->assertFalse($this->builder
->has('foo'));
144 // https://github.com/symfony/symfony/pull/4826
145 public function testRemoveAndGetForm()
147 $this->builder
->add('foo', 'text');
148 $this->builder
->remove('foo');
149 $form = $this->builder
->getForm();
150 $this->assertInstanceOf('Symfony\Component\Form\Form', $form);
153 public function testCreateNoTypeNo()
155 $this->factory
->expects($this->once())
156 ->method('createNamedBuilder')
157 ->with('foo', 'text', null, array())
160 $this->builder
->create('foo');
163 public function testGetUnknown()
165 $this->setExpectedException('Symfony\Component\Form\Exception\InvalidArgumentException', 'The child with the name "foo" does not exist.');
166 $this->builder
->get('foo');
169 public function testGetExplicitType()
171 $expectedType = 'text';
172 $expectedName = 'foo';
173 $expectedOptions = array('bar' => 'baz');
175 $this->factory
->expects($this->once())
176 ->method('createNamedBuilder')
177 ->with($expectedName, $expectedType, null, $expectedOptions)
178 ->will($this->returnValue($this->getFormBuilder()));
180 $this->builder
->add($expectedName, $expectedType, $expectedOptions);
181 $builder = $this->builder
->get($expectedName);
183 $this->assertNotSame($builder, $this->builder
);
186 public function testGetGuessedType()
188 $expectedName = 'foo';
189 $expectedOptions = array('bar' => 'baz');
191 $this->factory
->expects($this->once())
192 ->method('createBuilderForProperty')
193 ->with('stdClass', $expectedName, null, $expectedOptions)
194 ->will($this->returnValue($this->getFormBuilder()));
196 $this->builder
= new FormBuilder('name', 'stdClass', $this->dispatcher
, $this->factory
);
197 $this->builder
->add($expectedName, null, $expectedOptions);
198 $builder = $this->builder
->get($expectedName);
200 $this->assertNotSame($builder, $this->builder
);
203 public function testGetFormConfigErasesReferences()
205 $builder = new FormBuilder('name', null, $this->dispatcher
, $this->factory
);
206 $builder->add(new FormBuilder('child', null, $this->dispatcher
, $this->factory
));
208 $config = $builder->getFormConfig();
209 $reflClass = new \
ReflectionClass($config);
210 $children = $reflClass->getProperty('children');
211 $unresolvedChildren = $reflClass->getProperty('unresolvedChildren');
213 $children->setAccessible(true);
214 $unresolvedChildren->setAccessible(true);
216 $this->assertEmpty($children->getValue($config));
217 $this->assertEmpty($unresolvedChildren->getValue($config));
220 private function getFormBuilder($name = 'name')
222 $mock = $this->getMockBuilder('Symfony\Component\Form\FormBuilder')
223 ->disableOriginalConstructor()
226 $mock->expects($this->any())
228 ->will($this->returnValue($name));