54e66c518f
The extension classes are now the only constructor argument of the FormFactory class. They replace the existing "type loader" classes. new FormFactory(array( new CoreExtension($validator, $storage), new CsrfExtension($csrfProvider), new DoctrineOrmExtension($em), )); Together with a few upcoming commits this mechanism will make * extension of the form framework in bundles and * usage of the forms outside of Symfony2 much easier.
199 lines
6.7 KiB
PHP
199 lines
6.7 KiB
PHP
<?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\Tests\Component\Form;
|
|
|
|
use Symfony\Component\Form\FormFactory;
|
|
use Symfony\Component\Form\Guess\Guess;
|
|
use Symfony\Component\Form\Guess\ValueGuess;
|
|
use Symfony\Component\Form\Guess\TypeGuess;
|
|
|
|
class FormFactoryTest extends \PHPUnit_Framework_TestCase
|
|
{
|
|
private $extension1;
|
|
|
|
private $extension2;
|
|
|
|
private $guesser1;
|
|
|
|
private $guesser2;
|
|
|
|
private $factory;
|
|
|
|
protected function setUp()
|
|
{
|
|
$this->guesser1 = $this->getMock('Symfony\Component\Form\FormTypeGuesserInterface');
|
|
$this->guesser2 = $this->getMock('Symfony\Component\Form\FormTypeGuesserInterface');
|
|
$this->extension1 = $this->getMock('Symfony\Component\Form\FormExtensionInterface');
|
|
$this->extension1->expects($this->any())
|
|
->method('getTypeGuesser')
|
|
->will($this->returnValue($this->guesser1));
|
|
$this->extension2 = $this->getMock('Symfony\Component\Form\FormExtensionInterface');
|
|
$this->extension2->expects($this->any())
|
|
->method('getTypeGuesser')
|
|
->will($this->returnValue($this->guesser2));
|
|
$this->factory = new FormFactory(array($this->extension1, $this->extension2));
|
|
}
|
|
|
|
public function testCreateBuilderForPropertyCreatesFieldWithHighestConfidence()
|
|
{
|
|
$this->guesser1->expects($this->once())
|
|
->method('guessType')
|
|
->with('Application\Author', 'firstName')
|
|
->will($this->returnValue(new TypeGuess(
|
|
'text',
|
|
array('max_length' => 10),
|
|
Guess::MEDIUM_CONFIDENCE
|
|
)));
|
|
|
|
$this->guesser2->expects($this->once())
|
|
->method('guessType')
|
|
->with('Application\Author', 'firstName')
|
|
->will($this->returnValue(new TypeGuess(
|
|
'password',
|
|
array('max_length' => 7),
|
|
Guess::HIGH_CONFIDENCE
|
|
)));
|
|
|
|
$factory = $this->createMockFactory(array('createNamedBuilder'));
|
|
|
|
$factory->expects($this->once())
|
|
->method('createNamedBuilder')
|
|
->with('password', 'firstName', null, array('max_length' => 7))
|
|
->will($this->returnValue('builderInstance'));
|
|
|
|
$builder = $factory->createBuilderForProperty('Application\Author', 'firstName');
|
|
|
|
$this->assertEquals('builderInstance', $builder);
|
|
}
|
|
|
|
public function testCreateBuilderCreatesTextFieldIfNoGuess()
|
|
{
|
|
$this->guesser1->expects($this->once())
|
|
->method('guessType')
|
|
->with('Application\Author', 'firstName')
|
|
->will($this->returnValue(null));
|
|
|
|
$factory = $this->createMockFactory(array('createNamedBuilder'));
|
|
|
|
$factory->expects($this->once())
|
|
->method('createNamedBuilder')
|
|
->with('text', 'firstName')
|
|
->will($this->returnValue('builderInstance'));
|
|
|
|
$builder = $factory->createBuilderForProperty('Application\Author', 'firstName');
|
|
|
|
$this->assertEquals('builderInstance', $builder);
|
|
}
|
|
|
|
public function testOptionsCanBeOverridden()
|
|
{
|
|
$this->guesser1->expects($this->once())
|
|
->method('guessType')
|
|
->with('Application\Author', 'firstName')
|
|
->will($this->returnValue(new TypeGuess(
|
|
'text',
|
|
array('max_length' => 10),
|
|
Guess::MEDIUM_CONFIDENCE
|
|
)));
|
|
|
|
$factory = $this->createMockFactory(array('createNamedBuilder'));
|
|
|
|
$factory->expects($this->once())
|
|
->method('createNamedBuilder')
|
|
->with('text', 'firstName', null, array('max_length' => 11))
|
|
->will($this->returnValue('builderInstance'));
|
|
|
|
$builder = $factory->createBuilderForProperty(
|
|
'Application\Author',
|
|
'firstName',
|
|
null,
|
|
array('max_length' => 11)
|
|
);
|
|
|
|
$this->assertEquals('builderInstance', $builder);
|
|
}
|
|
|
|
public function testCreateBuilderUsesMaxLengthIfFound()
|
|
{
|
|
$this->guesser1->expects($this->once())
|
|
->method('guessMaxLength')
|
|
->with('Application\Author', 'firstName')
|
|
->will($this->returnValue(new ValueGuess(
|
|
15,
|
|
Guess::MEDIUM_CONFIDENCE
|
|
)));
|
|
|
|
$this->guesser2->expects($this->once())
|
|
->method('guessMaxLength')
|
|
->with('Application\Author', 'firstName')
|
|
->will($this->returnValue(new ValueGuess(
|
|
20,
|
|
Guess::HIGH_CONFIDENCE
|
|
)));
|
|
|
|
$factory = $this->createMockFactory(array('createNamedBuilder'));
|
|
|
|
$factory->expects($this->once())
|
|
->method('createNamedBuilder')
|
|
->with('text', 'firstName', null, array('max_length' => 20))
|
|
->will($this->returnValue('builderInstance'));
|
|
|
|
$builder = $factory->createBuilderForProperty(
|
|
'Application\Author',
|
|
'firstName'
|
|
);
|
|
|
|
$this->assertEquals('builderInstance', $builder);
|
|
}
|
|
|
|
public function testCreateBuilderUsesRequiredSettingWithHighestConfidence()
|
|
{
|
|
$this->guesser1->expects($this->once())
|
|
->method('guessRequired')
|
|
->with('Application\Author', 'firstName')
|
|
->will($this->returnValue(new ValueGuess(
|
|
true,
|
|
Guess::MEDIUM_CONFIDENCE
|
|
)));
|
|
|
|
$this->guesser2->expects($this->once())
|
|
->method('guessRequired')
|
|
->with('Application\Author', 'firstName')
|
|
->will($this->returnValue(new ValueGuess(
|
|
false,
|
|
Guess::HIGH_CONFIDENCE
|
|
)));
|
|
|
|
$factory = $this->createMockFactory(array('createNamedBuilder'));
|
|
|
|
$factory->expects($this->once())
|
|
->method('createNamedBuilder')
|
|
->with('text', 'firstName', null, array('required' => false))
|
|
->will($this->returnValue('builderInstance'));
|
|
|
|
$builder = $factory->createBuilderForProperty(
|
|
'Application\Author',
|
|
'firstName'
|
|
);
|
|
|
|
$this->assertEquals('builderInstance', $builder);
|
|
}
|
|
|
|
private function createMockFactory(array $methods = array())
|
|
{
|
|
return $this->getMockBuilder('Symfony\Component\Form\FormFactory')
|
|
->setMethods($methods)
|
|
->setConstructorArgs(array(array($this->extension1, $this->extension2)))
|
|
->getMock();
|
|
}
|
|
}
|