[Form] allowed no type guesser to be registered

This commit is contained in:
Bilal Amarni 2012-11-08 19:49:46 +01:00
parent 602a45880f
commit 84635bda7f
8 changed files with 101 additions and 13 deletions

View File

@ -95,7 +95,10 @@ class FormFactory implements FormFactoryInterface
*/
public function createBuilderForProperty($class, $property, $data = null, array $options = array(), FormBuilderInterface $parent = null)
{
$guesser = $this->registry->getTypeGuesser();
if (null === $guesser = $this->registry->getTypeGuesser()) {
return $this->createNamedBuilder($property, 'text', $data, $options, $parent);
}
$typeGuess = $guesser->guessType($class, $property);
$maxLengthGuess = $guesser->guessMaxLength($class, $property);
// Keep $minLengthGuess for BC until Symfony 2.3

View File

@ -145,9 +145,11 @@ class FormFactoryBuilder implements FormFactoryBuilderInterface
$extensions = $this->extensions;
if (count($this->types) > 0 || count($this->typeExtensions) > 0 || count($this->typeGuessers) > 0) {
$typeGuesser = count($this->typeGuessers) > 1
? new FormTypeGuesserChain($this->typeGuessers)
: $this->typeGuessers[0];
if (count($this->typeGuessers) > 1) {
$typeGuesser = new FormTypeGuesserChain($this->typeGuessers);
} else {
$typeGuesser = isset($this->typeGuessers[0]) ? $this->typeGuessers[0] : null;
}
$extensions[] = new PreloadedExtension($this->types, $this->typeExtensions, $typeGuesser);
}

View File

@ -33,9 +33,9 @@ class FormRegistry implements FormRegistryInterface
private $types = array();
/**
* @var FormTypeGuesserInterface
* @var FormTypeGuesserInterface|false|null
*/
private $guesser;
private $guesser = false;
/**
* @var ResolvedFormTypeFactoryInterface
@ -158,7 +158,7 @@ class FormRegistry implements FormRegistryInterface
*/
public function getTypeGuesser()
{
if (null === $this->guesser) {
if (false === $this->guesser) {
$guessers = array();
foreach ($this->extensions as $extension) {
@ -170,7 +170,7 @@ class FormRegistry implements FormRegistryInterface
}
}
$this->guesser = new FormTypeGuesserChain($guessers);
$this->guesser = !empty($guessers) ? new FormTypeGuesserChain($guessers) : null;
}
return $this->guesser;

View File

@ -55,7 +55,7 @@ interface FormRegistryInterface
/**
* Returns the guesser responsible for guessing types.
*
* @return FormTypeGuesserInterface
* @return FormTypeGuesserInterface|null
*/
public function getTypeGuesser();

View File

@ -38,11 +38,11 @@ class PreloadedExtension implements FormExtensionInterface
/**
* Creates a new preloaded extension.
*
* @param array $types The types that the extension should support.
* @param array $typeExtensions The type extensions that the extension should support.
* @param FormTypeGuesserInterface $typeGuesser The guesser that the extension should support.
* @param array $types The types that the extension should support.
* @param array $typeExtensions The type extensions that the extension should support.
* @param FormTypeGuesserInterface|null $typeGuesser The guesser that the extension should support.
*/
public function __construct(array $types, array $typeExtensions, FormTypeGuesserInterface $typeGuesser)
public function __construct(array $types, array $typeExtensions, FormTypeGuesserInterface $typeGuesser = null)
{
$this->types = $types;
$this->typeExtensions = $typeExtensions;

View File

@ -0,0 +1,59 @@
<?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.
*/
namespace Symfony\Component\Form\Tests;
use Symfony\Component\Form\FormFactoryBuilder;
use Symfony\Component\Form\Tests\Fixtures\FooType;
class FormFactoryBuilderTest extends \PHPUnit_Framework_TestCase
{
private $registry;
private $guesser;
private $type;
protected function setUp()
{
$factory = new \ReflectionClass('Symfony\Component\Form\FormFactory');
$this->registry = $factory->getProperty('registry');
$this->registry->setAccessible(true);
$this->guesser = $this->getMock('Symfony\Component\Form\FormTypeGuesserInterface');
$this->type = new FooType;
}
public function testAddType()
{
$factoryBuilder = new FormFactoryBuilder;
$factoryBuilder->addType($this->type);
$factory = $factoryBuilder->getFormFactory();
$registry = $this->registry->getValue($factory);
$extensions = $registry->getExtensions();
$this->assertCount(1, $extensions);
$this->assertTrue($extensions[0]->hasType($this->type->getName()));
$this->assertNull($extensions[0]->getTypeGuesser());
}
public function testAddTypeGuesser()
{
$factoryBuilder = new FormFactoryBuilder;
$factoryBuilder->addTypeGuesser($this->guesser);
$factory = $factoryBuilder->getFormFactory();
$registry = $this->registry->getValue($factory);
$extensions = $registry->getExtensions();
$this->assertCount(1, $extensions);
$this->assertNotNull($extensions[0]->getTypeGuesser());
}
}

View File

@ -353,6 +353,24 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
$this->assertSame('FORM', $this->factory->createNamed('name', 'type', null, $options));
}
public function testCreateBuilderForPropertyWithoutTypeGuesser()
{
$registry = $this->getMock('Symfony\Component\Form\FormRegistryInterface');
$factory = $this->getMockBuilder('Symfony\Component\Form\FormFactory')
->setMethods(array('createNamedBuilder'))
->setConstructorArgs(array($registry, $this->resolvedTypeFactory))
->getMock();
$factory->expects($this->once())
->method('createNamedBuilder')
->with('firstName', 'text', null, array())
->will($this->returnValue('builderInstance'));
$builder = $factory->createBuilderForProperty('Application\Author', 'firstName');
$this->assertEquals('builderInstance', $builder);
}
public function testCreateBuilderForPropertyCreatesFormWithHighestConfidence()
{
$this->guesser1->expects($this->once())

View File

@ -252,6 +252,12 @@ class FormRegistryTest extends \PHPUnit_Framework_TestCase
$expectedGuesser = new FormTypeGuesserChain(array($this->guesser1, $this->guesser2));
$this->assertEquals($expectedGuesser, $this->registry->getTypeGuesser());
$registry = new FormRegistry(
array($this->getMock('Symfony\Component\Form\FormExtensionInterface')),
$this->resolvedTypeFactory);
$this->assertNull($registry->getTypeGuesser());
}
public function testGetExtensions()