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\PropertyAccess\PropertyPath
;
15 use Symfony\Component\Form\Form
;
16 use Symfony\Component\Form\CallbackTransformer
;
17 use Symfony\Component\Form\Tests\Fixtures\Author
;
18 use Symfony\Component\Form\Tests\Fixtures\FixedDataTransformer
;
19 use Symfony\Component\Form\FormError
;
21 class FormTest_AuthorWithoutRefSetter
25 protected $referenceCopy;
27 public function __construct($reference)
29 $this->reference
= $reference;
30 $this->referenceCopy
= $reference;
33 // The returned object should be modified by reference without having
34 // to provide a setReference() method
35 public function getReference()
37 return $this->reference
;
40 // The returned object is a copy, so setReferenceCopy() must be used
42 public function getReferenceCopy()
44 return is_object($this->referenceCopy
) ? clone $this->referenceCopy
: $this->referenceCopy
;
47 public function setReferenceCopy($reference)
49 $this->referenceCopy
= $reference;
53 class FormTypeTest
extends BaseTypeTest
55 public function testCreateFormInstances()
57 $this->assertInstanceOf('Symfony\Component\Form\Form', $this->factory
->create('form'));
60 public function testPassRequiredAsOption()
62 $form = $this->factory
->create('form', null, array('required' => false));
64 $this->assertFalse($form->isRequired());
66 $form = $this->factory
->create('form', null, array('required' => true));
68 $this->assertTrue($form->isRequired());
71 public function testSubmittedDataIsTrimmedBeforeTransforming()
73 $form = $this->factory
->createBuilder('form')
74 ->addViewTransformer(new FixedDataTransformer(array(
83 $this->assertEquals('a', $form->getViewData());
84 $this->assertEquals('reverse[a]', $form->getData());
87 public function testSubmittedDataIsNotTrimmedBeforeTransformingIfNoTrimming()
89 $form = $this->factory
->createBuilder('form', null, array('trim' => false))
90 ->addViewTransformer(new FixedDataTransformer(array(
92 'reverse[ a ]' => ' a ',
99 $this->assertEquals(' a ', $form->getViewData());
100 $this->assertEquals('reverse[ a ]', $form->getData());
103 public function testNonReadOnlyFormWithReadOnlyParentIsReadOnly()
105 $view = $this->factory
->createNamedBuilder('parent', 'form', null, array('read_only' => true))
106 ->add('child', 'form')
110 $this->assertTrue($view['child']->vars
['read_only']);
113 public function testReadOnlyFormWithNonReadOnlyParentIsReadOnly()
115 $view = $this->factory
->createNamedBuilder('parent', 'form')
116 ->add('child', 'form', array('read_only' => true))
120 $this->assertTrue($view['child']->vars
['read_only']);
123 public function testNonReadOnlyFormWithNonReadOnlyParentIsNotReadOnly()
125 $view = $this->factory
->createNamedBuilder('parent', 'form')
126 ->add('child', 'form')
130 $this->assertFalse($view['child']->vars
['read_only']);
133 public function testPassMaxLengthToView()
135 $form = $this->factory
->create('form', null, array('max_length' => 10));
136 $view = $form->createView();
138 $this->assertSame(10, $view->vars
['max_length']);
141 public function testSubmitWithEmptyDataCreatesObjectIfClassAvailable()
143 $builder = $this->factory
->createBuilder('form', null, array(
144 'data_class' => 'Symfony\Component\Form\Tests\Fixtures\Author',
147 $builder->add('firstName', 'text');
148 $builder->add('lastName', 'text');
149 $form = $builder->getForm();
151 $form->setData(null);
152 // partially empty, still an object is created
153 $form->submit(array('firstName' => 'Bernhard', 'lastName' => ''));
155 $author = new Author();
156 $author->firstName
= 'Bernhard';
157 $author->setLastName('');
159 $this->assertEquals($author, $form->getData());
162 public function testSubmitWithEmptyDataCreatesObjectIfInitiallySubmittedWithObject()
164 $builder = $this->factory
->createBuilder('form', null, array(
165 // data class is inferred from the passed object
166 'data' => new Author(),
169 $builder->add('firstName', 'text');
170 $builder->add('lastName', 'text');
171 $form = $builder->getForm();
173 $form->setData(null);
174 // partially empty, still an object is created
175 $form->submit(array('firstName' => 'Bernhard', 'lastName' => ''));
177 $author = new Author();
178 $author->firstName
= 'Bernhard';
179 $author->setLastName('');
181 $this->assertEquals($author, $form->getData());
184 public function testSubmitWithEmptyDataCreatesArrayIfDataClassIsNull()
186 $builder = $this->factory
->createBuilder('form', null, array(
187 'data_class' => null,
190 $builder->add('firstName', 'text');
191 $form = $builder->getForm();
193 $form->setData(null);
194 $form->submit(array('firstName' => 'Bernhard'));
196 $this->assertSame(array('firstName' => 'Bernhard'), $form->getData());
199 public function testSubmitEmptyWithEmptyDataCreatesNoObjectIfNotRequired()
201 $builder = $this->factory
->createBuilder('form', null, array(
202 'data_class' => 'Symfony\Component\Form\Tests\Fixtures\Author',
205 $builder->add('firstName', 'text');
206 $builder->add('lastName', 'text');
207 $form = $builder->getForm();
209 $form->setData(null);
210 $form->submit(array('firstName' => '', 'lastName' => ''));
212 $this->assertNull($form->getData());
215 public function testSubmitEmptyWithEmptyDataCreatesObjectIfRequired()
217 $builder = $this->factory
->createBuilder('form', null, array(
218 'data_class' => 'Symfony\Component\Form\Tests\Fixtures\Author',
221 $builder->add('firstName', 'text');
222 $builder->add('lastName', 'text');
223 $form = $builder->getForm();
225 $form->setData(null);
226 $form->submit(array('firstName' => '', 'lastName' => ''));
228 $this->assertEquals(new Author(), $form->getData());
232 * We need something to write the field values into
234 public function testSubmitWithEmptyDataStoresArrayIfNoClassAvailable()
236 $form = $this->factory
->createBuilder('form')
237 ->add('firstName', 'text')
240 $form->setData(null);
241 $form->submit(array('firstName' => 'Bernhard'));
243 $this->assertSame(array('firstName' => 'Bernhard'), $form->getData());
246 public function testSubmitWithEmptyDataPassesEmptyStringToTransformerIfNotCompound()
248 $form = $this->factory
->createBuilder('form')
249 ->addViewTransformer(new FixedDataTransformer(array(
250 // required for the initial, internal setData(null)
252 // required to test that submit(null) is converted to ''
260 $this->assertSame('empty', $form->getData());
263 public function testSubmitWithEmptyDataUsesEmptyDataOption()
265 $author = new Author();
267 $builder = $this->factory
->createBuilder('form', null, array(
268 'data_class' => 'Symfony\Component\Form\Tests\Fixtures\Author',
269 'empty_data' => $author,
271 $builder->add('firstName', 'text');
272 $form = $builder->getForm();
274 $form->submit(array('firstName' => 'Bernhard'));
276 $this->assertSame($author, $form->getData());
277 $this->assertEquals('Bernhard', $author->firstName
);
280 public function provideZeros()
285 array('00000', '00000'),
290 * @dataProvider provideZeros
291 * @see https://github.com/symfony/symfony/issues/1986
293 public function testSetDataThroughParamsWithZero($data, $dataAsString)
295 $form = $this->factory
->create('form', null, array(
299 $view = $form->createView();
301 $this->assertFalse($form->isEmpty());
303 $this->assertSame($dataAsString, $view->vars
['value']);
304 $this->assertSame($dataAsString, $form->getData());
308 * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
310 public function testAttributesException()
312 $this->factory
->create('form', null, array('attr' => ''));
315 public function testNameCanBeEmptyString()
317 $form = $this->factory
->createNamed('', 'form');
319 $this->assertEquals('', $form->getName());
322 public function testSubformDoesntCallSetters()
324 $author = new FormTest_AuthorWithoutRefSetter(new Author());
326 $builder = $this->factory
->createBuilder('form', $author);
327 $builder->add('reference', 'form', array(
328 'data_class' => 'Symfony\Component\Form\Tests\Fixtures\Author',
330 $builder->get('reference')->add('firstName', 'text');
331 $form = $builder->getForm();
334 // reference has a getter, but not setter
335 'reference' => array(
336 'firstName' => 'Foo',
340 $this->assertEquals('Foo', $author->getReference()->firstName
);
343 public function testSubformCallsSettersIfTheObjectChanged()
346 $author = new FormTest_AuthorWithoutRefSetter(null);
347 $newReference = new Author();
349 $builder = $this->factory
->createBuilder('form', $author);
350 $builder->add('referenceCopy', 'form', array(
351 'data_class' => 'Symfony\Component\Form\Tests\Fixtures\Author',
353 $builder->get('referenceCopy')->add('firstName', 'text');
354 $form = $builder->getForm();
356 $form['referenceCopy']->setData($newReference); // new author object
359 // referenceCopy has a getter that returns a copy
360 'referenceCopy' => array(
361 'firstName' => 'Foo',
365 $this->assertEquals('Foo', $author->getReferenceCopy()->firstName
);
368 public function testSubformCallsSettersIfByReferenceIsFalse()
370 $author = new FormTest_AuthorWithoutRefSetter(new Author());
372 $builder = $this->factory
->createBuilder('form', $author);
373 $builder->add('referenceCopy', 'form', array(
374 'data_class' => 'Symfony\Component\Form\Tests\Fixtures\Author',
375 'by_reference' => false
377 $builder->get('referenceCopy')->add('firstName', 'text');
378 $form = $builder->getForm();
381 // referenceCopy has a getter that returns a copy
382 'referenceCopy' => array(
383 'firstName' => 'Foo',
387 // firstName can only be updated if setReferenceCopy() was called
388 $this->assertEquals('Foo', $author->getReferenceCopy()->firstName
);
391 public function testSubformCallsSettersIfReferenceIsScalar()
393 $author = new FormTest_AuthorWithoutRefSetter('scalar');
395 $builder = $this->factory
->createBuilder('form', $author);
396 $builder->add('referenceCopy', 'form');
397 $builder->get('referenceCopy')->addViewTransformer(new CallbackTransformer(
399 function ($value) { // reverseTransform
404 $form = $builder->getForm();
407 'referenceCopy' => array(), // doesn't matter actually
410 // firstName can only be updated if setReferenceCopy() was called
411 $this->assertEquals('foobar', $author->getReferenceCopy());
414 public function testSubformAlwaysInsertsIntoArrays()
416 $ref1 = new Author();
417 $ref2 = new Author();
418 $author = array('referenceCopy' => $ref1);
420 $builder = $this->factory
->createBuilder('form');
421 $builder->setData($author);
422 $builder->add('referenceCopy', 'form');
423 $builder->get('referenceCopy')->addViewTransformer(new CallbackTransformer(
425 function ($value) use ($ref2) { // reverseTransform
430 $form = $builder->getForm();
433 'referenceCopy' => array('a' => 'b'), // doesn't matter actually
436 // the new reference was inserted into the array
437 $author = $form->getData();
438 $this->assertSame($ref2, $author['referenceCopy']);
441 public function testPassMultipartTrueIfAnyChildIsMultipartToView()
443 $view = $this->factory
->createBuilder('form')
449 $this->assertTrue($view->vars
['multipart']);
452 public function testViewIsNotRenderedByDefault()
454 $view = $this->factory
->createBuilder('form')
459 $this->assertFalse($view->isRendered());
462 public function testErrorBubblingIfCompound()
464 $form = $this->factory
->create('form', null, array(
468 $this->assertTrue($form->getConfig()->getErrorBubbling());
471 public function testNoErrorBubblingIfNotCompound()
473 $form = $this->factory
->create('form', null, array(
477 $this->assertFalse($form->getConfig()->getErrorBubbling());
480 public function testOverrideErrorBubbling()
482 $form = $this->factory
->create('form', null, array(
484 'error_bubbling' => true,
487 $this->assertTrue($form->getConfig()->getErrorBubbling());
490 public function testPropertyPath()
492 $form = $this->factory
->create('form', null, array(
493 'property_path' => 'foo',
496 $this->assertEquals(new PropertyPath('foo'), $form->getPropertyPath());
497 $this->assertTrue($form->getConfig()->getMapped());
500 public function testPropertyPathNullImpliesDefault()
502 $form = $this->factory
->createNamed('name', 'form', null, array(
503 'property_path' => null,
506 $this->assertEquals(new PropertyPath('name'), $form->getPropertyPath());
507 $this->assertTrue($form->getConfig()->getMapped());
510 public function testNotMapped()
512 $form = $this->factory
->create('form', null, array(
513 'property_path' => 'foo',
517 $this->assertEquals(new PropertyPath('foo'), $form->getPropertyPath());
518 $this->assertFalse($form->getConfig()->getMapped());
521 public function testViewValidNotSubmitted()
523 $form = $this->factory
->create('form');
524 $view = $form->createView();
525 $this->assertTrue($view->vars
['valid']);
528 public function testViewNotValidSubmitted()
530 $form = $this->factory
->create('form');
531 $form->submit(array());
532 $form->addError(new FormError('An error'));
533 $view = $form->createView();
534 $this->assertFalse($view->vars
['valid']);
537 public function testDataOptionSupersedesSetDataCalls()
539 $form = $this->factory
->create('form', null, array(
544 $form->setData('foobar');
546 $this->assertSame('default', $form->getData());
549 public function testNormDataIsPassedToView()
551 $view = $this->factory
->createBuilder('form')
552 ->addViewTransformer(new FixedDataTransformer(array(
559 $this->assertSame('foo', $view->vars
['data']);
560 $this->assertSame('bar', $view->vars
['value']);
563 // https://github.com/symfony/symfony/issues/6862
564 public function testPassZeroLabelToView()
566 $view = $this->factory
->create('form', null, array(
571 $this->assertSame('0', $view->vars
['label']);
574 protected function getTestedType()