From 69c9fef3a0bae4efa891a28f823f92658b3373fc Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Fri, 23 Mar 2012 11:21:47 -0400 Subject: [PATCH] [Form] Failing test for empty_data option BC break This demonstrates the issue described in symfony/symfony#3354. FieldType no longer has access to the child type's data_class option, which makes it unable to create the default closure for empty_data. --- .../Component/Form/Fixtures/AuthorType.php | 31 +++++++++++++++++++ .../Tests/Component/Form/FormFactoryTest.php | 16 ++++++++++ 2 files changed, 47 insertions(+) create mode 100644 tests/Symfony/Tests/Component/Form/Fixtures/AuthorType.php diff --git a/tests/Symfony/Tests/Component/Form/Fixtures/AuthorType.php b/tests/Symfony/Tests/Component/Form/Fixtures/AuthorType.php new file mode 100644 index 0000000000000..a3c9367d6d1ea --- /dev/null +++ b/tests/Symfony/Tests/Component/Form/Fixtures/AuthorType.php @@ -0,0 +1,31 @@ +add('firstName') + ->add('lastName') + ; + } + + public function getName() + { + return 'author'; + } + + public function getDefaultOptions(array $options) + { + return array( + 'data_class' => 'Symfony\Tests\Component\Form\Fixtures\Author', + ); + } +} diff --git a/tests/Symfony/Tests/Component/Form/FormFactoryTest.php b/tests/Symfony/Tests/Component/Form/FormFactoryTest.php index 932cf0b38f841..5fe5decc13c52 100644 --- a/tests/Symfony/Tests/Component/Form/FormFactoryTest.php +++ b/tests/Symfony/Tests/Component/Form/FormFactoryTest.php @@ -16,6 +16,8 @@ use Symfony\Component\Form\Guess\Guess; use Symfony\Component\Form\Guess\ValueGuess; use Symfony\Component\Form\Guess\TypeGuess; +use Symfony\Tests\Component\Form\Fixtures\Author; +use Symfony\Tests\Component\Form\Fixtures\AuthorType; use Symfony\Tests\Component\Form\Fixtures\TestExtension; use Symfony\Tests\Component\Form\Fixtures\FooType; use Symfony\Tests\Component\Form\Fixtures\FooTypeBarExtension; @@ -531,6 +533,20 @@ public function testUnknownOption() $factory->createNamedBuilder($type, "text", "value", array("unknown" => "opt")); } + public function testFieldTypeCreatesDefaultValueForEmptyDataOption() + { + $factory = new FormFactory(array(new \Symfony\Component\Form\Extension\Core\CoreExtension())); + + $form = $factory->createNamedBuilder(new AuthorType(), 'author')->getForm(); + $form->bind(array('firstName' => 'John', 'lastName' => 'Smith')); + + $author = new Author(); + $author->firstName = 'John'; + $author->setLastName('Smith'); + + $this->assertEquals($author, $form->getData()); + } + private function createMockFactory(array $methods = array()) { return $this->getMockBuilder('Symfony\Component\Form\FormFactory')