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.
|
|
|
|
*/
|
|
|
|
|
2012-03-28 14:43:52 +01:00
|
|
|
namespace Symfony\Component\Form\Tests;
|
2011-03-19 21:03:49 +00:00
|
|
|
|
2017-02-08 07:24:27 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2015-05-21 22:12:55 +01:00
|
|
|
use Symfony\Component\Form\ButtonBuilder;
|
2011-03-21 19:46:43 +00:00
|
|
|
use Symfony\Component\Form\FormBuilder;
|
2015-05-21 22:12:55 +01:00
|
|
|
use Symfony\Component\Form\SubmitButtonBuilder;
|
2011-03-19 21:03:49 +00:00
|
|
|
|
2017-02-08 07:24:27 +00:00
|
|
|
class FormBuilderTest extends TestCase
|
2011-03-19 21:03:49 +00:00
|
|
|
{
|
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;
|
|
|
|
|
2012-03-28 14:43:52 +01:00
|
|
|
protected function setUp()
|
2011-03-19 21:03:49 +00:00
|
|
|
{
|
2016-12-19 09:02:29 +00:00
|
|
|
$this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
|
|
|
|
$this->factory = $this->getMockBuilder('Symfony\Component\Form\FormFactoryInterface')->getMock();
|
2012-05-17 15:09:13 +01:00
|
|
|
$this->builder = new FormBuilder('name', null, $this->dispatcher, $this->factory);
|
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;
|
|
|
|
}
|
|
|
|
|
2011-03-31 12:48:06 +01:00
|
|
|
/**
|
|
|
|
* Changing the name is not allowed, otherwise the name and property path
|
2014-12-21 17:00:50 +00:00
|
|
|
* are not synchronized anymore.
|
2011-03-31 12:48:06 +01:00
|
|
|
*
|
2014-12-21 16:36:15 +00:00
|
|
|
* @see FormType::buildForm()
|
2011-03-31 12:48:06 +01:00
|
|
|
*/
|
|
|
|
public function testNoSetName()
|
|
|
|
{
|
|
|
|
$this->assertFalse(method_exists($this->builder, 'setName'));
|
2011-03-19 21:03:49 +00:00
|
|
|
}
|
|
|
|
|
2012-12-14 18:27:28 +00:00
|
|
|
public function testAddNameNoStringAndNoInteger()
|
2011-03-19 21:03:49 +00:00
|
|
|
{
|
2017-02-20 14:23:32 +00:00
|
|
|
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Form\Exception\UnexpectedTypeException');
|
2012-12-14 18:27:28 +00:00
|
|
|
$this->builder->add(true);
|
2011-03-19 21:03:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddTypeNoString()
|
|
|
|
{
|
2017-02-20 14:23:32 +00:00
|
|
|
$this->{method_exists($this, $_ = 'expectException') ? $_ : '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()
|
|
|
|
{
|
2012-05-17 15:09:13 +01:00
|
|
|
$this->builder = new FormBuilder('name', 'stdClass', $this->dispatcher, $this->factory);
|
2011-03-19 21:03:49 +00:00
|
|
|
$builder = $this->builder->add('foo');
|
|
|
|
$this->assertSame($builder, $this->builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddIsFluent()
|
|
|
|
{
|
2015-06-23 17:42:21 +01:00
|
|
|
$builder = $this->builder->add('foo', 'Symfony\Component\Form\Extension\Core\Type\TextType', 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'));
|
2015-06-23 17:42:21 +01:00
|
|
|
$this->builder->add('foo', 'Symfony\Component\Form\Extension\Core\Type\TextType');
|
2011-03-19 21:03:49 +00:00
|
|
|
$this->assertTrue($this->builder->has('foo'));
|
|
|
|
}
|
|
|
|
|
2012-12-14 18:27:28 +00:00
|
|
|
public function testAddIntegerName()
|
|
|
|
{
|
|
|
|
$this->assertFalse($this->builder->has(0));
|
2015-06-23 17:42:21 +01:00
|
|
|
$this->builder->add(0, 'Symfony\Component\Form\Extension\Core\Type\TextType');
|
2012-12-14 18:27:28 +00:00
|
|
|
$this->assertTrue($this->builder->has(0));
|
|
|
|
}
|
|
|
|
|
2012-04-11 18:07:08 +01:00
|
|
|
public function testAll()
|
|
|
|
{
|
2012-05-16 18:25:27 +01:00
|
|
|
$this->factory->expects($this->once())
|
|
|
|
->method('createNamedBuilder')
|
2015-06-23 17:42:21 +01:00
|
|
|
->with('foo', 'Symfony\Component\Form\Extension\Core\Type\TextType')
|
2012-05-17 15:09:13 +01:00
|
|
|
->will($this->returnValue(new FormBuilder('foo', null, $this->dispatcher, $this->factory)));
|
2012-05-16 18:25:27 +01:00
|
|
|
|
2012-04-11 19:45:41 +01:00
|
|
|
$this->assertCount(0, $this->builder->all());
|
2012-04-11 18:07:08 +01:00
|
|
|
$this->assertFalse($this->builder->has('foo'));
|
|
|
|
|
2015-06-23 17:42:21 +01:00
|
|
|
$this->builder->add('foo', 'Symfony\Component\Form\Extension\Core\Type\TextType');
|
2012-04-11 18:07:08 +01:00
|
|
|
$children = $this->builder->all();
|
|
|
|
|
|
|
|
$this->assertTrue($this->builder->has('foo'));
|
2012-04-11 19:45:41 +01:00
|
|
|
$this->assertCount(1, $children);
|
2012-04-11 18:07:08 +01:00
|
|
|
$this->assertArrayHasKey('foo', $children);
|
|
|
|
}
|
|
|
|
|
2012-07-06 16:32:57 +01:00
|
|
|
/*
|
|
|
|
* https://github.com/symfony/symfony/issues/4693
|
|
|
|
*/
|
|
|
|
public function testMaintainOrderOfLazyAndExplicitChildren()
|
|
|
|
{
|
2015-06-23 17:42:21 +01:00
|
|
|
$this->builder->add('foo', 'Symfony\Component\Form\Extension\Core\Type\TextType');
|
2012-07-06 16:32:57 +01:00
|
|
|
$this->builder->add($this->getFormBuilder('bar'));
|
2015-06-23 17:42:21 +01:00
|
|
|
$this->builder->add('baz', 'Symfony\Component\Form\Extension\Core\Type\TextType');
|
2012-07-06 16:32:57 +01:00
|
|
|
|
|
|
|
$children = $this->builder->all();
|
|
|
|
|
|
|
|
$this->assertSame(array('foo', 'bar', 'baz'), array_keys($children));
|
|
|
|
}
|
|
|
|
|
2011-03-27 13:05:06 +01:00
|
|
|
public function testAddFormType()
|
|
|
|
{
|
|
|
|
$this->assertFalse($this->builder->has('foo'));
|
2016-12-19 09:02:29 +00:00
|
|
|
$this->builder->add('foo', $this->getMockBuilder('Symfony\Component\Form\FormTypeInterface')->getMock());
|
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()
|
|
|
|
{
|
2015-06-23 17:42:21 +01:00
|
|
|
$this->builder->add('foo', 'Symfony\Component\Form\Extension\Core\Type\TextType');
|
2011-03-19 21:03:49 +00:00
|
|
|
$this->builder->remove('foo');
|
|
|
|
$this->assertFalse($this->builder->has('foo'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemoveUnknown()
|
|
|
|
{
|
|
|
|
$this->builder->remove('foo');
|
|
|
|
$this->assertFalse($this->builder->has('foo'));
|
|
|
|
}
|
|
|
|
|
2012-07-10 07:48:09 +01:00
|
|
|
// https://github.com/symfony/symfony/pull/4826
|
|
|
|
public function testRemoveAndGetForm()
|
|
|
|
{
|
2015-06-23 17:42:21 +01:00
|
|
|
$this->builder->add('foo', 'Symfony\Component\Form\Extension\Core\Type\TextType');
|
2012-07-10 07:48:09 +01:00
|
|
|
$this->builder->remove('foo');
|
|
|
|
$form = $this->builder->getForm();
|
|
|
|
$this->assertInstanceOf('Symfony\Component\Form\Form', $form);
|
|
|
|
}
|
|
|
|
|
2012-07-06 16:32:57 +01:00
|
|
|
public function testCreateNoTypeNo()
|
2011-03-19 21:03:49 +00:00
|
|
|
{
|
2011-06-09 10:47:14 +01:00
|
|
|
$this->factory->expects($this->once())
|
2012-07-06 16:32:57 +01:00
|
|
|
->method('createNamedBuilder')
|
2015-06-23 17:42:21 +01:00
|
|
|
->with('foo', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, array())
|
2011-06-09 10:47:14 +01:00
|
|
|
;
|
|
|
|
|
2012-07-06 16:32:57 +01:00
|
|
|
$this->builder->create('foo');
|
2011-03-19 21:03:49 +00:00
|
|
|
}
|
|
|
|
|
2015-05-21 22:12:55 +01:00
|
|
|
public function testAddButton()
|
|
|
|
{
|
|
|
|
$this->builder->add(new ButtonBuilder('reset'));
|
|
|
|
$this->builder->add(new SubmitButtonBuilder('submit'));
|
|
|
|
}
|
|
|
|
|
2011-03-19 21:03:49 +00:00
|
|
|
public function testGetUnknown()
|
|
|
|
{
|
2017-02-20 14:23:32 +00:00
|
|
|
if (method_exists($this, 'expectException')) {
|
|
|
|
$this->expectException('Symfony\Component\Form\Exception\InvalidArgumentException');
|
|
|
|
$this->expectExceptionMessage('The child with the name "foo" does not exist.');
|
|
|
|
} else {
|
|
|
|
$this->setExpectedException('Symfony\Component\Form\Exception\InvalidArgumentException', 'The child with the name "foo" does not exist.');
|
|
|
|
}
|
|
|
|
|
2011-03-19 21:03:49 +00:00
|
|
|
$this->builder->get('foo');
|
|
|
|
}
|
|
|
|
|
2012-07-06 16:32:57 +01:00
|
|
|
public function testGetExplicitType()
|
2011-03-19 21:03:49 +00:00
|
|
|
{
|
2015-06-23 17:42:21 +01:00
|
|
|
$expectedType = 'Symfony\Component\Form\Extension\Core\Type\TextType';
|
2011-03-19 21:03:49 +00:00
|
|
|
$expectedName = 'foo';
|
|
|
|
$expectedOptions = array('bar' => 'baz');
|
|
|
|
|
2011-04-14 14:25:30 +01:00
|
|
|
$this->factory->expects($this->once())
|
2012-07-06 16:32:57 +01:00
|
|
|
->method('createNamedBuilder')
|
|
|
|
->with($expectedName, $expectedType, null, $expectedOptions)
|
|
|
|
->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);
|
|
|
|
}
|
|
|
|
|
2012-07-06 16:32:57 +01:00
|
|
|
public function testGetGuessedType()
|
2011-03-19 21:03:49 +00:00
|
|
|
{
|
|
|
|
$expectedName = 'foo';
|
|
|
|
$expectedOptions = array('bar' => 'baz');
|
|
|
|
|
2011-04-14 14:25:30 +01:00
|
|
|
$this->factory->expects($this->once())
|
2012-07-06 16:32:57 +01:00
|
|
|
->method('createBuilderForProperty')
|
|
|
|
->with('stdClass', $expectedName, null, $expectedOptions)
|
|
|
|
->will($this->returnValue($this->getFormBuilder()));
|
2011-03-19 21:03:49 +00:00
|
|
|
|
2012-05-17 15:09:13 +01:00
|
|
|
$this->builder = new FormBuilder('name', 'stdClass', $this->dispatcher, $this->factory);
|
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
|
|
|
|
2013-01-05 17:04:01 +00:00
|
|
|
public function testGetFormConfigErasesReferences()
|
|
|
|
{
|
|
|
|
$builder = new FormBuilder('name', null, $this->dispatcher, $this->factory);
|
|
|
|
$builder->add(new FormBuilder('child', null, $this->dispatcher, $this->factory));
|
|
|
|
|
|
|
|
$config = $builder->getFormConfig();
|
|
|
|
$reflClass = new \ReflectionClass($config);
|
|
|
|
$children = $reflClass->getProperty('children');
|
|
|
|
$unresolvedChildren = $reflClass->getProperty('unresolvedChildren');
|
|
|
|
|
|
|
|
$children->setAccessible(true);
|
|
|
|
$unresolvedChildren->setAccessible(true);
|
|
|
|
|
|
|
|
$this->assertEmpty($children->getValue($config));
|
|
|
|
$this->assertEmpty($unresolvedChildren->getValue($config));
|
|
|
|
}
|
|
|
|
|
2012-07-06 16:32:57 +01:00
|
|
|
private function getFormBuilder($name = 'name')
|
2011-03-21 19:46:43 +00:00
|
|
|
{
|
2012-07-06 16:32:57 +01:00
|
|
|
$mock = $this->getMockBuilder('Symfony\Component\Form\FormBuilder')
|
2011-03-21 19:46:43 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2012-07-06 16:32:57 +01:00
|
|
|
|
|
|
|
$mock->expects($this->any())
|
|
|
|
->method('getName')
|
|
|
|
->will($this->returnValue($name));
|
|
|
|
|
|
|
|
return $mock;
|
2011-03-21 19:46:43 +00:00
|
|
|
}
|
2011-06-08 18:56:59 +01:00
|
|
|
}
|