2012-07-06 14:33:06 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Form\Tests;
|
|
|
|
|
2013-08-22 13:20:52 +01:00
|
|
|
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
|
2013-04-20 14:56:42 +01:00
|
|
|
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler;
|
2012-07-06 14:33:06 +01:00
|
|
|
use Symfony\Component\Form\FormError;
|
2013-07-25 10:50:20 +01:00
|
|
|
use Symfony\Component\Form\Forms;
|
2013-09-17 10:39:17 +01:00
|
|
|
use Symfony\Component\Form\FormView;
|
2013-11-09 15:25:41 +00:00
|
|
|
use Symfony\Component\Form\SubmitButtonBuilder;
|
2012-07-06 14:33:06 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
|
|
|
use Symfony\Component\Form\Tests\Fixtures\FixedDataTransformer;
|
|
|
|
|
2012-07-16 11:47:05 +01:00
|
|
|
class CompoundFormTest extends AbstractFormTest
|
2012-07-06 14:33:06 +01:00
|
|
|
{
|
|
|
|
public function testValidIfAllChildrenAreValid()
|
|
|
|
{
|
2013-08-01 17:06:16 +01:00
|
|
|
$this->form->add($this->getBuilder('firstName')->getForm());
|
|
|
|
$this->form->add($this->getBuilder('lastName')->getForm());
|
2012-07-06 14:33:06 +01:00
|
|
|
|
2013-04-20 16:32:55 +01:00
|
|
|
$this->form->submit(array(
|
2012-07-06 14:33:06 +01:00
|
|
|
'firstName' => 'Bernhard',
|
|
|
|
'lastName' => 'Schussek',
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertTrue($this->form->isValid());
|
|
|
|
}
|
|
|
|
|
2012-12-14 15:05:05 +00:00
|
|
|
public function testInvalidIfChildIsInvalid()
|
2012-07-06 14:33:06 +01:00
|
|
|
{
|
2013-08-01 17:06:16 +01:00
|
|
|
$this->form->add($this->getBuilder('firstName')->getForm());
|
|
|
|
$this->form->add($this->getBuilder('lastName')->getForm());
|
2012-07-06 14:33:06 +01:00
|
|
|
|
2013-04-20 16:32:55 +01:00
|
|
|
$this->form->submit(array(
|
2012-07-06 14:33:06 +01:00
|
|
|
'firstName' => 'Bernhard',
|
|
|
|
'lastName' => 'Schussek',
|
|
|
|
));
|
|
|
|
|
2013-08-01 17:06:16 +01:00
|
|
|
$this->form->get('lastName')->addError(new FormError('Invalid'));
|
|
|
|
|
2012-07-06 14:33:06 +01:00
|
|
|
$this->assertFalse($this->form->isValid());
|
|
|
|
}
|
|
|
|
|
2013-08-01 17:06:16 +01:00
|
|
|
public function testDisabledFormsValidEvenIfChildrenInvalid()
|
|
|
|
{
|
|
|
|
$form = $this->getBuilder('person')
|
|
|
|
->setDisabled(true)
|
|
|
|
->setCompound(true)
|
|
|
|
->setDataMapper($this->getDataMapper())
|
|
|
|
->add($this->getBuilder('name'))
|
|
|
|
->getForm();
|
|
|
|
|
|
|
|
$form->submit(array('name' => 'Jacques Doe'));
|
|
|
|
|
|
|
|
$form->get('name')->addError(new FormError('Invalid'));
|
|
|
|
|
|
|
|
$this->assertTrue($form->isValid());
|
|
|
|
}
|
|
|
|
|
2013-09-27 12:47:53 +01:00
|
|
|
public function testSubmitForwardsNullIfNotClearMissingButValueIsExplicitlyNull()
|
|
|
|
{
|
|
|
|
$child = $this->getMockForm('firstName');
|
|
|
|
|
|
|
|
$this->form->add($child);
|
|
|
|
|
|
|
|
$child->expects($this->once())
|
|
|
|
->method('submit')
|
|
|
|
->with($this->equalTo(null));
|
|
|
|
|
|
|
|
$this->form->submit(array('firstName' => null), false);
|
|
|
|
}
|
|
|
|
|
2013-04-20 16:32:55 +01:00
|
|
|
public function testSubmitForwardsNullIfValueIsMissing()
|
2012-07-06 14:33:06 +01:00
|
|
|
{
|
|
|
|
$child = $this->getMockForm('firstName');
|
|
|
|
|
|
|
|
$this->form->add($child);
|
|
|
|
|
|
|
|
$child->expects($this->once())
|
2013-04-20 16:32:55 +01:00
|
|
|
->method('submit')
|
2012-07-06 14:33:06 +01:00
|
|
|
->with($this->equalTo(null));
|
|
|
|
|
2013-04-20 16:32:55 +01:00
|
|
|
$this->form->submit(array());
|
2012-07-06 14:33:06 +01:00
|
|
|
}
|
|
|
|
|
2013-08-01 16:40:17 +01:00
|
|
|
public function testSubmitDoesNotForwardNullIfNotClearMissing()
|
2013-04-25 15:08:09 +01:00
|
|
|
{
|
|
|
|
$child = $this->getMockForm('firstName');
|
|
|
|
|
|
|
|
$this->form->add($child);
|
|
|
|
|
2013-06-26 23:20:19 +01:00
|
|
|
$child->expects($this->never())
|
2013-08-01 16:40:17 +01:00
|
|
|
->method('submit');
|
2013-06-26 23:20:19 +01:00
|
|
|
|
2013-04-25 15:08:09 +01:00
|
|
|
$this->form->submit(array(), false);
|
|
|
|
}
|
|
|
|
|
2013-07-25 10:50:20 +01:00
|
|
|
public function testSubmitDoesNotAddExtraFieldForNullValues()
|
|
|
|
{
|
|
|
|
$factory = Forms::createFormFactoryBuilder()
|
|
|
|
->getFormFactory();
|
|
|
|
|
2015-06-23 17:42:21 +01:00
|
|
|
$child = $factory->createNamed('file', 'Symfony\Component\Form\Extension\Core\Type\FileType', null, array('auto_initialize' => false));
|
2013-07-25 10:50:20 +01:00
|
|
|
|
|
|
|
$this->form->add($child);
|
2014-02-27 22:38:05 +00:00
|
|
|
$this->form->submit(array('file' => null), false);
|
2013-07-25 10:50:20 +01:00
|
|
|
|
|
|
|
$this->assertCount(0, $this->form->getExtraData());
|
|
|
|
}
|
|
|
|
|
2013-04-25 15:08:09 +01:00
|
|
|
public function testClearMissingFlagIsForwarded()
|
|
|
|
{
|
|
|
|
$child = $this->getMockForm('firstName');
|
|
|
|
|
|
|
|
$this->form->add($child);
|
|
|
|
|
|
|
|
$child->expects($this->once())
|
|
|
|
->method('submit')
|
|
|
|
->with($this->equalTo('foo'), false);
|
|
|
|
|
|
|
|
$this->form->submit(array('firstName' => 'foo'), false);
|
|
|
|
}
|
|
|
|
|
2012-07-06 14:33:06 +01:00
|
|
|
public function testCloneChildren()
|
|
|
|
{
|
|
|
|
$child = $this->getBuilder('child')->getForm();
|
|
|
|
$this->form->add($child);
|
|
|
|
|
|
|
|
$clone = clone $this->form;
|
|
|
|
|
|
|
|
$this->assertNotSame($this->form, $clone);
|
|
|
|
$this->assertNotSame($child, $clone['child']);
|
2013-10-30 22:44:30 +00:00
|
|
|
$this->assertNotSame($this->form['child'], $clone['child']);
|
2012-07-06 14:33:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNotEmptyIfChildNotEmpty()
|
|
|
|
{
|
|
|
|
$child = $this->getMockForm();
|
|
|
|
$child->expects($this->once())
|
|
|
|
->method('isEmpty')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
|
|
$this->form->setData(null);
|
|
|
|
$this->form->add($child);
|
|
|
|
|
|
|
|
$this->assertFalse($this->form->isEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAdd()
|
|
|
|
{
|
|
|
|
$child = $this->getBuilder('foo')->getForm();
|
|
|
|
$this->form->add($child);
|
|
|
|
|
2012-12-14 15:05:05 +00:00
|
|
|
$this->assertTrue($this->form->has('foo'));
|
|
|
|
$this->assertSame($this->form, $child->getParent());
|
|
|
|
$this->assertSame(array('foo' => $child), $this->form->all());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddUsingNameAndType()
|
|
|
|
{
|
|
|
|
$child = $this->getBuilder('foo')->getForm();
|
|
|
|
|
|
|
|
$this->factory->expects($this->once())
|
|
|
|
->method('createNamed')
|
2015-11-26 08:12:29 +00:00
|
|
|
->with('foo', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, array(
|
2013-04-29 16:31:30 +01:00
|
|
|
'bar' => 'baz',
|
|
|
|
'auto_initialize' => false,
|
|
|
|
))
|
2012-12-14 15:05:05 +00:00
|
|
|
->will($this->returnValue($child));
|
|
|
|
|
2015-11-26 08:12:29 +00:00
|
|
|
$this->form->add('foo', 'Symfony\Component\Form\Extension\Core\Type\TextType', array('bar' => 'baz'));
|
2012-12-14 15:05:05 +00:00
|
|
|
|
|
|
|
$this->assertTrue($this->form->has('foo'));
|
|
|
|
$this->assertSame($this->form, $child->getParent());
|
|
|
|
$this->assertSame(array('foo' => $child), $this->form->all());
|
|
|
|
}
|
|
|
|
|
2012-12-14 18:27:28 +00:00
|
|
|
public function testAddUsingIntegerNameAndType()
|
|
|
|
{
|
|
|
|
$child = $this->getBuilder(0)->getForm();
|
|
|
|
|
|
|
|
$this->factory->expects($this->once())
|
|
|
|
->method('createNamed')
|
2015-11-26 08:12:29 +00:00
|
|
|
->with('0', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, array(
|
2013-04-29 16:31:30 +01:00
|
|
|
'bar' => 'baz',
|
|
|
|
'auto_initialize' => false,
|
|
|
|
))
|
2012-12-14 18:27:28 +00:00
|
|
|
->will($this->returnValue($child));
|
|
|
|
|
|
|
|
// in order to make casting unnecessary
|
2015-11-26 08:12:29 +00:00
|
|
|
$this->form->add(0, 'Symfony\Component\Form\Extension\Core\Type\TextType', array('bar' => 'baz'));
|
2012-12-14 18:27:28 +00:00
|
|
|
|
|
|
|
$this->assertTrue($this->form->has(0));
|
|
|
|
$this->assertSame($this->form, $child->getParent());
|
|
|
|
$this->assertSame(array(0 => $child), $this->form->all());
|
|
|
|
}
|
|
|
|
|
2014-12-30 11:40:10 +00:00
|
|
|
public function testAddWithoutType()
|
|
|
|
{
|
|
|
|
$child = $this->getBuilder('foo')->getForm();
|
|
|
|
|
|
|
|
$this->factory->expects($this->once())
|
|
|
|
->method('createNamed')
|
2015-11-26 08:12:29 +00:00
|
|
|
->with('foo', 'Symfony\Component\Form\Extension\Core\Type\TextType')
|
2014-12-30 11:40:10 +00:00
|
|
|
->will($this->returnValue($child));
|
|
|
|
|
|
|
|
$this->form->add('foo');
|
|
|
|
|
|
|
|
$this->assertTrue($this->form->has('foo'));
|
|
|
|
$this->assertSame($this->form, $child->getParent());
|
|
|
|
$this->assertSame(array('foo' => $child), $this->form->all());
|
2012-12-14 18:27:28 +00:00
|
|
|
}
|
|
|
|
|
2012-12-14 15:05:05 +00:00
|
|
|
public function testAddUsingNameButNoType()
|
|
|
|
{
|
|
|
|
$this->form = $this->getBuilder('name', null, '\stdClass')
|
|
|
|
->setCompound(true)
|
|
|
|
->setDataMapper($this->getDataMapper())
|
|
|
|
->getForm();
|
|
|
|
|
|
|
|
$child = $this->getBuilder('foo')->getForm();
|
|
|
|
|
|
|
|
$this->factory->expects($this->once())
|
|
|
|
->method('createForProperty')
|
|
|
|
->with('\stdClass', 'foo')
|
|
|
|
->will($this->returnValue($child));
|
|
|
|
|
|
|
|
$this->form->add('foo');
|
|
|
|
|
|
|
|
$this->assertTrue($this->form->has('foo'));
|
|
|
|
$this->assertSame($this->form, $child->getParent());
|
|
|
|
$this->assertSame(array('foo' => $child), $this->form->all());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddUsingNameButNoTypeAndOptions()
|
|
|
|
{
|
|
|
|
$this->form = $this->getBuilder('name', null, '\stdClass')
|
|
|
|
->setCompound(true)
|
|
|
|
->setDataMapper($this->getDataMapper())
|
|
|
|
->getForm();
|
|
|
|
|
|
|
|
$child = $this->getBuilder('foo')->getForm();
|
|
|
|
|
|
|
|
$this->factory->expects($this->once())
|
|
|
|
->method('createForProperty')
|
2013-04-29 16:31:30 +01:00
|
|
|
->with('\stdClass', 'foo', null, array(
|
|
|
|
'bar' => 'baz',
|
|
|
|
'auto_initialize' => false,
|
|
|
|
))
|
2012-12-14 15:05:05 +00:00
|
|
|
->will($this->returnValue($child));
|
|
|
|
|
|
|
|
$this->form->add('foo', null, array('bar' => 'baz'));
|
|
|
|
|
|
|
|
$this->assertTrue($this->form->has('foo'));
|
2012-07-06 14:33:06 +01:00
|
|
|
$this->assertSame($this->form, $child->getParent());
|
|
|
|
$this->assertSame(array('foo' => $child), $this->form->all());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-04-20 16:32:55 +01:00
|
|
|
* @expectedException \Symfony\Component\Form\Exception\AlreadySubmittedException
|
2012-07-06 14:33:06 +01:00
|
|
|
*/
|
2013-04-20 16:32:55 +01:00
|
|
|
public function testAddThrowsExceptionIfAlreadySubmitted()
|
2012-07-06 14:33:06 +01:00
|
|
|
{
|
2013-04-20 16:32:55 +01:00
|
|
|
$this->form->submit(array());
|
2012-07-06 14:33:06 +01:00
|
|
|
$this->form->add($this->getBuilder('foo')->getForm());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemove()
|
|
|
|
{
|
|
|
|
$child = $this->getBuilder('foo')->getForm();
|
|
|
|
$this->form->add($child);
|
|
|
|
$this->form->remove('foo');
|
|
|
|
|
|
|
|
$this->assertNull($child->getParent());
|
2013-01-10 01:11:10 +00:00
|
|
|
$this->assertCount(0, $this->form);
|
2012-07-06 14:33:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-04-20 16:32:55 +01:00
|
|
|
* @expectedException \Symfony\Component\Form\Exception\AlreadySubmittedException
|
2012-07-06 14:33:06 +01:00
|
|
|
*/
|
2013-04-20 16:32:55 +01:00
|
|
|
public function testRemoveThrowsExceptionIfAlreadySubmitted()
|
2012-07-06 14:33:06 +01:00
|
|
|
{
|
|
|
|
$this->form->add($this->getBuilder('foo')->setCompound(false)->getForm());
|
2013-04-20 16:32:55 +01:00
|
|
|
$this->form->submit(array('foo' => 'bar'));
|
2012-07-06 14:33:06 +01:00
|
|
|
$this->form->remove('foo');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemoveIgnoresUnknownName()
|
|
|
|
{
|
|
|
|
$this->form->remove('notexisting');
|
2017-03-18 09:10:35 +00:00
|
|
|
|
|
|
|
$this->assertCount(0, $this->form);
|
2012-07-06 14:33:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testArrayAccess()
|
|
|
|
{
|
|
|
|
$child = $this->getBuilder('foo')->getForm();
|
|
|
|
|
|
|
|
$this->form[] = $child;
|
|
|
|
|
|
|
|
$this->assertTrue(isset($this->form['foo']));
|
|
|
|
$this->assertSame($child, $this->form['foo']);
|
|
|
|
|
|
|
|
unset($this->form['foo']);
|
|
|
|
|
|
|
|
$this->assertFalse(isset($this->form['foo']));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCountable()
|
|
|
|
{
|
|
|
|
$this->form->add($this->getBuilder('foo')->getForm());
|
|
|
|
$this->form->add($this->getBuilder('bar')->getForm());
|
|
|
|
|
|
|
|
$this->assertCount(2, $this->form);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIterator()
|
|
|
|
{
|
|
|
|
$this->form->add($this->getBuilder('foo')->getForm());
|
|
|
|
$this->form->add($this->getBuilder('bar')->getForm());
|
|
|
|
|
|
|
|
$this->assertSame($this->form->all(), iterator_to_array($this->form));
|
|
|
|
}
|
|
|
|
|
2013-04-29 16:31:30 +01:00
|
|
|
public function testAddMapsViewDataToFormIfInitialized()
|
2012-07-06 14:33:06 +01:00
|
|
|
{
|
|
|
|
$mapper = $this->getDataMapper();
|
|
|
|
$form = $this->getBuilder()
|
|
|
|
->setCompound(true)
|
|
|
|
->setDataMapper($mapper)
|
|
|
|
->addViewTransformer(new FixedDataTransformer(array(
|
|
|
|
'' => '',
|
|
|
|
'foo' => 'bar',
|
|
|
|
)))
|
|
|
|
->setData('foo')
|
|
|
|
->getForm();
|
|
|
|
|
|
|
|
$child = $this->getBuilder()->getForm();
|
|
|
|
$mapper->expects($this->once())
|
|
|
|
->method('mapDataToForms')
|
2013-01-05 13:26:33 +00:00
|
|
|
->with('bar', $this->isInstanceOf('\RecursiveIteratorIterator'))
|
2014-12-03 22:04:33 +00:00
|
|
|
->will($this->returnCallback(function ($data, \RecursiveIteratorIterator $iterator) use ($child) {
|
|
|
|
$this->assertInstanceOf('Symfony\Component\Form\Util\InheritDataAwareIterator', $iterator->getInnerIterator());
|
2015-07-01 21:40:29 +01:00
|
|
|
$this->assertSame(array($child->getName() => $child), iterator_to_array($iterator));
|
2013-01-05 13:26:33 +00:00
|
|
|
}));
|
|
|
|
|
2013-04-29 16:31:30 +01:00
|
|
|
$form->initialize();
|
|
|
|
$form->add($child);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddDoesNotMapViewDataToFormIfNotInitialized()
|
|
|
|
{
|
|
|
|
$mapper = $this->getDataMapper();
|
|
|
|
$form = $this->getBuilder()
|
|
|
|
->setCompound(true)
|
|
|
|
->setDataMapper($mapper)
|
|
|
|
->getForm();
|
|
|
|
|
|
|
|
$child = $this->getBuilder()->getForm();
|
|
|
|
$mapper->expects($this->never())
|
|
|
|
->method('mapDataToForms');
|
|
|
|
|
2013-01-05 13:26:33 +00:00
|
|
|
$form->add($child);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddDoesNotMapViewDataToFormIfInheritData()
|
|
|
|
{
|
|
|
|
$mapper = $this->getDataMapper();
|
|
|
|
$form = $this->getBuilder()
|
|
|
|
->setCompound(true)
|
|
|
|
->setDataMapper($mapper)
|
|
|
|
->setInheritData(true)
|
|
|
|
->getForm();
|
|
|
|
|
|
|
|
$child = $this->getBuilder()->getForm();
|
|
|
|
$mapper->expects($this->never())
|
|
|
|
->method('mapDataToForms');
|
2012-07-06 14:33:06 +01:00
|
|
|
|
2013-04-29 16:31:30 +01:00
|
|
|
$form->initialize();
|
2012-07-06 14:33:06 +01:00
|
|
|
$form->add($child);
|
|
|
|
}
|
|
|
|
|
2013-08-22 13:20:52 +01:00
|
|
|
public function testSetDataSupportsDynamicAdditionAndRemovalOfChildren()
|
|
|
|
{
|
|
|
|
$form = $this->getBuilder()
|
|
|
|
->setCompound(true)
|
|
|
|
// We test using PropertyPathMapper on purpose. The traversal logic
|
|
|
|
// is currently contained in InheritDataAwareIterator, but even
|
|
|
|
// if that changes, this test should still function.
|
|
|
|
->setDataMapper(new PropertyPathMapper())
|
|
|
|
->getForm();
|
|
|
|
|
|
|
|
$child = $this->getMockForm('child');
|
|
|
|
$childToBeRemoved = $this->getMockForm('removed');
|
|
|
|
$childToBeAdded = $this->getMockForm('added');
|
|
|
|
|
|
|
|
$form->add($child);
|
|
|
|
$form->add($childToBeRemoved);
|
|
|
|
|
|
|
|
$child->expects($this->once())
|
|
|
|
->method('setData')
|
|
|
|
->will($this->returnCallback(function () use ($form, $childToBeAdded) {
|
|
|
|
$form->remove('removed');
|
|
|
|
$form->add($childToBeAdded);
|
|
|
|
}));
|
|
|
|
|
|
|
|
$childToBeRemoved->expects($this->never())
|
|
|
|
->method('setData');
|
|
|
|
|
|
|
|
// once when it it is created, once when it is added
|
|
|
|
$childToBeAdded->expects($this->exactly(2))
|
|
|
|
->method('setData');
|
|
|
|
|
|
|
|
// pass NULL to all children
|
|
|
|
$form->setData(array());
|
|
|
|
}
|
|
|
|
|
2012-07-06 14:33:06 +01:00
|
|
|
public function testSetDataMapsViewDataToChildren()
|
|
|
|
{
|
|
|
|
$mapper = $this->getDataMapper();
|
|
|
|
$form = $this->getBuilder()
|
|
|
|
->setCompound(true)
|
|
|
|
->setDataMapper($mapper)
|
|
|
|
->addViewTransformer(new FixedDataTransformer(array(
|
|
|
|
'' => '',
|
|
|
|
'foo' => 'bar',
|
|
|
|
)))
|
|
|
|
->getForm();
|
|
|
|
|
|
|
|
$form->add($child1 = $this->getBuilder('firstName')->getForm());
|
|
|
|
$form->add($child2 = $this->getBuilder('lastName')->getForm());
|
|
|
|
|
|
|
|
$mapper->expects($this->once())
|
|
|
|
->method('mapDataToForms')
|
2013-01-05 13:26:33 +00:00
|
|
|
->with('bar', $this->isInstanceOf('\RecursiveIteratorIterator'))
|
2014-12-03 22:04:33 +00:00
|
|
|
->will($this->returnCallback(function ($data, \RecursiveIteratorIterator $iterator) use ($child1, $child2) {
|
|
|
|
$this->assertInstanceOf('Symfony\Component\Form\Util\InheritDataAwareIterator', $iterator->getInnerIterator());
|
|
|
|
$this->assertSame(array('firstName' => $child1, 'lastName' => $child2), iterator_to_array($iterator));
|
2013-01-05 13:26:33 +00:00
|
|
|
}));
|
2012-07-06 14:33:06 +01:00
|
|
|
|
|
|
|
$form->setData('foo');
|
|
|
|
}
|
|
|
|
|
2013-08-22 12:28:18 +01:00
|
|
|
public function testSubmitSupportsDynamicAdditionAndRemovalOfChildren()
|
2013-08-15 20:04:26 +01:00
|
|
|
{
|
|
|
|
$child = $this->getMockForm('child');
|
|
|
|
$childToBeRemoved = $this->getMockForm('removed');
|
|
|
|
$childToBeAdded = $this->getMockForm('added');
|
|
|
|
|
|
|
|
$this->form->add($child);
|
|
|
|
$this->form->add($childToBeRemoved);
|
|
|
|
|
|
|
|
$form = $this->form;
|
|
|
|
|
|
|
|
$child->expects($this->once())
|
2013-08-22 12:28:18 +01:00
|
|
|
->method('submit')
|
2013-08-15 20:04:26 +01:00
|
|
|
->will($this->returnCallback(function () use ($form, $childToBeAdded) {
|
|
|
|
$form->remove('removed');
|
|
|
|
$form->add($childToBeAdded);
|
|
|
|
}));
|
|
|
|
|
|
|
|
$childToBeRemoved->expects($this->never())
|
2013-08-22 12:28:18 +01:00
|
|
|
->method('submit');
|
2013-08-15 20:04:26 +01:00
|
|
|
|
|
|
|
$childToBeAdded->expects($this->once())
|
2013-08-22 12:28:18 +01:00
|
|
|
->method('submit');
|
2013-08-15 20:04:26 +01:00
|
|
|
|
|
|
|
// pass NULL to all children
|
2013-08-22 12:28:18 +01:00
|
|
|
$this->form->submit(array());
|
2013-08-15 20:04:26 +01:00
|
|
|
}
|
|
|
|
|
2013-04-20 16:32:55 +01:00
|
|
|
public function testSubmitMapsSubmittedChildrenOntoExistingViewData()
|
2012-07-06 14:33:06 +01:00
|
|
|
{
|
|
|
|
$mapper = $this->getDataMapper();
|
|
|
|
$form = $this->getBuilder()
|
|
|
|
->setCompound(true)
|
|
|
|
->setDataMapper($mapper)
|
|
|
|
->addViewTransformer(new FixedDataTransformer(array(
|
|
|
|
'' => '',
|
|
|
|
'foo' => 'bar',
|
|
|
|
)))
|
|
|
|
->setData('foo')
|
|
|
|
->getForm();
|
|
|
|
|
|
|
|
$form->add($child1 = $this->getBuilder('firstName')->setCompound(false)->getForm());
|
|
|
|
$form->add($child2 = $this->getBuilder('lastName')->setCompound(false)->getForm());
|
|
|
|
|
|
|
|
$mapper->expects($this->once())
|
|
|
|
->method('mapFormsToData')
|
2013-01-05 13:26:33 +00:00
|
|
|
->with($this->isInstanceOf('\RecursiveIteratorIterator'), 'bar')
|
2014-12-03 22:04:33 +00:00
|
|
|
->will($this->returnCallback(function (\RecursiveIteratorIterator $iterator) use ($child1, $child2) {
|
|
|
|
$this->assertInstanceOf('Symfony\Component\Form\Util\InheritDataAwareIterator', $iterator->getInnerIterator());
|
|
|
|
$this->assertSame(array('firstName' => $child1, 'lastName' => $child2), iterator_to_array($iterator));
|
|
|
|
$this->assertEquals('Bernhard', $child1->getData());
|
|
|
|
$this->assertEquals('Schussek', $child2->getData());
|
2012-07-06 14:33:06 +01:00
|
|
|
}));
|
|
|
|
|
2013-04-20 16:32:55 +01:00
|
|
|
$form->submit(array(
|
2012-07-06 14:33:06 +01:00
|
|
|
'firstName' => 'Bernhard',
|
|
|
|
'lastName' => 'Schussek',
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2013-01-05 13:26:33 +00:00
|
|
|
public function testMapFormsToDataIsNotInvokedIfInheritData()
|
|
|
|
{
|
|
|
|
$mapper = $this->getDataMapper();
|
|
|
|
$form = $this->getBuilder()
|
|
|
|
->setCompound(true)
|
|
|
|
->setDataMapper($mapper)
|
|
|
|
->setInheritData(true)
|
|
|
|
->addViewTransformer(new FixedDataTransformer(array(
|
|
|
|
'' => '',
|
|
|
|
'foo' => 'bar',
|
|
|
|
)))
|
|
|
|
->getForm();
|
|
|
|
|
|
|
|
$form->add($child1 = $this->getBuilder('firstName')->setCompound(false)->getForm());
|
|
|
|
$form->add($child2 = $this->getBuilder('lastName')->setCompound(false)->getForm());
|
|
|
|
|
|
|
|
$mapper->expects($this->never())
|
|
|
|
->method('mapFormsToData');
|
|
|
|
|
2013-04-20 16:32:55 +01:00
|
|
|
$form->submit(array(
|
2013-01-05 13:26:33 +00:00
|
|
|
'firstName' => 'Bernhard',
|
|
|
|
'lastName' => 'Schussek',
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2012-07-06 14:33:06 +01:00
|
|
|
/*
|
|
|
|
* https://github.com/symfony/symfony/issues/4480
|
|
|
|
*/
|
2013-04-20 16:32:55 +01:00
|
|
|
public function testSubmitRestoresViewDataIfCompoundAndEmpty()
|
2012-07-06 14:33:06 +01:00
|
|
|
{
|
|
|
|
$mapper = $this->getDataMapper();
|
|
|
|
$object = new \stdClass();
|
|
|
|
$form = $this->getBuilder('name', null, 'stdClass')
|
|
|
|
->setCompound(true)
|
|
|
|
->setDataMapper($mapper)
|
|
|
|
->setData($object)
|
|
|
|
->getForm();
|
|
|
|
|
2013-04-20 16:32:55 +01:00
|
|
|
$form->submit(array());
|
2012-07-06 14:33:06 +01:00
|
|
|
|
|
|
|
$this->assertSame($object, $form->getData());
|
|
|
|
}
|
|
|
|
|
2013-04-20 16:32:55 +01:00
|
|
|
public function testSubmitMapsSubmittedChildrenOntoEmptyData()
|
2012-07-06 14:33:06 +01:00
|
|
|
{
|
|
|
|
$mapper = $this->getDataMapper();
|
|
|
|
$object = new \stdClass();
|
|
|
|
$form = $this->getBuilder()
|
|
|
|
->setCompound(true)
|
|
|
|
->setDataMapper($mapper)
|
|
|
|
->setEmptyData($object)
|
|
|
|
->setData(null)
|
|
|
|
->getForm();
|
|
|
|
|
|
|
|
$form->add($child = $this->getBuilder('name')->setCompound(false)->getForm());
|
|
|
|
|
|
|
|
$mapper->expects($this->once())
|
|
|
|
->method('mapFormsToData')
|
2013-01-05 13:26:33 +00:00
|
|
|
->with($this->isInstanceOf('\RecursiveIteratorIterator'), $object)
|
2014-12-03 22:04:33 +00:00
|
|
|
->will($this->returnCallback(function (\RecursiveIteratorIterator $iterator) use ($child) {
|
|
|
|
$this->assertInstanceOf('Symfony\Component\Form\Util\InheritDataAwareIterator', $iterator->getInnerIterator());
|
|
|
|
$this->assertSame(array('name' => $child), iterator_to_array($iterator));
|
2013-01-05 13:26:33 +00:00
|
|
|
}));
|
2012-07-06 14:33:06 +01:00
|
|
|
|
2013-04-20 16:32:55 +01:00
|
|
|
$form->submit(array(
|
2012-07-06 14:33:06 +01:00
|
|
|
'name' => 'Bernhard',
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function requestMethodProvider()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('POST'),
|
|
|
|
array('PUT'),
|
|
|
|
array('DELETE'),
|
|
|
|
array('PATCH'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider requestMethodProvider
|
|
|
|
*/
|
2013-04-20 16:32:55 +01:00
|
|
|
public function testSubmitPostOrPutRequest($method)
|
2012-07-06 14:33:06 +01:00
|
|
|
{
|
|
|
|
$path = tempnam(sys_get_temp_dir(), 'sf2');
|
|
|
|
touch($path);
|
|
|
|
|
|
|
|
$values = array(
|
|
|
|
'author' => array(
|
|
|
|
'name' => 'Bernhard',
|
|
|
|
'image' => array('filename' => 'foobar.png'),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$files = array(
|
|
|
|
'author' => array(
|
|
|
|
'error' => array('image' => UPLOAD_ERR_OK),
|
|
|
|
'name' => array('image' => 'upload.png'),
|
|
|
|
'size' => array('image' => 123),
|
|
|
|
'tmp_name' => array('image' => $path),
|
|
|
|
'type' => array('image' => 'image/png'),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$request = new Request(array(), $values, array(), array(), $files, array(
|
|
|
|
'REQUEST_METHOD' => $method,
|
|
|
|
));
|
|
|
|
|
|
|
|
$form = $this->getBuilder('author')
|
2012-12-30 15:38:36 +00:00
|
|
|
->setMethod($method)
|
2012-07-06 14:33:06 +01:00
|
|
|
->setCompound(true)
|
|
|
|
->setDataMapper($this->getDataMapper())
|
2013-04-20 14:56:42 +01:00
|
|
|
->setRequestHandler(new HttpFoundationRequestHandler())
|
2012-07-06 14:33:06 +01:00
|
|
|
->getForm();
|
|
|
|
$form->add($this->getBuilder('name')->getForm());
|
|
|
|
$form->add($this->getBuilder('image')->getForm());
|
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$form->handleRequest($request);
|
2012-07-06 14:33:06 +01:00
|
|
|
|
|
|
|
$file = new UploadedFile($path, 'upload.png', 'image/png', 123, UPLOAD_ERR_OK);
|
|
|
|
|
|
|
|
$this->assertEquals('Bernhard', $form['name']->getData());
|
|
|
|
$this->assertEquals($file, $form['image']->getData());
|
|
|
|
|
|
|
|
unlink($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider requestMethodProvider
|
|
|
|
*/
|
2013-04-20 16:32:55 +01:00
|
|
|
public function testSubmitPostOrPutRequestWithEmptyRootFormName($method)
|
2012-07-06 14:33:06 +01:00
|
|
|
{
|
|
|
|
$path = tempnam(sys_get_temp_dir(), 'sf2');
|
|
|
|
touch($path);
|
|
|
|
|
|
|
|
$values = array(
|
|
|
|
'name' => 'Bernhard',
|
|
|
|
'extra' => 'data',
|
|
|
|
);
|
|
|
|
|
|
|
|
$files = array(
|
|
|
|
'image' => array(
|
|
|
|
'error' => UPLOAD_ERR_OK,
|
|
|
|
'name' => 'upload.png',
|
|
|
|
'size' => 123,
|
|
|
|
'tmp_name' => $path,
|
|
|
|
'type' => 'image/png',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$request = new Request(array(), $values, array(), array(), $files, array(
|
|
|
|
'REQUEST_METHOD' => $method,
|
|
|
|
));
|
|
|
|
|
|
|
|
$form = $this->getBuilder('')
|
2012-12-30 15:38:36 +00:00
|
|
|
->setMethod($method)
|
2012-07-06 14:33:06 +01:00
|
|
|
->setCompound(true)
|
|
|
|
->setDataMapper($this->getDataMapper())
|
2013-04-20 14:56:42 +01:00
|
|
|
->setRequestHandler(new HttpFoundationRequestHandler())
|
2012-07-06 14:33:06 +01:00
|
|
|
->getForm();
|
|
|
|
$form->add($this->getBuilder('name')->getForm());
|
|
|
|
$form->add($this->getBuilder('image')->getForm());
|
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$form->handleRequest($request);
|
2012-07-06 14:33:06 +01:00
|
|
|
|
|
|
|
$file = new UploadedFile($path, 'upload.png', 'image/png', 123, UPLOAD_ERR_OK);
|
|
|
|
|
|
|
|
$this->assertEquals('Bernhard', $form['name']->getData());
|
|
|
|
$this->assertEquals($file, $form['image']->getData());
|
|
|
|
$this->assertEquals(array('extra' => 'data'), $form->getExtraData());
|
|
|
|
|
|
|
|
unlink($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider requestMethodProvider
|
|
|
|
*/
|
2013-04-20 16:32:55 +01:00
|
|
|
public function testSubmitPostOrPutRequestWithSingleChildForm($method)
|
2012-07-06 14:33:06 +01:00
|
|
|
{
|
|
|
|
$path = tempnam(sys_get_temp_dir(), 'sf2');
|
|
|
|
touch($path);
|
|
|
|
|
|
|
|
$files = array(
|
|
|
|
'image' => array(
|
|
|
|
'error' => UPLOAD_ERR_OK,
|
|
|
|
'name' => 'upload.png',
|
|
|
|
'size' => 123,
|
|
|
|
'tmp_name' => $path,
|
|
|
|
'type' => 'image/png',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$request = new Request(array(), array(), array(), array(), $files, array(
|
|
|
|
'REQUEST_METHOD' => $method,
|
|
|
|
));
|
|
|
|
|
2012-07-09 19:34:45 +01:00
|
|
|
$form = $this->getBuilder('image')
|
2012-12-30 15:38:36 +00:00
|
|
|
->setMethod($method)
|
2013-04-20 14:56:42 +01:00
|
|
|
->setRequestHandler(new HttpFoundationRequestHandler())
|
2012-07-09 19:34:45 +01:00
|
|
|
->getForm();
|
2012-07-06 14:33:06 +01:00
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$form->handleRequest($request);
|
2012-07-06 14:33:06 +01:00
|
|
|
|
|
|
|
$file = new UploadedFile($path, 'upload.png', 'image/png', 123, UPLOAD_ERR_OK);
|
|
|
|
|
|
|
|
$this->assertEquals($file, $form->getData());
|
|
|
|
|
|
|
|
unlink($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider requestMethodProvider
|
|
|
|
*/
|
2013-04-20 16:32:55 +01:00
|
|
|
public function testSubmitPostOrPutRequestWithSingleChildFormUploadedFile($method)
|
2012-07-06 14:33:06 +01:00
|
|
|
{
|
|
|
|
$path = tempnam(sys_get_temp_dir(), 'sf2');
|
|
|
|
touch($path);
|
|
|
|
|
|
|
|
$values = array(
|
|
|
|
'name' => 'Bernhard',
|
|
|
|
);
|
|
|
|
|
|
|
|
$request = new Request(array(), $values, array(), array(), array(), array(
|
|
|
|
'REQUEST_METHOD' => $method,
|
|
|
|
));
|
|
|
|
|
2012-07-09 19:34:45 +01:00
|
|
|
$form = $this->getBuilder('name')
|
2012-12-30 15:38:36 +00:00
|
|
|
->setMethod($method)
|
2013-04-20 14:56:42 +01:00
|
|
|
->setRequestHandler(new HttpFoundationRequestHandler())
|
2012-07-09 19:34:45 +01:00
|
|
|
->getForm();
|
2012-07-06 14:33:06 +01:00
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$form->handleRequest($request);
|
2012-07-06 14:33:06 +01:00
|
|
|
|
|
|
|
$this->assertEquals('Bernhard', $form->getData());
|
|
|
|
|
|
|
|
unlink($path);
|
|
|
|
}
|
|
|
|
|
2013-04-20 16:32:55 +01:00
|
|
|
public function testSubmitGetRequest()
|
2012-07-06 14:33:06 +01:00
|
|
|
{
|
|
|
|
$values = array(
|
|
|
|
'author' => array(
|
|
|
|
'firstName' => 'Bernhard',
|
|
|
|
'lastName' => 'Schussek',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$request = new Request($values, array(), array(), array(), array(), array(
|
|
|
|
'REQUEST_METHOD' => 'GET',
|
|
|
|
));
|
|
|
|
|
|
|
|
$form = $this->getBuilder('author')
|
2012-12-30 15:38:36 +00:00
|
|
|
->setMethod('GET')
|
2012-07-06 14:33:06 +01:00
|
|
|
->setCompound(true)
|
|
|
|
->setDataMapper($this->getDataMapper())
|
2013-04-20 14:56:42 +01:00
|
|
|
->setRequestHandler(new HttpFoundationRequestHandler())
|
2012-07-06 14:33:06 +01:00
|
|
|
->getForm();
|
|
|
|
$form->add($this->getBuilder('firstName')->getForm());
|
|
|
|
$form->add($this->getBuilder('lastName')->getForm());
|
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$form->handleRequest($request);
|
2012-07-06 14:33:06 +01:00
|
|
|
|
|
|
|
$this->assertEquals('Bernhard', $form['firstName']->getData());
|
|
|
|
$this->assertEquals('Schussek', $form['lastName']->getData());
|
|
|
|
}
|
|
|
|
|
2013-04-20 16:32:55 +01:00
|
|
|
public function testSubmitGetRequestWithEmptyRootFormName()
|
2012-07-06 14:33:06 +01:00
|
|
|
{
|
|
|
|
$values = array(
|
|
|
|
'firstName' => 'Bernhard',
|
|
|
|
'lastName' => 'Schussek',
|
2014-09-21 19:53:12 +01:00
|
|
|
'extra' => 'data',
|
2012-07-06 14:33:06 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$request = new Request($values, array(), array(), array(), array(), array(
|
|
|
|
'REQUEST_METHOD' => 'GET',
|
|
|
|
));
|
|
|
|
|
|
|
|
$form = $this->getBuilder('')
|
2012-12-30 15:38:36 +00:00
|
|
|
->setMethod('GET')
|
2012-07-06 14:33:06 +01:00
|
|
|
->setCompound(true)
|
|
|
|
->setDataMapper($this->getDataMapper())
|
2013-04-20 14:56:42 +01:00
|
|
|
->setRequestHandler(new HttpFoundationRequestHandler())
|
2012-07-06 14:33:06 +01:00
|
|
|
->getForm();
|
|
|
|
$form->add($this->getBuilder('firstName')->getForm());
|
|
|
|
$form->add($this->getBuilder('lastName')->getForm());
|
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$form->handleRequest($request);
|
2012-07-06 14:33:06 +01:00
|
|
|
|
|
|
|
$this->assertEquals('Bernhard', $form['firstName']->getData());
|
|
|
|
$this->assertEquals('Schussek', $form['lastName']->getData());
|
|
|
|
$this->assertEquals(array('extra' => 'data'), $form->getExtraData());
|
|
|
|
}
|
|
|
|
|
2013-12-31 16:24:28 +00:00
|
|
|
public function testGetErrors()
|
|
|
|
{
|
|
|
|
$this->form->addError($error1 = new FormError('Error 1'));
|
|
|
|
$this->form->addError($error2 = new FormError('Error 2'));
|
|
|
|
|
|
|
|
$errors = $this->form->getErrors();
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
"ERROR: Error 1\n".
|
|
|
|
"ERROR: Error 2\n",
|
|
|
|
(string) $errors
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertSame(array($error1, $error2), iterator_to_array($errors));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetErrorsDeep()
|
|
|
|
{
|
|
|
|
$this->form->addError($error1 = new FormError('Error 1'));
|
|
|
|
$this->form->addError($error2 = new FormError('Error 2'));
|
|
|
|
|
|
|
|
$childForm = $this->getBuilder('Child')->getForm();
|
|
|
|
$childForm->addError($nestedError = new FormError('Nested Error'));
|
|
|
|
$this->form->add($childForm);
|
|
|
|
|
|
|
|
$errors = $this->form->getErrors(true);
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
"ERROR: Error 1\n".
|
|
|
|
"ERROR: Error 2\n".
|
2014-01-10 12:53:52 +00:00
|
|
|
"ERROR: Nested Error\n",
|
2013-12-31 16:24:28 +00:00
|
|
|
(string) $errors
|
|
|
|
);
|
|
|
|
|
2014-01-10 12:53:52 +00:00
|
|
|
$this->assertSame(
|
|
|
|
array($error1, $error2, $nestedError),
|
|
|
|
iterator_to_array($errors)
|
|
|
|
);
|
2013-12-31 16:24:28 +00:00
|
|
|
}
|
|
|
|
|
2014-01-10 12:53:52 +00:00
|
|
|
public function testGetErrorsDeepRecursive()
|
2013-12-31 16:24:28 +00:00
|
|
|
{
|
|
|
|
$this->form->addError($error1 = new FormError('Error 1'));
|
|
|
|
$this->form->addError($error2 = new FormError('Error 2'));
|
|
|
|
|
|
|
|
$childForm = $this->getBuilder('Child')->getForm();
|
|
|
|
$childForm->addError($nestedError = new FormError('Nested Error'));
|
|
|
|
$this->form->add($childForm);
|
|
|
|
|
2014-01-10 12:53:52 +00:00
|
|
|
$errors = $this->form->getErrors(true, false);
|
2013-12-31 16:24:28 +00:00
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
"ERROR: Error 1\n".
|
|
|
|
"ERROR: Error 2\n".
|
2014-01-10 12:53:52 +00:00
|
|
|
"Child:\n".
|
|
|
|
" ERROR: Nested Error\n",
|
2013-12-31 16:24:28 +00:00
|
|
|
(string) $errors
|
|
|
|
);
|
|
|
|
|
2014-01-10 12:53:52 +00:00
|
|
|
$errorsAsArray = iterator_to_array($errors);
|
|
|
|
|
|
|
|
$this->assertSame($error1, $errorsAsArray[0]);
|
|
|
|
$this->assertSame($error2, $errorsAsArray[1]);
|
|
|
|
$this->assertInstanceOf('Symfony\Component\Form\FormErrorIterator', $errorsAsArray[2]);
|
|
|
|
|
|
|
|
$nestedErrorsAsArray = iterator_to_array($errorsAsArray[2]);
|
|
|
|
|
|
|
|
$this->assertCount(1, $nestedErrorsAsArray);
|
|
|
|
$this->assertSame($nestedError, $nestedErrorsAsArray[0]);
|
2012-07-06 14:33:06 +01:00
|
|
|
}
|
|
|
|
|
2013-09-17 10:39:17 +01:00
|
|
|
// Basic cases are covered in SimpleFormTest
|
|
|
|
public function testCreateViewWithChildren()
|
|
|
|
{
|
2016-12-19 09:02:29 +00:00
|
|
|
$type = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormTypeInterface')->getMock();
|
2013-09-17 10:39:17 +01:00
|
|
|
$options = array('a' => 'Foo', 'b' => 'Bar');
|
|
|
|
$field1 = $this->getMockForm('foo');
|
|
|
|
$field2 = $this->getMockForm('bar');
|
|
|
|
$view = new FormView();
|
|
|
|
$field1View = new FormView();
|
|
|
|
$field2View = new FormView();
|
|
|
|
|
|
|
|
$this->form = $this->getBuilder('form', null, null, $options)
|
|
|
|
->setCompound(true)
|
|
|
|
->setDataMapper($this->getDataMapper())
|
|
|
|
->setType($type)
|
|
|
|
->getForm();
|
|
|
|
$this->form->add($field1);
|
|
|
|
$this->form->add($field2);
|
|
|
|
|
2014-12-03 22:04:33 +00:00
|
|
|
$assertChildViewsEqual = function (array $childViews) {
|
|
|
|
return function (FormView $view) use ($childViews) {
|
|
|
|
$this->assertSame($childViews, $view->children);
|
2013-09-17 10:39:17 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// First create the view
|
|
|
|
$type->expects($this->once())
|
|
|
|
->method('createView')
|
|
|
|
->will($this->returnValue($view));
|
|
|
|
|
|
|
|
// Then build it for the form itself
|
|
|
|
$type->expects($this->once())
|
|
|
|
->method('buildView')
|
|
|
|
->with($view, $this->form, $options)
|
|
|
|
->will($this->returnCallback($assertChildViewsEqual(array())));
|
|
|
|
|
|
|
|
// Then add the first child form
|
|
|
|
$field1->expects($this->once())
|
|
|
|
->method('createView')
|
|
|
|
->will($this->returnValue($field1View));
|
|
|
|
|
|
|
|
// Then the second child form
|
|
|
|
$field2->expects($this->once())
|
|
|
|
->method('createView')
|
|
|
|
->will($this->returnValue($field2View));
|
|
|
|
|
|
|
|
// Again build the view for the form itself. This time the child views
|
|
|
|
// exist.
|
|
|
|
$type->expects($this->once())
|
|
|
|
->method('finishView')
|
|
|
|
->with($view, $this->form, $options)
|
|
|
|
->will($this->returnCallback($assertChildViewsEqual(array('foo' => $field1View, 'bar' => $field2View))));
|
|
|
|
|
|
|
|
$this->assertSame($view, $this->form->createView());
|
|
|
|
}
|
|
|
|
|
2013-11-09 15:25:41 +00:00
|
|
|
public function testNoClickedButtonBeforeSubmission()
|
|
|
|
{
|
|
|
|
$this->assertNull($this->form->getClickedButton());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNoClickedButton()
|
|
|
|
{
|
|
|
|
$button = $this->getMockBuilder('Symfony\Component\Form\SubmitButton')
|
|
|
|
->setConstructorArgs(array(new SubmitButtonBuilder('submit')))
|
|
|
|
->setMethods(array('isClicked'))
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$button->expects($this->any())
|
|
|
|
->method('isClicked')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
|
|
$parentForm = $this->getBuilder('parent')->getForm();
|
|
|
|
$nestedForm = $this->getBuilder('nested')->getForm();
|
|
|
|
|
|
|
|
$this->form->setParent($parentForm);
|
|
|
|
$this->form->add($button);
|
|
|
|
$this->form->add($nestedForm);
|
|
|
|
$this->form->submit(array());
|
|
|
|
|
|
|
|
$this->assertNull($this->form->getClickedButton());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testClickedButton()
|
|
|
|
{
|
|
|
|
$button = $this->getMockBuilder('Symfony\Component\Form\SubmitButton')
|
|
|
|
->setConstructorArgs(array(new SubmitButtonBuilder('submit')))
|
|
|
|
->setMethods(array('isClicked'))
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$button->expects($this->any())
|
|
|
|
->method('isClicked')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$this->form->add($button);
|
|
|
|
$this->form->submit(array());
|
|
|
|
|
|
|
|
$this->assertSame($button, $this->form->getClickedButton());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testClickedButtonFromNestedForm()
|
|
|
|
{
|
|
|
|
$button = $this->getBuilder('submit')->getForm();
|
|
|
|
|
|
|
|
$nestedForm = $this->getMockBuilder('Symfony\Component\Form\Form')
|
|
|
|
->setConstructorArgs(array($this->getBuilder('nested')))
|
|
|
|
->setMethods(array('getClickedButton'))
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$nestedForm->expects($this->any())
|
|
|
|
->method('getClickedButton')
|
|
|
|
->will($this->returnValue($button));
|
|
|
|
|
|
|
|
$this->form->add($nestedForm);
|
|
|
|
$this->form->submit(array());
|
|
|
|
|
|
|
|
$this->assertSame($button, $this->form->getClickedButton());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testClickedButtonFromParentForm()
|
|
|
|
{
|
|
|
|
$button = $this->getBuilder('submit')->getForm();
|
|
|
|
|
|
|
|
$parentForm = $this->getMockBuilder('Symfony\Component\Form\Form')
|
|
|
|
->setConstructorArgs(array($this->getBuilder('parent')))
|
|
|
|
->setMethods(array('getClickedButton'))
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$parentForm->expects($this->any())
|
|
|
|
->method('getClickedButton')
|
|
|
|
->will($this->returnValue($button));
|
|
|
|
|
|
|
|
$this->form->setParent($parentForm);
|
|
|
|
$this->form->submit(array());
|
|
|
|
|
|
|
|
$this->assertSame($button, $this->form->getClickedButton());
|
|
|
|
}
|
|
|
|
|
2012-07-06 14:33:06 +01:00
|
|
|
protected function createForm()
|
|
|
|
{
|
|
|
|
return $this->getBuilder()
|
|
|
|
->setCompound(true)
|
|
|
|
->setDataMapper($this->getDataMapper())
|
|
|
|
->getForm();
|
|
|
|
}
|
|
|
|
}
|