diff --git a/src/Symfony/Component/Form/FormFactory.php b/src/Symfony/Component/Form/FormFactory.php index d8cab0478d..8562c2d1db 100644 --- a/src/Symfony/Component/Form/FormFactory.php +++ b/src/Symfony/Component/Form/FormFactory.php @@ -65,7 +65,11 @@ class FormFactory implements FormFactoryInterface throw new UnexpectedTypeException($type, 'string'); } - return $this->createNamedBuilder(StringUtil::fqcnToBlockPrefix($type), $type, $data, $options); + if (null === $name = $this->registry->getType($type)->getBlockPrefix()) { + $name = StringUtil::fqcnToBlockPrefix($type); + } + + return $this->createNamedBuilder($name, $type, $data, $options); } /** diff --git a/src/Symfony/Component/Form/Tests/FormFactoryTest.php b/src/Symfony/Component/Form/Tests/FormFactoryTest.php index 3efb18eabb..c4f1d1837d 100644 --- a/src/Symfony/Component/Form/Tests/FormFactoryTest.php +++ b/src/Symfony/Component/Form/Tests/FormFactoryTest.php @@ -169,13 +169,52 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase $this->factory->create(new \stdClass()); } + public function testCreateUsesBlockPrefixIfTypeGivenAsString() + { + $options = array('a' => '1', 'b' => '2'); + $resolvedOptions = array('a' => '2', 'b' => '3'); + + // the interface does not have the method, so use the real class + $resolvedType = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormType') + ->disableOriginalConstructor() + ->getMock(); + + $resolvedType->expects($this->any()) + ->method('getBlockPrefix') + ->willReturn('TYPE_PREFIX'); + + $this->registry->expects($this->any()) + ->method('getType') + ->with('TYPE') + ->will($this->returnValue($resolvedType)); + + $resolvedType->expects($this->once()) + ->method('createBuilder') + ->with($this->factory, 'TYPE_PREFIX', $options) + ->will($this->returnValue($this->builder)); + + $this->builder->expects($this->any()) + ->method('getOptions') + ->will($this->returnValue($resolvedOptions)); + + $resolvedType->expects($this->once()) + ->method('buildForm') + ->with($this->builder, $resolvedOptions); + + $this->builder->expects($this->once()) + ->method('getForm') + ->will($this->returnValue('FORM')); + + $this->assertSame('FORM', $this->factory->create('TYPE', null, $options)); + } + public function testCreateUsesTypeNameIfTypeGivenAsString() { $options = array('a' => '1', 'b' => '2'); $resolvedOptions = array('a' => '2', 'b' => '3'); $resolvedType = $this->getMockResolvedType(); - $this->registry->expects($this->once()) + $this->registry->expects($this->any()) ->method('getType') ->with('TYPE') ->will($this->returnValue($resolvedType)); @@ -206,7 +245,7 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase $resolvedOptions = array('a' => '2', 'b' => '3'); $resolvedType = $this->getMockResolvedType(); - $this->registry->expects($this->once()) + $this->registry->expects($this->any()) ->method('getType') ->with('Vendor\Name\Space\UserForm') ->will($this->returnValue($resolvedType)); @@ -237,7 +276,7 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase $resolvedOptions = array('a' => '2', 'b' => '3'); $resolvedType = $this->getMockResolvedType(); - $this->registry->expects($this->once()) + $this->registry->expects($this->any()) ->method('getType') ->with('Vendor\Name\Space\UserType') ->will($this->returnValue($resolvedType)); @@ -268,7 +307,7 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase $resolvedOptions = array('a' => '2', 'b' => '3'); $resolvedType = $this->getMockResolvedType(); - $this->registry->expects($this->once()) + $this->registry->expects($this->any()) ->method('getType') ->with('Vendor\Name\Space\Type') ->will($this->returnValue($resolvedType)); @@ -299,7 +338,7 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase $resolvedOptions = array('a' => '2', 'b' => '3'); $resolvedType = $this->getMockResolvedType(); - $this->registry->expects($this->once()) + $this->registry->expects($this->any()) ->method('getType') ->with('Vendor\Name\Space\MyProfileHTMLType') ->will($this->returnValue($resolvedType));