This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/tests/Symfony/Tests/Component/Form/FormFactoryTest.php

194 lines
7.0 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\Type\Guesser\Guess;
use Symfony\Component\Form\Type\Guesser\ValueGuess;
use Symfony\Component\Form\Type\Guesser\TypeGuess;
class FormFactoryTest extends \PHPUnit_Framework_TestCase
{
private $typeLoader;
private $rendererFactoryLoader;
private $factory;
protected function setUp()
{
$this->typeLoader = $this->getMock('Symfony\Component\Form\Type\Loader\TypeLoaderInterface');
$this->rendererFactoryLoader = $this->getMock('Symfony\Component\Form\Renderer\Loader\RendererFactoryLoaderInterface');
$this->factory = new FormFactory($this->typeLoader, $this->rendererFactoryLoader);
}
public function testCreateBuilderForPropertyCreatesFieldWithHighestConfidence()
{
$guesser1 = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
$guesser1->expects($this->once())
->method('guessType')
->with('Application\Author', 'firstName')
->will($this->returnValue(new TypeGuess(
'text',
array('max_length' => 10),
Guess::MEDIUM_CONFIDENCE
)));
$guesser2 = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
$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('createBuilder'), array($guesser1, $guesser2));
$factory->expects($this->once())
->method('createBuilder')
->with('password', 'firstName', array('max_length' => 7))
->will($this->returnValue('builderInstance'));
$builder = $factory->createBuilderForProperty('Application\Author', 'firstName');
$this->assertEquals('builderInstance', $builder);
}
public function testCreateBuilderCreatesTextFieldIfNoGuess()
{
$guesser = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
$guesser->expects($this->once())
->method('guessType')
->with('Application\Author', 'firstName')
->will($this->returnValue(null));
$factory = $this->createMockFactory(array('createBuilder'), array($guesser));
$factory->expects($this->once())
->method('createBuilder')
->with('text', 'firstName')
->will($this->returnValue('builderInstance'));
$builder = $factory->createBuilderForProperty('Application\Author', 'firstName');
$this->assertEquals('builderInstance', $builder);
}
public function testOptionsCanBeOverridden()
{
$guesser = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
$guesser->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('createBuilder'), array($guesser));
$factory->expects($this->once())
->method('createBuilder')
->with('text', 'firstName', array('max_length' => 11))
->will($this->returnValue('builderInstance'));
$builder = $factory->createBuilderForProperty(
'Application\Author',
'firstName',
array('max_length' => 11)
);
$this->assertEquals('builderInstance', $builder);
}
public function testCreateBuilderUsesMaxLengthIfFound()
{
$guesser1 = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
$guesser1->expects($this->once())
->method('guessMaxLength')
->with('Application\Author', 'firstName')
->will($this->returnValue(new ValueGuess(
15,
Guess::MEDIUM_CONFIDENCE
)));
$guesser2 = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
$guesser2->expects($this->once())
->method('guessMaxLength')
->with('Application\Author', 'firstName')
->will($this->returnValue(new ValueGuess(
20,
Guess::HIGH_CONFIDENCE
)));
$factory = $this->createMockFactory(array('createBuilder'), array($guesser1, $guesser2));
$factory->expects($this->once())
->method('createBuilder')
->with('text', 'firstName', array('max_length' => 20))
->will($this->returnValue('builderInstance'));
$builder = $factory->createBuilderForProperty(
'Application\Author',
'firstName'
);
$this->assertEquals('builderInstance', $builder);
}
public function testCreateBuilderUsesRequiredSettingWithHighestConfidence()
{
$guesser1 = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
$guesser1->expects($this->once())
->method('guessRequired')
->with('Application\Author', 'firstName')
->will($this->returnValue(new ValueGuess(
true,
Guess::MEDIUM_CONFIDENCE
)));
$guesser2 = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
$guesser2->expects($this->once())
->method('guessRequired')
->with('Application\Author', 'firstName')
->will($this->returnValue(new ValueGuess(
false,
Guess::HIGH_CONFIDENCE
)));
$factory = $this->createMockFactory(array('createBuilder'), array($guesser1, $guesser2));
$factory->expects($this->once())
->method('createBuilder')
->with('text', 'firstName', array('required' => false))
->will($this->returnValue('builderInstance'));
$builder = $factory->createBuilderForProperty(
'Application\Author',
'firstName'
);
$this->assertEquals('builderInstance', $builder);
}
private function createMockFactory(array $methods = array(), array $guessers = array())
{
return $this->getMockBuilder('Symfony\Component\Form\FormFactory')
->setMethods($methods)
->setConstructorArgs(array($this->typeLoader, $this->rendererFactoryLoader, $guessers))
->getMock();
}
}