Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix BC for the default root form name #16758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/Symfony/Component/Form/FormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,29 @@ public function createBuilder($type = 'Symfony\Component\Form\Extension\Core\Typ
$typeName = $type->getName();
}
} elseif ($type instanceof FormTypeInterface) {
// BC
$typeName = $type->getName();
if (method_exists($type, 'getBlockPrefix')) {
// As of Symfony 3.0, the block prefix of the type is used as
// default name
$name = $type->getBlockPrefix();
} else {
// BC
$typeName = $type->getName();
}
} elseif (is_string($type)) {
// BC
$typeName = $type;
$resolvedType = $this->registry->getType($type);
if (method_exists($resolvedType, 'getBlockPrefix')) {
// As of Symfony 3.0, the block prefix of the type is used as
// default name
$name = $resolvedType->getBlockPrefix();
} else {
// BC
$typeName = $type;
}
} else {
throw new UnexpectedTypeException($type, 'string, Symfony\Component\Form\ResolvedFormTypeInterface or Symfony\Component\Form\FormTypeInterface');
}

// BC when there is no block prefix
if (null === $name) {
if (false === strpos($typeName, '\\')) {
// No FQCN - leave unchanged for BC
Expand Down
51 changes: 45 additions & 6 deletions src/Symfony/Component/Form/Tests/FormFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,52 @@ public function testCreateThrowsUnderstandableException()
$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));
Expand Down Expand Up @@ -339,7 +378,7 @@ public function testCreateStripsNamespaceOffTypeName()
$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));
Expand Down Expand Up @@ -370,7 +409,7 @@ public function testLegacyCreateStripsNamespaceOffTypeNameAccessByFQCN()
$resolvedOptions = array('a' => '2', 'b' => '3');
$resolvedType = $this->getMockResolvedType();

$this->registry->expects($this->once())
$this->registry->expects($this->any())
->method('getType')
->with('userform')
->will($this->returnValue($resolvedType));
Expand Down Expand Up @@ -401,7 +440,7 @@ public function testCreateStripsTypeSuffixOffTypeName()
$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));
Expand Down Expand Up @@ -432,7 +471,7 @@ public function testCreateDoesNotStripTypeSuffixIfResultEmpty()
$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));
Expand Down Expand Up @@ -463,7 +502,7 @@ public function testCreateConvertsTypeToUnderscoreSyntax()
$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));
Expand Down