Add tests

This commit is contained in:
Dariusz Górecki 2012-01-07 15:14:50 +01:00
parent 4f38b14e7d
commit 3702541b5a
5 changed files with 112 additions and 1 deletions

View File

@ -86,7 +86,9 @@ class FieldType extends AbstractType
$fullName = $name;
}
} else {
$id = $name;
// If this form node have empty name, set id to `form`
// to avoid rendering `id=""` in html structure
$id = $name ?: 'form';
$fullName = $name;
}

View File

@ -303,6 +303,10 @@ class Form implements \IteratorAggregate, FormInterface
*/
public function setParent(FormInterface $parent = null)
{
if ('' === $this->getName()) {
throw new FormException('Form with empty name can not have parent form.');
}
$this->parent = $parent;
return $this;

View File

@ -1790,4 +1790,19 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
'
);
}
public function testEmptyRootFormName()
{
$form = $this->factory->createNamedBuilder('form', '', '', array(
'property_path' => 'name',
))
->add('child', 'text')
->getForm();
$this->assertMatchesXpath($this->renderWidget($form->createView()),
'//input[@type="hidden"][@id="_token"][@name="_token"]
|
//input[@type="text"][@id="child"][@name="child"]'
, 2);
}
}

View File

@ -254,4 +254,10 @@ class FieldTypeTest extends TypeTestCase
$form = $this->factory->create('field', null, array('attr' => ''));
}
public function testNameCanBeEmptyString()
{
$form = $this->factory->createNamed('field', '');
$this->assertEquals('', $form->getName());
}
}

View File

@ -868,6 +868,49 @@ class FormTest extends \PHPUnit_Framework_TestCase
unlink($path);
}
/**
* @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);
}
public function testBindGetRequest()
{
$values = array(
@ -891,6 +934,29 @@ class FormTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('Schussek', $form['lastName']->getData());
}
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());
}
public function testBindResetsErrors()
{
$form = $this->getBuilder()->getForm();
@ -1024,6 +1090,24 @@ class FormTest extends \PHPUnit_Framework_TestCase
$this->assertEquals("name:\n ERROR: Error!\nfoo:\n No errors\n", $parent->getErrorsAsString());
}
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();
}
protected function getBuilder($name = 'name', EventDispatcherInterface $dispatcher = null)
{
return new FormBuilder($name, $this->factory, $dispatcher ?: $this->dispatcher);