2011-03-21 21:10:53 +00: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\Tests\Component\Form;
|
|
|
|
|
2011-03-31 14:23:33 +01:00
|
|
|
require_once __DIR__.'/Fixtures/FixedDataTransformer.php';
|
2011-04-02 10:22:29 +01:00
|
|
|
require_once __DIR__.'/Fixtures/FixedFilterListener.php';
|
2011-03-31 14:23:33 +01:00
|
|
|
|
2011-03-21 21:10:53 +00:00
|
|
|
use Symfony\Component\Form\Form;
|
2011-04-15 12:41:45 +01:00
|
|
|
use Symfony\Component\Form\FormView;
|
2011-03-21 21:10:53 +00:00
|
|
|
use Symfony\Component\Form\FormBuilder;
|
|
|
|
use Symfony\Component\Form\FormError;
|
2011-04-22 16:41:21 +01:00
|
|
|
use Symfony\Component\Form\Exception\TransformationFailedException;
|
2011-04-02 10:22:29 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher;
|
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2011-03-31 14:23:33 +01:00
|
|
|
use Symfony\Tests\Component\Form\Fixtures\FixedDataTransformer;
|
2011-04-02 10:22:29 +01:00
|
|
|
use Symfony\Tests\Component\Form\Fixtures\FixedFilterListener;
|
2011-03-21 21:10:53 +00:00
|
|
|
|
|
|
|
class FormTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
private $dispatcher;
|
|
|
|
|
2011-04-14 14:25:30 +01:00
|
|
|
private $factory;
|
|
|
|
|
2011-03-21 21:10:53 +00:00
|
|
|
private $builder;
|
|
|
|
|
|
|
|
private $form;
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
$this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
|
2011-04-14 14:25:30 +01:00
|
|
|
$this->factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
|
2011-03-31 14:23:33 +01:00
|
|
|
$this->form = $this->getBuilder()->getForm();
|
|
|
|
}
|
|
|
|
|
2011-06-14 09:44:38 +01:00
|
|
|
protected function tearDown()
|
|
|
|
{
|
|
|
|
$this->dispatcher = null;
|
|
|
|
$this->factory = null;
|
|
|
|
$this->form = null;
|
|
|
|
}
|
|
|
|
|
2011-03-31 14:23:33 +01:00
|
|
|
/**
|
|
|
|
* @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
|
|
|
|
*/
|
|
|
|
public function testConstructExpectsValidValidators()
|
|
|
|
{
|
|
|
|
$validators = array(new \stdClass());
|
|
|
|
|
2011-04-02 15:39:19 +01:00
|
|
|
new Form('name', $this->dispatcher, array(), array(), array(), null, $validators);
|
2011-03-31 14:23:33 +01:00
|
|
|
}
|
|
|
|
|
2012-01-16 23:02:58 +00:00
|
|
|
public function getHtml4Ids()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('a0', true),
|
|
|
|
array('a9', true),
|
|
|
|
array('z0', true),
|
|
|
|
array('A0', true),
|
|
|
|
array('A9', true),
|
|
|
|
array('Z0', true),
|
|
|
|
array('#', false),
|
|
|
|
array('a#', false),
|
|
|
|
array('a$', false),
|
|
|
|
array('a%', false),
|
|
|
|
array('a ', false),
|
|
|
|
array("a\t", false),
|
|
|
|
array("a\n", false),
|
|
|
|
array('a-', true),
|
|
|
|
array('a_', true),
|
|
|
|
array('a:', true),
|
|
|
|
// Periods are allowed by the HTML4 spec, but disallowed by us
|
|
|
|
// because they break the generated property paths
|
|
|
|
array('a.', false),
|
|
|
|
// Contrary to the HTML4 spec, we allow names starting with a
|
|
|
|
// number, otherwise naming fields by collection indices is not
|
|
|
|
// possible.
|
|
|
|
// For root forms, leading digits will be stripped from the
|
|
|
|
// "id" attribute to produce valid HTML4.
|
|
|
|
array('0', true),
|
|
|
|
array('9', true),
|
|
|
|
// Contrary to the HTML4 spec, we allow names starting with an
|
|
|
|
// underscore, since this is already a widely used practice in
|
|
|
|
// Symfony2.
|
|
|
|
// For root forms, leading underscores will be stripped from the
|
|
|
|
// "id" attribute to produce valid HTML4.
|
|
|
|
array('_', true),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getHtml4Ids
|
|
|
|
*/
|
|
|
|
public function testConstructAcceptsOnlyNamesValidAsIdsInHtml4($name, $accepted)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
new Form($name, $this->dispatcher);
|
|
|
|
if (!$accepted) {
|
|
|
|
$this->fail(sprintf('The value "%s" should not be accepted', $name));
|
|
|
|
}
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
// if the value was not accepted, but should be, rethrow exception
|
|
|
|
if ($accepted) {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-31 14:23:33 +01:00
|
|
|
public function testDataIsInitializedEmpty()
|
|
|
|
{
|
|
|
|
$norm = new FixedDataTransformer(array(
|
|
|
|
'' => 'foo',
|
|
|
|
));
|
|
|
|
$client = new FixedDataTransformer(array(
|
|
|
|
'foo' => 'bar',
|
|
|
|
));
|
|
|
|
|
2011-04-02 15:39:19 +01:00
|
|
|
$form = new Form('name', $this->dispatcher, array(), array($client), array($norm));
|
2011-03-31 14:23:33 +01:00
|
|
|
|
|
|
|
$this->assertNull($form->getData());
|
|
|
|
$this->assertSame('foo', $form->getNormData());
|
|
|
|
$this->assertSame('bar', $form->getClientData());
|
2011-03-21 21:10:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testErrorsBubbleUpIfEnabled()
|
|
|
|
{
|
|
|
|
$error = new FormError('Error!');
|
|
|
|
$parent = $this->form;
|
2011-03-31 14:23:33 +01:00
|
|
|
$form = $this->getBuilder()->setErrorBubbling(true)->getForm();
|
2011-03-21 21:10:53 +00:00
|
|
|
|
|
|
|
$form->setParent($parent);
|
|
|
|
$form->addError($error);
|
|
|
|
|
|
|
|
$this->assertEquals(array(), $form->getErrors());
|
|
|
|
$this->assertEquals(array($error), $parent->getErrors());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testErrorsDontBubbleUpIfDisabled()
|
|
|
|
{
|
|
|
|
$error = new FormError('Error!');
|
|
|
|
$parent = $this->form;
|
2011-03-31 14:23:33 +01:00
|
|
|
$form = $this->getBuilder()->setErrorBubbling(false)->getForm();
|
2011-03-21 21:10:53 +00:00
|
|
|
|
|
|
|
$form->setParent($parent);
|
|
|
|
$form->addError($error);
|
|
|
|
|
|
|
|
$this->assertEquals(array($error), $form->getErrors());
|
|
|
|
$this->assertEquals(array(), $parent->getErrors());
|
|
|
|
}
|
2011-03-22 00:40:02 +00:00
|
|
|
|
|
|
|
public function testValidIfAllChildrenAreValid()
|
|
|
|
{
|
|
|
|
$this->form->add($this->getValidForm('firstName'));
|
|
|
|
$this->form->add($this->getValidForm('lastName'));
|
|
|
|
|
|
|
|
$this->form->bind(array(
|
|
|
|
'firstName' => 'Bernhard',
|
|
|
|
'lastName' => 'Schussek',
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertTrue($this->form->isValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvalidIfChildrenIsInvalid()
|
|
|
|
{
|
|
|
|
$this->form->add($this->getValidForm('firstName'));
|
|
|
|
$this->form->add($this->getInvalidForm('lastName'));
|
|
|
|
|
|
|
|
$this->form->bind(array(
|
|
|
|
'firstName' => 'Bernhard',
|
|
|
|
'lastName' => 'Schussek',
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertFalse($this->form->isValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBind()
|
|
|
|
{
|
|
|
|
$child = $this->getMockForm('firstName');
|
|
|
|
|
|
|
|
$this->form->add($child);
|
|
|
|
|
|
|
|
$child->expects($this->once())
|
|
|
|
->method('bind')
|
|
|
|
->with($this->equalTo('Bernhard'));
|
|
|
|
|
|
|
|
$this->form->bind(array('firstName' => 'Bernhard'));
|
|
|
|
|
|
|
|
$this->assertEquals(array('firstName' => 'Bernhard'), $this->form->getData());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBindForwardsNullIfValueIsMissing()
|
|
|
|
{
|
|
|
|
$child = $this->getMockForm('firstName');
|
|
|
|
|
|
|
|
$this->form->add($child);
|
|
|
|
|
|
|
|
$child->expects($this->once())
|
|
|
|
->method('bind')
|
|
|
|
->with($this->equalTo(null));
|
|
|
|
|
|
|
|
$this->form->bind(array());
|
|
|
|
}
|
|
|
|
|
2012-01-26 15:54:42 +00:00
|
|
|
public function testBindIsIgnoredIfDisabled()
|
2011-04-02 10:39:15 +01:00
|
|
|
{
|
|
|
|
$form = $this->getBuilder()
|
2012-01-26 15:54:42 +00:00
|
|
|
->setDisabled(true)
|
2011-04-02 10:39:15 +01:00
|
|
|
->setData('initial')
|
|
|
|
->getForm();
|
|
|
|
|
|
|
|
$form->bind('new');
|
|
|
|
|
|
|
|
$this->assertEquals('initial', $form->getData());
|
2011-05-19 00:24:28 +01:00
|
|
|
$this->assertTrue($form->isBound());
|
2011-04-02 10:39:15 +01:00
|
|
|
}
|
|
|
|
|
2011-03-31 14:23:33 +01:00
|
|
|
public function testNeverRequiredIfParentNotRequired()
|
2011-03-22 00:40:02 +00:00
|
|
|
{
|
2011-03-31 14:23:33 +01:00
|
|
|
$parent = $this->getBuilder()->setRequired(false)->getForm();
|
|
|
|
$child = $this->getBuilder()->setRequired(true)->getForm();
|
|
|
|
|
|
|
|
$child->setParent($parent);
|
|
|
|
|
|
|
|
$this->assertFalse($child->isRequired());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRequired()
|
|
|
|
{
|
|
|
|
$parent = $this->getBuilder()->setRequired(true)->getForm();
|
|
|
|
$child = $this->getBuilder()->setRequired(true)->getForm();
|
|
|
|
|
|
|
|
$child->setParent($parent);
|
|
|
|
|
|
|
|
$this->assertTrue($child->isRequired());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNotRequired()
|
|
|
|
{
|
|
|
|
$parent = $this->getBuilder()->setRequired(true)->getForm();
|
|
|
|
$child = $this->getBuilder()->setRequired(false)->getForm();
|
|
|
|
|
|
|
|
$child->setParent($parent);
|
|
|
|
|
|
|
|
$this->assertFalse($child->isRequired());
|
|
|
|
}
|
|
|
|
|
2012-01-26 15:54:42 +00:00
|
|
|
public function testAlwaysDisabledIfParentDisabled()
|
2011-03-31 14:23:33 +01:00
|
|
|
{
|
2012-01-26 15:54:42 +00:00
|
|
|
$parent = $this->getBuilder()->setDisabled(true)->getForm();
|
|
|
|
$child = $this->getBuilder()->setDisabled(false)->getForm();
|
2011-03-31 14:23:33 +01:00
|
|
|
|
|
|
|
$child->setParent($parent);
|
|
|
|
|
2012-01-26 15:54:42 +00:00
|
|
|
$this->assertTrue($child->isDisabled());
|
2011-03-31 14:23:33 +01:00
|
|
|
}
|
|
|
|
|
2012-01-26 15:54:42 +00:00
|
|
|
public function testDisabled()
|
2011-03-31 14:23:33 +01:00
|
|
|
{
|
2012-01-26 15:54:42 +00:00
|
|
|
$parent = $this->getBuilder()->setDisabled(false)->getForm();
|
|
|
|
$child = $this->getBuilder()->setDisabled(true)->getForm();
|
2011-03-31 14:23:33 +01:00
|
|
|
|
|
|
|
$child->setParent($parent);
|
|
|
|
|
2012-01-26 15:54:42 +00:00
|
|
|
$this->assertTrue($child->isDisabled());
|
2011-03-31 14:23:33 +01:00
|
|
|
}
|
|
|
|
|
2012-01-26 15:54:42 +00:00
|
|
|
public function testNotDisabled()
|
2011-03-31 14:23:33 +01:00
|
|
|
{
|
2012-01-26 15:54:42 +00:00
|
|
|
$parent = $this->getBuilder()->setDisabled(false)->getForm();
|
|
|
|
$child = $this->getBuilder()->setDisabled(false)->getForm();
|
2011-03-31 14:23:33 +01:00
|
|
|
|
|
|
|
$child->setParent($parent);
|
|
|
|
|
2012-01-26 15:54:42 +00:00
|
|
|
$this->assertFalse($child->isDisabled());
|
2011-03-31 14:23:33 +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']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetRootReturnsRootOfParent()
|
|
|
|
{
|
|
|
|
$parent = $this->getMockForm();
|
|
|
|
$parent->expects($this->once())
|
|
|
|
->method('getRoot')
|
|
|
|
->will($this->returnValue('ROOT'));
|
|
|
|
|
|
|
|
$this->form->setParent($parent);
|
|
|
|
|
|
|
|
$this->assertEquals('ROOT', $this->form->getRoot());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetRootReturnsSelfIfNoParent()
|
|
|
|
{
|
|
|
|
$this->assertSame($this->form, $this->form->getRoot());
|
|
|
|
}
|
|
|
|
|
2011-04-06 12:45:19 +01:00
|
|
|
public function testEmptyIfEmptyArray()
|
2011-03-31 14:23:33 +01:00
|
|
|
{
|
|
|
|
$this->form->setData(array());
|
|
|
|
|
|
|
|
$this->assertTrue($this->form->isEmpty());
|
|
|
|
}
|
|
|
|
|
2011-04-06 12:45:19 +01:00
|
|
|
public function testEmptyIfNull()
|
2011-03-31 14:23:33 +01:00
|
|
|
{
|
|
|
|
$this->form->setData(null);
|
|
|
|
|
|
|
|
$this->assertTrue($this->form->isEmpty());
|
|
|
|
}
|
|
|
|
|
2011-04-06 12:45:19 +01:00
|
|
|
public function testEmptyIfEmptyString()
|
2011-03-31 14:23:33 +01:00
|
|
|
{
|
|
|
|
$this->form->setData('');
|
|
|
|
|
|
|
|
$this->assertTrue($this->form->isEmpty());
|
|
|
|
}
|
|
|
|
|
2011-04-06 12:45:19 +01:00
|
|
|
public function testNotEmptyIfText()
|
2011-03-31 14:23:33 +01:00
|
|
|
{
|
|
|
|
$this->form->setData('foobar');
|
|
|
|
|
|
|
|
$this->assertFalse($this->form->isEmpty());
|
|
|
|
}
|
|
|
|
|
2011-04-06 12:45:19 +01:00
|
|
|
public function testNotEmptyIfChildNotEmpty()
|
2011-03-31 14:23:33 +01:00
|
|
|
{
|
|
|
|
$child = $this->getMockForm();
|
|
|
|
$child->expects($this->once())
|
|
|
|
->method('isEmpty')
|
|
|
|
->will($this->returnValue(false));
|
2011-03-22 00:40:02 +00:00
|
|
|
|
2011-03-31 14:23:33 +01:00
|
|
|
$this->form->setData(null);
|
|
|
|
$this->form->add($child);
|
|
|
|
|
|
|
|
$this->assertFalse($this->form->isEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidIfBound()
|
|
|
|
{
|
|
|
|
$this->form->bind('foobar');
|
|
|
|
|
|
|
|
$this->assertTrue($this->form->isValid());
|
|
|
|
}
|
|
|
|
|
2012-01-26 15:54:42 +00:00
|
|
|
public function testValidIfBoundAndDisabled()
|
2011-05-19 00:24:28 +01:00
|
|
|
{
|
2012-01-26 15:54:42 +00:00
|
|
|
$form = $this->getBuilder()->setDisabled(true)->getForm();
|
2011-05-19 00:24:28 +01:00
|
|
|
$form->bind('foobar');
|
|
|
|
|
2011-06-10 15:11:50 +01:00
|
|
|
$this->assertTrue($form->isValid());
|
|
|
|
}
|
|
|
|
|
2012-01-26 15:54:42 +00:00
|
|
|
public function testValidIfBoundAndDisabledWithChildren()
|
2011-06-10 15:11:50 +01:00
|
|
|
{
|
|
|
|
$this->factory->expects($this->once())
|
|
|
|
->method('createNamedBuilder')
|
|
|
|
->with('text', 'name', null, array())
|
|
|
|
->will($this->returnValue($this->getBuilder('name')));
|
|
|
|
|
|
|
|
$form = $this->getBuilder('person')
|
2012-01-26 15:54:42 +00:00
|
|
|
->setDisabled(true)
|
2011-06-10 15:11:50 +01:00
|
|
|
->add('name', 'text')
|
|
|
|
->getForm();
|
|
|
|
$form->bind(array('name' => 'Jacques Doe'));
|
|
|
|
|
2011-05-19 00:24:28 +01:00
|
|
|
$this->assertTrue($form->isValid());
|
|
|
|
}
|
|
|
|
|
2011-07-04 11:12:42 +01:00
|
|
|
/**
|
|
|
|
* @expectedException \LogicException
|
|
|
|
*/
|
2011-03-31 14:23:33 +01:00
|
|
|
public function testNotValidIfNotBound()
|
|
|
|
{
|
2011-07-04 11:12:42 +01:00
|
|
|
$this->form->isValid();
|
2011-03-31 14:23:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNotValidIfErrors()
|
|
|
|
{
|
|
|
|
$this->form->bind('foobar');
|
|
|
|
$this->form->addError(new FormError('Error!'));
|
|
|
|
|
|
|
|
$this->assertFalse($this->form->isValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNotValidIfChildNotValid()
|
|
|
|
{
|
|
|
|
$child = $this->getMockForm();
|
2011-03-22 00:40:02 +00:00
|
|
|
$child->expects($this->once())
|
2011-03-31 14:23:33 +01:00
|
|
|
->method('isValid')
|
|
|
|
->will($this->returnValue(false));
|
2011-03-22 00:40:02 +00:00
|
|
|
|
2011-03-31 14:23:33 +01:00
|
|
|
$this->form->bind('foobar');
|
2011-03-22 00:40:02 +00:00
|
|
|
$this->form->add($child);
|
2011-03-31 14:23:33 +01:00
|
|
|
|
|
|
|
$this->assertFalse($this->form->isValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHasErrors()
|
|
|
|
{
|
|
|
|
$this->form->addError(new FormError('Error!'));
|
|
|
|
|
|
|
|
$this->assertTrue($this->form->hasErrors());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHasNoErrors()
|
|
|
|
{
|
|
|
|
$this->assertFalse($this->form->hasErrors());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHasChildren()
|
|
|
|
{
|
|
|
|
$this->form->add($this->getBuilder()->getForm());
|
|
|
|
|
|
|
|
$this->assertTrue($this->form->hasChildren());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHasNoChildren()
|
|
|
|
{
|
|
|
|
$this->assertFalse($this->form->hasChildren());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAdd()
|
|
|
|
{
|
|
|
|
$child = $this->getBuilder('foo')->getForm();
|
|
|
|
$this->form->add($child);
|
|
|
|
|
|
|
|
$this->assertSame($this->form, $child->getParent());
|
|
|
|
$this->assertSame(array('foo' => $child), $this->form->getChildren());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemove()
|
|
|
|
{
|
|
|
|
$child = $this->getBuilder('foo')->getForm();
|
|
|
|
$this->form->add($child);
|
|
|
|
$this->form->remove('foo');
|
|
|
|
|
|
|
|
$this->assertNull($child->getParent());
|
|
|
|
$this->assertFalse($this->form->hasChildren());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemoveIgnoresUnknownName()
|
|
|
|
{
|
|
|
|
$this->form->remove('notexisting');
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
2012-01-18 13:42:47 +00:00
|
|
|
$this->assertCount(2, $this->form);
|
2011-03-31 14:23:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIterator()
|
|
|
|
{
|
|
|
|
$this->form->add($this->getBuilder('foo')->getForm());
|
|
|
|
$this->form->add($this->getBuilder('bar')->getForm());
|
|
|
|
|
|
|
|
$this->assertSame($this->form->getChildren(), iterator_to_array($this->form));
|
|
|
|
}
|
|
|
|
|
2011-04-06 12:45:19 +01:00
|
|
|
public function testBound()
|
2011-03-31 14:23:33 +01:00
|
|
|
{
|
|
|
|
$this->form->bind('foobar');
|
|
|
|
|
|
|
|
$this->assertTrue($this->form->isBound());
|
|
|
|
}
|
|
|
|
|
2011-04-06 12:45:19 +01:00
|
|
|
public function testNotBound()
|
2011-03-31 14:23:33 +01:00
|
|
|
{
|
|
|
|
$this->assertFalse($this->form->isBound());
|
|
|
|
}
|
|
|
|
|
2011-04-02 10:22:29 +01:00
|
|
|
public function testSetDataExecutesTransformationChain()
|
|
|
|
{
|
|
|
|
// use real event dispatcher now
|
|
|
|
$form = $this->getBuilder('name', new EventDispatcher())
|
|
|
|
->addEventSubscriber(new FixedFilterListener(array(
|
2011-04-15 09:57:32 +01:00
|
|
|
'onSetData' => array(
|
2011-04-02 10:22:29 +01:00
|
|
|
'app' => 'filtered',
|
|
|
|
),
|
|
|
|
)))
|
2011-04-02 15:39:19 +01:00
|
|
|
->appendNormTransformer(new FixedDataTransformer(array(
|
2011-04-02 10:22:29 +01:00
|
|
|
'' => '',
|
|
|
|
'filtered' => 'norm',
|
|
|
|
)))
|
2011-04-02 15:39:19 +01:00
|
|
|
->appendClientTransformer(new FixedDataTransformer(array(
|
2011-04-02 10:22:29 +01:00
|
|
|
'' => '',
|
|
|
|
'norm' => 'client',
|
|
|
|
)))
|
|
|
|
->getForm();
|
|
|
|
|
|
|
|
$form->setData('app');
|
|
|
|
|
|
|
|
$this->assertEquals('filtered', $form->getData());
|
|
|
|
$this->assertEquals('norm', $form->getNormData());
|
|
|
|
$this->assertEquals('client', $form->getClientData());
|
|
|
|
}
|
|
|
|
|
2011-04-02 15:39:19 +01:00
|
|
|
public function testSetDataExecutesClientTransformersInOrder()
|
|
|
|
{
|
|
|
|
$form = $this->getBuilder()
|
|
|
|
->appendClientTransformer(new FixedDataTransformer(array(
|
|
|
|
'' => '',
|
|
|
|
'first' => 'second',
|
|
|
|
)))
|
|
|
|
->appendClientTransformer(new FixedDataTransformer(array(
|
|
|
|
'' => '',
|
|
|
|
'second' => 'third',
|
|
|
|
)))
|
|
|
|
->getForm();
|
|
|
|
|
|
|
|
$form->setData('first');
|
|
|
|
|
|
|
|
$this->assertEquals('third', $form->getClientData());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetDataExecutesNormTransformersInOrder()
|
|
|
|
{
|
|
|
|
$form = $this->getBuilder()
|
|
|
|
->appendNormTransformer(new FixedDataTransformer(array(
|
|
|
|
'' => '',
|
|
|
|
'first' => 'second',
|
|
|
|
)))
|
|
|
|
->appendNormTransformer(new FixedDataTransformer(array(
|
|
|
|
'' => '',
|
|
|
|
'second' => 'third',
|
|
|
|
)))
|
|
|
|
->getForm();
|
|
|
|
|
|
|
|
$form->setData('first');
|
|
|
|
|
|
|
|
$this->assertEquals('third', $form->getNormData());
|
|
|
|
}
|
|
|
|
|
2011-04-06 12:45:19 +01:00
|
|
|
/*
|
|
|
|
* When there is no data transformer, the data must have the same format
|
|
|
|
* in all three representations
|
|
|
|
*/
|
|
|
|
public function testSetDataConvertsScalarToStringIfNoTransformer()
|
|
|
|
{
|
|
|
|
$form = $this->getBuilder()->getForm();
|
|
|
|
|
|
|
|
$form->setData(1);
|
|
|
|
|
|
|
|
$this->assertSame('1', $form->getData());
|
|
|
|
$this->assertSame('1', $form->getNormData());
|
|
|
|
$this->assertSame('1', $form->getClientData());
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Data in client format should, if possible, always be a string to
|
|
|
|
* facilitate differentiation between '0' and ''
|
|
|
|
*/
|
|
|
|
public function testSetDataConvertsScalarToStringIfOnlyNormTransformer()
|
|
|
|
{
|
|
|
|
$form = $this->getBuilder()
|
|
|
|
->appendNormTransformer(new FixedDataTransformer(array(
|
|
|
|
'' => '',
|
|
|
|
1 => 23,
|
|
|
|
)))
|
|
|
|
->getForm();
|
|
|
|
|
|
|
|
$form->setData(1);
|
|
|
|
|
|
|
|
$this->assertSame(1, $form->getData());
|
|
|
|
$this->assertSame(23, $form->getNormData());
|
|
|
|
$this->assertSame('23', $form->getClientData());
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NULL remains NULL in app and norm format to remove the need to treat
|
|
|
|
* empty values and NULL explicitely in the application
|
|
|
|
*/
|
|
|
|
public function testSetDataConvertsNullToStringIfNoTransformer()
|
|
|
|
{
|
|
|
|
$form = $this->getBuilder()->getForm();
|
|
|
|
|
|
|
|
$form->setData(null);
|
|
|
|
|
|
|
|
$this->assertNull($form->getData());
|
|
|
|
$this->assertNull($form->getNormData());
|
|
|
|
$this->assertSame('', $form->getClientData());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBindConvertsEmptyToNullIfNoTransformer()
|
|
|
|
{
|
|
|
|
$form = $this->getBuilder()->getForm();
|
|
|
|
|
|
|
|
$form->bind('');
|
|
|
|
|
|
|
|
$this->assertNull($form->getData());
|
|
|
|
$this->assertNull($form->getNormData());
|
|
|
|
$this->assertSame('', $form->getClientData());
|
|
|
|
}
|
|
|
|
|
2011-04-02 10:22:29 +01:00
|
|
|
public function testBindExecutesTransformationChain()
|
|
|
|
{
|
|
|
|
// use real event dispatcher now
|
|
|
|
$form = $this->getBuilder('name', new EventDispatcher())
|
|
|
|
->addEventSubscriber(new FixedFilterListener(array(
|
2011-04-15 09:57:32 +01:00
|
|
|
'onBindClientData' => array(
|
2011-04-02 10:22:29 +01:00
|
|
|
'client' => 'filteredclient',
|
|
|
|
),
|
2011-04-15 09:57:32 +01:00
|
|
|
'onBindNormData' => array(
|
2011-04-02 10:22:29 +01:00
|
|
|
'norm' => 'filterednorm',
|
|
|
|
),
|
|
|
|
)))
|
2011-04-02 15:39:19 +01:00
|
|
|
->appendClientTransformer(new FixedDataTransformer(array(
|
2011-04-02 10:22:29 +01:00
|
|
|
'' => '',
|
|
|
|
// direction is reversed!
|
|
|
|
'norm' => 'filteredclient',
|
|
|
|
'filterednorm' => 'cleanedclient'
|
|
|
|
)))
|
2011-04-02 15:39:19 +01:00
|
|
|
->appendNormTransformer(new FixedDataTransformer(array(
|
2011-04-02 10:22:29 +01:00
|
|
|
'' => '',
|
|
|
|
// direction is reversed!
|
|
|
|
'app' => 'filterednorm',
|
|
|
|
)))
|
|
|
|
->getForm();
|
|
|
|
|
|
|
|
$form->setData('app');
|
|
|
|
|
|
|
|
$this->assertEquals('app', $form->getData());
|
|
|
|
$this->assertEquals('filterednorm', $form->getNormData());
|
|
|
|
$this->assertEquals('cleanedclient', $form->getClientData());
|
|
|
|
}
|
|
|
|
|
2011-04-02 15:39:19 +01:00
|
|
|
public function testBindExecutesClientTransformersInReverseOrder()
|
|
|
|
{
|
|
|
|
$form = $this->getBuilder()
|
|
|
|
->appendClientTransformer(new FixedDataTransformer(array(
|
|
|
|
'' => '',
|
|
|
|
'third' => 'second',
|
|
|
|
)))
|
|
|
|
->appendClientTransformer(new FixedDataTransformer(array(
|
|
|
|
'' => '',
|
|
|
|
'second' => 'first',
|
|
|
|
)))
|
|
|
|
->getForm();
|
|
|
|
|
|
|
|
$form->bind('first');
|
|
|
|
|
|
|
|
$this->assertEquals('third', $form->getNormData());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBindExecutesNormTransformersInReverseOrder()
|
|
|
|
{
|
|
|
|
$form = $this->getBuilder()
|
|
|
|
->appendNormTransformer(new FixedDataTransformer(array(
|
|
|
|
'' => '',
|
|
|
|
'third' => 'second',
|
|
|
|
)))
|
|
|
|
->appendNormTransformer(new FixedDataTransformer(array(
|
|
|
|
'' => '',
|
|
|
|
'second' => 'first',
|
|
|
|
)))
|
|
|
|
->getForm();
|
|
|
|
|
|
|
|
$form->bind('first');
|
|
|
|
|
|
|
|
$this->assertEquals('third', $form->getData());
|
|
|
|
}
|
|
|
|
|
2011-04-06 12:45:19 +01:00
|
|
|
public function testSynchronizedByDefault()
|
2011-04-02 10:22:29 +01:00
|
|
|
{
|
|
|
|
$this->assertTrue($this->form->isSynchronized());
|
|
|
|
}
|
|
|
|
|
2011-04-06 12:45:19 +01:00
|
|
|
public function testSynchronizedAfterBinding()
|
2011-04-02 10:22:29 +01:00
|
|
|
{
|
|
|
|
$this->form->bind('foobar');
|
|
|
|
|
|
|
|
$this->assertTrue($this->form->isSynchronized());
|
|
|
|
}
|
|
|
|
|
2011-04-06 12:45:19 +01:00
|
|
|
public function testNotSynchronizedIfTransformationFailed()
|
2011-04-02 10:22:29 +01:00
|
|
|
{
|
|
|
|
$transformer = $this->getDataTransformer();
|
|
|
|
$transformer->expects($this->once())
|
|
|
|
->method('reverseTransform')
|
|
|
|
->will($this->throwException(new TransformationFailedException()));
|
|
|
|
|
|
|
|
$form = $this->getBuilder()
|
2011-04-02 15:39:19 +01:00
|
|
|
->appendClientTransformer($transformer)
|
2011-04-02 10:22:29 +01:00
|
|
|
->getForm();
|
|
|
|
|
|
|
|
$form->bind('foobar');
|
|
|
|
|
|
|
|
$this->assertFalse($form->isSynchronized());
|
|
|
|
}
|
|
|
|
|
2011-04-02 11:00:19 +01:00
|
|
|
public function testEmptyDataCreatedBeforeTransforming()
|
|
|
|
{
|
|
|
|
$form = $this->getBuilder()
|
|
|
|
->setEmptyData('foo')
|
2011-04-02 15:39:19 +01:00
|
|
|
->appendClientTransformer(new FixedDataTransformer(array(
|
2011-04-02 11:00:19 +01:00
|
|
|
'' => '',
|
|
|
|
// direction is reversed!
|
|
|
|
'bar' => 'foo',
|
|
|
|
)))
|
|
|
|
->getForm();
|
|
|
|
|
|
|
|
$form->bind('');
|
|
|
|
|
|
|
|
$this->assertEquals('bar', $form->getData());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEmptyDataFromClosure()
|
|
|
|
{
|
|
|
|
$test = $this;
|
|
|
|
$form = $this->getBuilder()
|
|
|
|
->setEmptyData(function ($form) use ($test) {
|
|
|
|
// the form instance is passed to the closure to allow use
|
|
|
|
// of form data when creating the empty value
|
|
|
|
$test->assertInstanceOf('Symfony\Component\Form\FormInterface', $form);
|
|
|
|
|
|
|
|
return 'foo';
|
|
|
|
})
|
2011-04-02 15:39:19 +01:00
|
|
|
->appendClientTransformer(new FixedDataTransformer(array(
|
2011-04-02 11:00:19 +01:00
|
|
|
'' => '',
|
|
|
|
// direction is reversed!
|
|
|
|
'bar' => 'foo',
|
|
|
|
)))
|
|
|
|
->getForm();
|
|
|
|
|
|
|
|
$form->bind('');
|
|
|
|
|
|
|
|
$this->assertEquals('bar', $form->getData());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddMapsClientDataToForm()
|
|
|
|
{
|
|
|
|
$mapper = $this->getDataMapper();
|
|
|
|
$form = $this->getBuilder()
|
|
|
|
->setDataMapper($mapper)
|
2011-04-02 15:39:19 +01:00
|
|
|
->appendClientTransformer(new FixedDataTransformer(array(
|
2011-04-02 11:00:19 +01:00
|
|
|
'' => '',
|
|
|
|
'foo' => 'bar',
|
|
|
|
)))
|
|
|
|
->setData('foo')
|
|
|
|
->getForm();
|
|
|
|
|
|
|
|
$child = $this->getBuilder()->getForm();
|
|
|
|
$mapper->expects($this->once())
|
|
|
|
->method('mapDataToForm')
|
|
|
|
->with('bar', $child);
|
|
|
|
|
|
|
|
$form->add($child);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetDataMapsClientDataToChildren()
|
|
|
|
{
|
|
|
|
$mapper = $this->getDataMapper();
|
|
|
|
$form = $this->getBuilder()
|
|
|
|
->setDataMapper($mapper)
|
2011-04-02 15:39:19 +01:00
|
|
|
->appendClientTransformer(new FixedDataTransformer(array(
|
2011-04-02 11:00:19 +01:00
|
|
|
'' => '',
|
|
|
|
'foo' => 'bar',
|
|
|
|
)))
|
|
|
|
->getForm();
|
|
|
|
|
|
|
|
$form->add($child1 = $this->getBuilder('firstName')->getForm());
|
|
|
|
$form->add($child2 = $this->getBuilder('lastName')->getForm());
|
|
|
|
|
|
|
|
$mapper->expects($this->once())
|
|
|
|
->method('mapDataToForms')
|
|
|
|
->with('bar', array('firstName' => $child1, 'lastName' => $child2));
|
|
|
|
|
|
|
|
$form->setData('foo');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBindMapsBoundChildrenOntoExistingClientData()
|
|
|
|
{
|
|
|
|
$test = $this;
|
|
|
|
$mapper = $this->getDataMapper();
|
|
|
|
$form = $this->getBuilder()
|
|
|
|
->setDataMapper($mapper)
|
2011-04-02 15:39:19 +01:00
|
|
|
->appendClientTransformer(new FixedDataTransformer(array(
|
2011-04-02 11:00:19 +01:00
|
|
|
'' => '',
|
|
|
|
'foo' => 'bar',
|
|
|
|
)))
|
|
|
|
->setData('foo')
|
|
|
|
->getForm();
|
|
|
|
|
|
|
|
$form->add($child1 = $this->getBuilder('firstName')->getForm());
|
|
|
|
$form->add($child2 = $this->getBuilder('lastName')->getForm());
|
|
|
|
|
|
|
|
$mapper->expects($this->once())
|
|
|
|
->method('mapFormsToData')
|
|
|
|
->with(array('firstName' => $child1, 'lastName' => $child2), 'bar')
|
|
|
|
->will($this->returnCallback(function ($children, $bar) use ($test) {
|
|
|
|
$test->assertEquals('Bernhard', $children['firstName']->getData());
|
|
|
|
$test->assertEquals('Schussek', $children['lastName']->getData());
|
|
|
|
}));
|
|
|
|
|
|
|
|
$form->bind(array(
|
|
|
|
'firstName' => 'Bernhard',
|
|
|
|
'lastName' => 'Schussek',
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBindMapsBoundChildrenOntoEmptyData()
|
|
|
|
{
|
|
|
|
$test = $this;
|
|
|
|
$mapper = $this->getDataMapper();
|
|
|
|
$object = new \stdClass();
|
|
|
|
$form = $this->getBuilder()
|
|
|
|
->setDataMapper($mapper)
|
|
|
|
->setEmptyData($object)
|
|
|
|
->setData(null)
|
|
|
|
->getForm();
|
|
|
|
|
|
|
|
$form->add($child = $this->getBuilder('name')->getForm());
|
|
|
|
|
|
|
|
$mapper->expects($this->once())
|
|
|
|
->method('mapFormsToData')
|
|
|
|
->with(array('name' => $child), $object);
|
|
|
|
|
|
|
|
$form->bind(array(
|
|
|
|
'name' => 'Bernhard',
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2011-04-02 10:22:29 +01:00
|
|
|
public function testBindValidatesAfterTransformation()
|
|
|
|
{
|
|
|
|
$test = $this;
|
|
|
|
$validator = $this->getFormValidator();
|
|
|
|
$form = $this->getBuilder()
|
|
|
|
->addValidator($validator)
|
|
|
|
->getForm();
|
|
|
|
|
|
|
|
$validator->expects($this->once())
|
|
|
|
->method('validate')
|
|
|
|
->with($form)
|
|
|
|
->will($this->returnCallback(function ($form) use ($test) {
|
|
|
|
$test->assertEquals('foobar', $form->getData());
|
|
|
|
}));
|
|
|
|
|
|
|
|
$form->bind('foobar');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function requestMethodProvider()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('POST'),
|
|
|
|
array('PUT'),
|
2012-01-20 00:04:31 +00:00
|
|
|
array('DELETE'),
|
2012-01-24 18:46:37 +00:00
|
|
|
array('PATCH'),
|
2011-04-02 10:22:29 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider requestMethodProvider
|
|
|
|
*/
|
|
|
|
public function testBindPostOrPutRequest($method)
|
|
|
|
{
|
|
|
|
$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')->getForm();
|
|
|
|
$form->add($this->getBuilder('name')->getForm());
|
|
|
|
$form->add($this->getBuilder('image')->getForm());
|
|
|
|
|
|
|
|
$form->bindRequest($request);
|
|
|
|
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2012-01-07 14:14:50 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider requestMethodProvider
|
|
|
|
*/
|
|
|
|
public function testBindPostOrPutRequestWithEmptyRootFormName($method)
|
|
|
|
{
|
|
|
|
$path = tempnam(sys_get_temp_dir(), 'sf2');
|
|
|
|
touch($path);
|
|
|
|
|
|
|
|
$values = array(
|
|
|
|
'name' => 'Bernhard',
|
|
|
|
'image' => array('filename' => 'foobar.png'),
|
|
|
|
'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('')->getForm();
|
|
|
|
$form->add($this->getBuilder('name')->getForm());
|
|
|
|
$form->add($this->getBuilder('image')->getForm());
|
|
|
|
|
|
|
|
$form->bindRequest($request);
|
|
|
|
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2011-04-02 10:22:29 +01:00
|
|
|
public function testBindGetRequest()
|
|
|
|
{
|
|
|
|
$values = array(
|
|
|
|
'author' => array(
|
|
|
|
'firstName' => 'Bernhard',
|
|
|
|
'lastName' => 'Schussek',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$request = new Request($values, array(), array(), array(), array(), array(
|
|
|
|
'REQUEST_METHOD' => 'GET',
|
|
|
|
));
|
|
|
|
|
|
|
|
$form = $this->getBuilder('author')->getForm();
|
|
|
|
$form->add($this->getBuilder('firstName')->getForm());
|
|
|
|
$form->add($this->getBuilder('lastName')->getForm());
|
|
|
|
|
|
|
|
$form->bindRequest($request);
|
|
|
|
|
|
|
|
$this->assertEquals('Bernhard', $form['firstName']->getData());
|
|
|
|
$this->assertEquals('Schussek', $form['lastName']->getData());
|
|
|
|
}
|
|
|
|
|
2012-01-07 14:14:50 +00:00
|
|
|
public function testBindGetRequestWithEmptyRootFormName()
|
|
|
|
{
|
|
|
|
$values = array(
|
|
|
|
'firstName' => 'Bernhard',
|
|
|
|
'lastName' => 'Schussek',
|
|
|
|
'extra' => 'data'
|
|
|
|
);
|
|
|
|
|
|
|
|
$request = new Request($values, array(), array(), array(), array(), array(
|
|
|
|
'REQUEST_METHOD' => 'GET',
|
|
|
|
));
|
|
|
|
|
|
|
|
$form = $this->getBuilder('')->getForm();
|
|
|
|
$form->add($this->getBuilder('firstName')->getForm());
|
|
|
|
$form->add($this->getBuilder('lastName')->getForm());
|
|
|
|
|
|
|
|
$form->bindRequest($request);
|
|
|
|
|
|
|
|
$this->assertEquals('Bernhard', $form['firstName']->getData());
|
|
|
|
$this->assertEquals('Schussek', $form['lastName']->getData());
|
|
|
|
$this->assertEquals(array('extra' => 'data'), $form->getExtraData());
|
|
|
|
}
|
|
|
|
|
2011-04-06 12:45:19 +01:00
|
|
|
public function testBindResetsErrors()
|
|
|
|
{
|
|
|
|
$form = $this->getBuilder()->getForm();
|
|
|
|
$form->addError(new FormError('Error!'));
|
|
|
|
$form->bind('foobar');
|
|
|
|
|
|
|
|
$this->assertSame(array(), $form->getErrors());
|
|
|
|
}
|
|
|
|
|
2011-04-15 12:52:11 +01:00
|
|
|
public function testCreateView()
|
2011-04-15 12:41:45 +01:00
|
|
|
{
|
|
|
|
$test = $this;
|
2011-04-22 16:41:21 +01:00
|
|
|
$type1 = $this->getMock('Symfony\Component\Form\FormTypeInterface');
|
2011-04-22 18:22:26 +01:00
|
|
|
$type1Extension = $this->getMock('Symfony\Component\Form\FormTypeExtensionInterface');
|
|
|
|
$type1->expects($this->any())
|
|
|
|
->method('getExtensions')
|
|
|
|
->will($this->returnValue(array($type1Extension)));
|
2011-04-22 16:41:21 +01:00
|
|
|
$type2 = $this->getMock('Symfony\Component\Form\FormTypeInterface');
|
2011-04-22 18:22:26 +01:00
|
|
|
$type2Extension = $this->getMock('Symfony\Component\Form\FormTypeExtensionInterface');
|
|
|
|
$type2->expects($this->any())
|
|
|
|
->method('getExtensions')
|
|
|
|
->will($this->returnValue(array($type2Extension)));
|
2011-04-15 12:41:45 +01:00
|
|
|
$calls = array();
|
|
|
|
|
|
|
|
$type1->expects($this->once())
|
|
|
|
->method('buildView')
|
|
|
|
->will($this->returnCallback(function (FormView $view, Form $form) use ($test, &$calls) {
|
|
|
|
$calls[] = 'type1::buildView';
|
|
|
|
$test->assertTrue($view->hasParent());
|
|
|
|
$test->assertFalse($view->hasChildren());
|
|
|
|
}));
|
|
|
|
|
2011-04-22 18:22:26 +01:00
|
|
|
$type1Extension->expects($this->once())
|
|
|
|
->method('buildView')
|
|
|
|
->will($this->returnCallback(function (FormView $view, Form $form) use ($test, &$calls) {
|
|
|
|
$calls[] = 'type1ext::buildView';
|
|
|
|
$test->assertTrue($view->hasParent());
|
|
|
|
$test->assertFalse($view->hasChildren());
|
|
|
|
}));
|
|
|
|
|
2011-04-15 12:41:45 +01:00
|
|
|
$type2->expects($this->once())
|
|
|
|
->method('buildView')
|
|
|
|
->will($this->returnCallback(function (FormView $view, Form $form) use ($test, &$calls) {
|
|
|
|
$calls[] = 'type2::buildView';
|
|
|
|
$test->assertTrue($view->hasParent());
|
|
|
|
$test->assertFalse($view->hasChildren());
|
|
|
|
}));
|
|
|
|
|
2011-04-22 18:22:26 +01:00
|
|
|
$type2Extension->expects($this->once())
|
|
|
|
->method('buildView')
|
|
|
|
->will($this->returnCallback(function (FormView $view, Form $form) use ($test, &$calls) {
|
|
|
|
$calls[] = 'type2ext::buildView';
|
|
|
|
$test->assertTrue($view->hasParent());
|
|
|
|
$test->assertFalse($view->hasChildren());
|
|
|
|
}));
|
|
|
|
|
2011-04-15 12:41:45 +01:00
|
|
|
$type1->expects($this->once())
|
|
|
|
->method('buildViewBottomUp')
|
|
|
|
->will($this->returnCallback(function (FormView $view, Form $form) use ($test, &$calls) {
|
|
|
|
$calls[] = 'type1::buildViewBottomUp';
|
|
|
|
$test->assertTrue($view->hasChildren());
|
|
|
|
}));
|
|
|
|
|
2011-04-22 18:22:26 +01:00
|
|
|
$type1Extension->expects($this->once())
|
|
|
|
->method('buildViewBottomUp')
|
|
|
|
->will($this->returnCallback(function (FormView $view, Form $form) use ($test, &$calls) {
|
|
|
|
$calls[] = 'type1ext::buildViewBottomUp';
|
|
|
|
$test->assertTrue($view->hasChildren());
|
|
|
|
}));
|
|
|
|
|
2011-04-15 12:41:45 +01:00
|
|
|
$type2->expects($this->once())
|
|
|
|
->method('buildViewBottomUp')
|
|
|
|
->will($this->returnCallback(function (FormView $view, Form $form) use ($test, &$calls) {
|
|
|
|
$calls[] = 'type2::buildViewBottomUp';
|
|
|
|
$test->assertTrue($view->hasChildren());
|
|
|
|
}));
|
|
|
|
|
2011-04-22 18:22:26 +01:00
|
|
|
$type2Extension->expects($this->once())
|
|
|
|
->method('buildViewBottomUp')
|
|
|
|
->will($this->returnCallback(function (FormView $view, Form $form) use ($test, &$calls) {
|
|
|
|
$calls[] = 'type2ext::buildViewBottomUp';
|
|
|
|
$test->assertTrue($view->hasChildren());
|
|
|
|
}));
|
|
|
|
|
2011-04-15 12:41:45 +01:00
|
|
|
$form = $this->getBuilder()->setTypes(array($type1, $type2))->getForm();
|
|
|
|
$form->setParent($this->getBuilder()->getForm());
|
|
|
|
$form->add($this->getBuilder()->getForm());
|
|
|
|
|
2011-04-15 12:52:11 +01:00
|
|
|
$form->createView();
|
2011-04-15 12:41:45 +01:00
|
|
|
|
|
|
|
$this->assertEquals(array(
|
|
|
|
0 => 'type1::buildView',
|
2011-04-22 18:22:26 +01:00
|
|
|
1 => 'type1ext::buildView',
|
|
|
|
2 => 'type2::buildView',
|
|
|
|
3 => 'type2ext::buildView',
|
|
|
|
4 => 'type1::buildViewBottomUp',
|
|
|
|
5 => 'type1ext::buildViewBottomUp',
|
|
|
|
6 => 'type2::buildViewBottomUp',
|
|
|
|
7 => 'type2ext::buildViewBottomUp',
|
2011-04-15 12:41:45 +01:00
|
|
|
), $calls);
|
|
|
|
}
|
|
|
|
|
2011-04-15 12:52:11 +01:00
|
|
|
public function testCreateViewAcceptsParent()
|
2011-04-15 12:41:45 +01:00
|
|
|
{
|
|
|
|
$parent = new FormView();
|
|
|
|
|
|
|
|
$form = $this->getBuilder()->getForm();
|
2011-04-15 12:52:11 +01:00
|
|
|
$view = $form->createView($parent);
|
2011-04-15 12:41:45 +01:00
|
|
|
|
|
|
|
$this->assertSame($parent, $view->getParent());
|
|
|
|
}
|
|
|
|
|
2011-09-27 09:12:54 +01:00
|
|
|
public function testGetErrorsAsString()
|
|
|
|
{
|
|
|
|
$form = $this->getBuilder()->getForm();
|
|
|
|
$form->addError(new FormError('Error!'));
|
|
|
|
|
|
|
|
$this->assertEquals("ERROR: Error!\n", $form->getErrorsAsString());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetErrorsAsStringDeep()
|
|
|
|
{
|
|
|
|
$form = $this->getBuilder()->getForm();
|
|
|
|
$form->addError(new FormError('Error!'));
|
|
|
|
|
|
|
|
$parent = $this->getBuilder()->getForm();
|
|
|
|
$parent->add($form);
|
|
|
|
|
|
|
|
$parent->add($this->getBuilder('foo')->getForm());
|
|
|
|
|
|
|
|
$this->assertEquals("name:\n ERROR: Error!\nfoo:\n No errors\n", $parent->getErrorsAsString());
|
|
|
|
}
|
|
|
|
|
2012-01-07 14:14:50 +00:00
|
|
|
public function testFormCanHaveEmptyName()
|
|
|
|
{
|
|
|
|
$form = $this->getBuilder('')->getForm();
|
|
|
|
|
|
|
|
$this->assertEquals('', $form->getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Symfony\Component\Form\Exception\FormException
|
|
|
|
* @expectedExceptionMessage Form with empty name can not have parent form.
|
|
|
|
*/
|
|
|
|
public function testFormCannotHaveEmptyNameNotInRootLevel()
|
|
|
|
{
|
|
|
|
$parent = $this->getBuilder()
|
|
|
|
->add($this->getBuilder(''))
|
|
|
|
->getForm();
|
|
|
|
}
|
|
|
|
|
2011-12-28 20:47:11 +00:00
|
|
|
public function testGetValidatorsReturnsValidators()
|
|
|
|
{
|
|
|
|
$validator = $this->getFormValidator();
|
|
|
|
$form = $this->getBuilder()
|
|
|
|
->addValidator($validator)
|
|
|
|
->getForm();
|
|
|
|
|
|
|
|
$this->assertEquals(array($validator), $form->getValidators());
|
|
|
|
}
|
|
|
|
|
2011-04-02 10:22:29 +01:00
|
|
|
protected function getBuilder($name = 'name', EventDispatcherInterface $dispatcher = null)
|
2011-03-31 14:23:33 +01:00
|
|
|
{
|
2011-04-14 14:25:30 +01:00
|
|
|
return new FormBuilder($name, $this->factory, $dispatcher ?: $this->dispatcher);
|
2011-03-22 00:40:02 +00:00
|
|
|
}
|
|
|
|
|
2011-03-31 14:23:33 +01:00
|
|
|
protected function getMockForm($name = 'name')
|
2011-03-22 00:40:02 +00:00
|
|
|
{
|
|
|
|
$form = $this->getMock('Symfony\Tests\Component\Form\FormInterface');
|
|
|
|
|
|
|
|
$form->expects($this->any())
|
|
|
|
->method('getName')
|
|
|
|
->will($this->returnValue($name));
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getValidForm($name)
|
|
|
|
{
|
|
|
|
$form = $this->getMockForm($name);
|
|
|
|
|
|
|
|
$form->expects($this->any())
|
|
|
|
->method('isValid')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getInvalidForm($name)
|
|
|
|
{
|
|
|
|
$form = $this->getMockForm($name);
|
|
|
|
|
|
|
|
$form->expects($this->any())
|
|
|
|
->method('isValid')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
2011-04-02 10:22:29 +01:00
|
|
|
|
|
|
|
protected function getDataMapper()
|
|
|
|
{
|
2011-04-22 16:41:21 +01:00
|
|
|
return $this->getMock('Symfony\Component\Form\DataMapperInterface');
|
2011-04-02 10:22:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function getDataTransformer()
|
|
|
|
{
|
2011-04-22 16:41:21 +01:00
|
|
|
return $this->getMock('Symfony\Component\Form\DataTransformerInterface');
|
2011-04-02 10:22:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function getFormValidator()
|
|
|
|
{
|
2011-04-22 16:41:21 +01:00
|
|
|
return $this->getMock('Symfony\Component\Form\FormValidatorInterface');
|
2011-04-02 10:22:29 +01:00
|
|
|
}
|
2011-06-08 18:56:59 +01:00
|
|
|
}
|