diff --git a/tests/Symfony/Tests/Component/Form/Fixtures/BarType.php b/tests/Symfony/Tests/Component/Form/Fixtures/BarType.php new file mode 100644 index 0000000000000..a9ab5df84d680 --- /dev/null +++ b/tests/Symfony/Tests/Component/Form/Fixtures/BarType.php @@ -0,0 +1,52 @@ +setAttribute('bar', 'x'); + $builder->setAttribute('data_option', $options['data']); + } + + public function getName() + { + return 'bar'; + } + + public function createBuilder($name, FormFactoryInterface $factory, array $options) + { + return new FormBuilder($name, $factory, new EventDispatcher()); + } + + public function getDefaultOptions(array $options) + { + return array( + 'data' => null, + 'data_class' => 'Symfony\Tests\Component\Form\Fixtures\Author', + 'empty_data' => true, + 'required' => false, + 'max_length' => null, + 'a_or_b' => 'a', + ); + } + + public function getAllowedOptionValues(array $options) + { + return array( + 'a_or_b' => array('a', 'b'), + ); + } + + public function getParent(array $options) + { + return null; + } +} diff --git a/tests/Symfony/Tests/Component/Form/FormFactoryTest.php b/tests/Symfony/Tests/Component/Form/FormFactoryTest.php index 932cf0b38f841..35bdb1e60e39f 100644 --- a/tests/Symfony/Tests/Component/Form/FormFactoryTest.php +++ b/tests/Symfony/Tests/Component/Form/FormFactoryTest.php @@ -18,6 +18,8 @@ use Symfony\Component\Form\Guess\TypeGuess; use Symfony\Tests\Component\Form\Fixtures\TestExtension; use Symfony\Tests\Component\Form\Fixtures\FooType; +use Symfony\Tests\Component\Form\Fixtures\BarType; +use Symfony\Tests\Component\Form\Fixtures\Author; use Symfony\Tests\Component\Form\Fixtures\FooTypeBarExtension; use Symfony\Tests\Component\Form\Fixtures\FooTypeBazExtension; @@ -516,6 +518,19 @@ public function testUnknownOptions() $factory->createNamedBuilder($type, "text", "value", array("invalid" => "opt", "unknown" => "opt")); } + public function testNullObjectWithCustomTypeReturnsCorrectObjectType() + { + $type = new BarType(); + $form = $this->factory->create($type); + $form->setData(new Author()); + $fdata = $form->getData(); + $this->assertTrue($fdata instanceof Author); + + $form = $this->factory->create($type); + $fdata = $form->getData(); + $this->assertTrue($fdata instanceof Author, 'Failing in returning object of type specified in data_class when creating with empty data'); + } + public function testUnknownOption() { $type = new \Symfony\Component\Form\Extension\Core\Type\TextType();