2011-03-19 21:03:49 +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-21 19:46:43 +00:00
|
|
|
use Symfony\Component\Form\FormBuilder;
|
2011-04-22 16:41:21 +01:00
|
|
|
use Symfony\Component\Form\Guess\Guess;
|
2011-03-19 21:03:49 +00:00
|
|
|
|
|
|
|
class FormBuilderTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2011-03-27 13:34:48 +01:00
|
|
|
private $dispatcher;
|
|
|
|
|
2011-04-14 14:25:30 +01:00
|
|
|
private $factory;
|
|
|
|
|
2011-03-19 21:03:49 +00:00
|
|
|
private $builder;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
2011-03-27 13:34:48 +01:00
|
|
|
$this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
|
2011-04-14 14:25:30 +01:00
|
|
|
$this->factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
|
|
|
|
$this->builder = new FormBuilder('name', $this->factory, $this->dispatcher);
|
2011-03-31 12:48:06 +01:00
|
|
|
}
|
|
|
|
|
2011-06-14 09:44:38 +01:00
|
|
|
protected function tearDown()
|
|
|
|
{
|
|
|
|
$this->dispatcher = null;
|
|
|
|
$this->factory = null;
|
|
|
|
$this->builder = null;
|
|
|
|
}
|
|
|
|
|
2012-01-16 23:02:58 +00:00
|
|
|
public function getHtml4Ids()
|
|
|
|
{
|
|
|
|
// The full list is tested in FormTest, since both Form and FormBuilder
|
|
|
|
// use the same implementation internally
|
|
|
|
return array(
|
|
|
|
array('#', false),
|
|
|
|
array('a ', false),
|
|
|
|
array("a\t", false),
|
|
|
|
array("a\n", false),
|
|
|
|
array('a.', false),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getHtml4Ids
|
|
|
|
*/
|
|
|
|
public function testConstructAcceptsOnlyNamesValidAsIdsInHtml4($name, $accepted)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
new FormBuilder($name, $this->factory, $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 12:48:06 +01:00
|
|
|
/**
|
|
|
|
* Changing the name is not allowed, otherwise the name and property path
|
|
|
|
* are not synchronized anymore
|
|
|
|
*
|
|
|
|
* @see FieldType::buildForm
|
|
|
|
*/
|
|
|
|
public function testNoSetName()
|
|
|
|
{
|
|
|
|
$this->assertFalse(method_exists($this->builder, 'setName'));
|
2011-03-19 21:03:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddNameNoString()
|
|
|
|
{
|
|
|
|
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
|
|
|
|
$this->builder->add(1234);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddTypeNoString()
|
|
|
|
{
|
|
|
|
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
|
2011-03-31 12:48:06 +01:00
|
|
|
$this->builder->add('foo', 1234);
|
2011-03-19 21:03:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddWithGuessFluent()
|
|
|
|
{
|
2011-04-14 14:25:30 +01:00
|
|
|
$this->builder = new FormBuilder('name', $this->factory, $this->dispatcher, 'stdClass');
|
2011-03-19 21:03:49 +00:00
|
|
|
$builder = $this->builder->add('foo');
|
|
|
|
$this->assertSame($builder, $this->builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddIsFluent()
|
|
|
|
{
|
2011-03-31 12:48:06 +01:00
|
|
|
$builder = $this->builder->add('foo', 'text', array('bar' => 'baz'));
|
2011-03-19 21:03:49 +00:00
|
|
|
$this->assertSame($builder, $this->builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAdd()
|
|
|
|
{
|
|
|
|
$this->assertFalse($this->builder->has('foo'));
|
|
|
|
$this->builder->add('foo', 'text');
|
|
|
|
$this->assertTrue($this->builder->has('foo'));
|
|
|
|
}
|
|
|
|
|
2011-03-27 13:05:06 +01:00
|
|
|
public function testAddFormType()
|
|
|
|
{
|
|
|
|
$this->assertFalse($this->builder->has('foo'));
|
2011-04-22 16:41:21 +01:00
|
|
|
$this->builder->add('foo', $this->getMock('Symfony\Component\Form\FormTypeInterface'));
|
2011-03-27 13:05:06 +01:00
|
|
|
$this->assertTrue($this->builder->has('foo'));
|
|
|
|
}
|
|
|
|
|
2011-03-19 21:03:49 +00:00
|
|
|
public function testRemove()
|
|
|
|
{
|
|
|
|
$this->builder->add('foo', 'text');
|
|
|
|
$this->builder->remove('foo');
|
|
|
|
$this->assertFalse($this->builder->has('foo'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemoveUnknown()
|
|
|
|
{
|
|
|
|
$this->builder->remove('foo');
|
|
|
|
$this->assertFalse($this->builder->has('foo'));
|
|
|
|
}
|
|
|
|
|
2011-04-24 09:54:50 +01:00
|
|
|
public function testCreateNoTypeNoDataClass()
|
2011-03-19 21:03:49 +00:00
|
|
|
{
|
2011-06-09 10:47:14 +01:00
|
|
|
$this->factory->expects($this->once())
|
|
|
|
->method('createNamedBuilder')
|
|
|
|
->with('text', 'foo', null, array())
|
|
|
|
;
|
|
|
|
|
|
|
|
$builder = $this->builder->create('foo');
|
2011-03-19 21:03:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetUnknown()
|
|
|
|
{
|
|
|
|
$this->setExpectedException('Symfony\Component\Form\Exception\FormException', 'The field "foo" does not exist');
|
|
|
|
$this->builder->get('foo');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetTyped()
|
|
|
|
{
|
|
|
|
$expectedType = 'text';
|
|
|
|
$expectedName = 'foo';
|
|
|
|
$expectedOptions = array('bar' => 'baz');
|
|
|
|
|
2011-04-14 14:25:30 +01:00
|
|
|
$this->factory->expects($this->once())
|
2011-04-22 09:42:21 +01:00
|
|
|
->method('createNamedBuilder')
|
|
|
|
->with($expectedType, $expectedName, null, $expectedOptions)
|
2011-03-21 19:46:43 +00:00
|
|
|
->will($this->returnValue($this->getFormBuilder()));
|
2011-03-19 21:03:49 +00:00
|
|
|
|
|
|
|
$this->builder->add($expectedName, $expectedType, $expectedOptions);
|
|
|
|
$builder = $this->builder->get($expectedName);
|
|
|
|
|
|
|
|
$this->assertNotSame($builder, $this->builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetGuessed()
|
|
|
|
{
|
|
|
|
$expectedName = 'foo';
|
|
|
|
$expectedOptions = array('bar' => 'baz');
|
|
|
|
|
2011-04-14 14:25:30 +01:00
|
|
|
$this->factory->expects($this->once())
|
2011-03-19 21:03:49 +00:00
|
|
|
->method('createBuilderForProperty')
|
2011-04-22 09:42:21 +01:00
|
|
|
->with('stdClass', $expectedName, null, $expectedOptions)
|
2011-03-21 19:46:43 +00:00
|
|
|
->will($this->returnValue($this->getFormBuilder()));
|
2011-03-19 21:03:49 +00:00
|
|
|
|
2011-04-14 14:25:30 +01:00
|
|
|
$this->builder = new FormBuilder('name', $this->factory, $this->dispatcher, 'stdClass');
|
2011-03-19 21:03:49 +00:00
|
|
|
$this->builder->add($expectedName, null, $expectedOptions);
|
|
|
|
$builder = $this->builder->get($expectedName);
|
|
|
|
|
|
|
|
$this->assertNotSame($builder, $this->builder);
|
|
|
|
}
|
2011-03-21 19:46:43 +00:00
|
|
|
|
2011-12-14 14:01:09 +00:00
|
|
|
public function testGetParent()
|
|
|
|
{
|
|
|
|
$this->assertNull($this->builder->getParent());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetParentForAddedBuilder()
|
|
|
|
{
|
|
|
|
$builder = new FormBuilder('name', $this->factory, $this->dispatcher);
|
|
|
|
$this->builder->add($builder);
|
|
|
|
$this->assertSame($this->builder, $builder->getParent());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetParentForRemovedBuilder()
|
|
|
|
{
|
|
|
|
$builder = new FormBuilder('name', $this->factory, $this->dispatcher);
|
|
|
|
$this->builder->add($builder);
|
|
|
|
$this->builder->remove('name');
|
|
|
|
$this->assertNull($builder->getParent());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetParentForCreatedBuilder()
|
|
|
|
{
|
|
|
|
$this->builder = new FormBuilder('name', $this->factory, $this->dispatcher, 'stdClass');
|
|
|
|
$this->factory
|
|
|
|
->expects($this->once())
|
|
|
|
->method('createNamedBuilder')
|
|
|
|
->with('text', 'bar', null, array(), $this->builder)
|
|
|
|
;
|
|
|
|
|
|
|
|
$this->factory
|
|
|
|
->expects($this->once())
|
|
|
|
->method('createBuilderForProperty')
|
|
|
|
->with('stdClass', 'foo', null, array(), $this->builder)
|
|
|
|
;
|
|
|
|
|
|
|
|
$this->builder->create('foo');
|
|
|
|
$this->builder->create('bar', 'text');
|
|
|
|
}
|
|
|
|
|
2011-03-21 19:46:43 +00:00
|
|
|
private function getFormBuilder()
|
|
|
|
{
|
|
|
|
return $this->getMockBuilder('Symfony\Component\Form\FormBuilder')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
}
|
2011-06-08 18:56:59 +01:00
|
|
|
}
|