2011-03-18 13:37:54 +00:00
< ? 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 .
*/
2012-03-28 14:43:52 +01:00
namespace Symfony\Component\Form\Tests ;
2011-03-18 13:37:54 +00:00
2012-07-12 18:17:14 +01:00
use Symfony\Component\Form\FormTypeGuesserChain ;
2011-03-18 13:37:54 +00:00
use Symfony\Component\Form\FormFactory ;
2011-04-22 16:41:21 +01:00
use Symfony\Component\Form\Guess\Guess ;
use Symfony\Component\Form\Guess\ValueGuess ;
use Symfony\Component\Form\Guess\TypeGuess ;
2012-03-23 15:21:47 +00:00
use Symfony\Component\Form\Tests\Fixtures\Author ;
2012-03-28 14:43:52 +01:00
use Symfony\Component\Form\Tests\Fixtures\FooType ;
2012-08-22 15:00:43 +01:00
use Symfony\Component\Form\Tests\Fixtures\FooSubType ;
use Symfony\Component\Form\Tests\Fixtures\FooSubTypeWithParentInstance ;
2011-03-18 13:37:54 +00:00
2012-07-12 18:17:14 +01:00
/**
* @ author Bernhard Schussek < bschussek @ gmail . com >
*/
2011-03-18 13:37:54 +00:00
class FormFactoryTest extends \PHPUnit_Framework_TestCase
{
2012-07-12 18:17:14 +01:00
/**
* @ var \PHPUnit_Framework_MockObject_MockObject
*/
2011-04-22 16:41:21 +01:00
private $guesser1 ;
2012-07-12 18:17:14 +01:00
/**
* @ var \PHPUnit_Framework_MockObject_MockObject
*/
2011-04-22 16:41:21 +01:00
private $guesser2 ;
2011-03-18 13:37:54 +00:00
2012-07-12 18:17:14 +01:00
/**
* @ var \PHPUnit_Framework_MockObject_MockObject
*/
private $registry ;
2012-07-29 18:10:47 +01:00
/**
* @ var \PHPUnit_Framework_MockObject_MockObject
*/
private $resolvedTypeFactory ;
2012-07-12 18:17:14 +01:00
/**
* @ var FormFactory
*/
2011-03-18 13:37:54 +00:00
private $factory ;
protected function setUp ()
{
2012-03-28 14:43:52 +01:00
if ( ! class_exists ( 'Symfony\Component\EventDispatcher\EventDispatcher' )) {
$this -> markTestSkipped ( 'The "EventDispatcher" component is not available' );
}
2012-07-29 18:10:47 +01:00
$this -> resolvedTypeFactory = $this -> getMock ( 'Symfony\Component\Form\ResolvedFormTypeFactoryInterface' );
2011-04-22 16:41:21 +01:00
$this -> guesser1 = $this -> getMock ( 'Symfony\Component\Form\FormTypeGuesserInterface' );
$this -> guesser2 = $this -> getMock ( 'Symfony\Component\Form\FormTypeGuesserInterface' );
2012-07-12 18:17:14 +01:00
$this -> registry = $this -> getMock ( 'Symfony\Component\Form\FormRegistryInterface' );
2012-07-29 18:10:47 +01:00
$this -> factory = new FormFactory ( $this -> registry , $this -> resolvedTypeFactory );
2011-03-18 13:37:54 +00:00
2012-07-12 18:17:14 +01:00
$this -> registry -> expects ( $this -> any ())
-> method ( 'getTypeGuesser' )
-> will ( $this -> returnValue ( new FormTypeGuesserChain ( array (
$this -> guesser1 ,
$this -> guesser2 ,
))));
2011-06-14 09:44:38 +01:00
}
2011-05-13 17:29:18 +01:00
public function testAddType ()
{
$type = new FooType ();
2012-07-12 18:17:14 +01:00
$resolvedType = $this -> getMockResolvedType ();
2011-05-13 17:29:18 +01:00
2012-07-29 18:10:47 +01:00
$this -> resolvedTypeFactory -> expects ( $this -> once ())
-> method ( 'createResolvedType' )
2012-07-12 18:17:14 +01:00
-> with ( $type )
-> will ( $this -> returnValue ( $resolvedType ));
2011-05-13 17:29:18 +01:00
2012-07-12 18:17:14 +01:00
$this -> registry -> expects ( $this -> once ())
-> method ( 'addType' )
-> with ( $resolvedType );
2011-05-13 17:29:18 +01:00
2012-12-14 06:31:09 +00:00
set_error_handler ( array ( 'Symfony\Component\Form\Tests\DeprecationErrorHandler' , 'handle' ));
2011-05-13 17:29:18 +01:00
$this -> factory -> addType ( $type );
2012-12-14 06:31:09 +00:00
restore_error_handler ();
2011-05-13 17:29:18 +01:00
}
2012-07-12 18:17:14 +01:00
public function testHasType ()
2011-05-13 17:29:18 +01:00
{
2012-07-12 18:17:14 +01:00
$this -> registry -> expects ( $this -> once ())
-> method ( 'hasType' )
-> with ( 'name' )
-> will ( $this -> returnValue ( 'RESULT' ));
2011-05-13 17:29:18 +01:00
2012-12-14 06:31:09 +00:00
set_error_handler ( array ( 'Symfony\Component\Form\Tests\DeprecationErrorHandler' , 'handle' ));
2012-07-12 18:17:14 +01:00
$this -> assertSame ( 'RESULT' , $this -> factory -> hasType ( 'name' ));
2012-12-14 06:31:09 +00:00
restore_error_handler ();
2011-05-13 17:29:18 +01:00
}
2012-07-12 18:17:14 +01:00
public function testGetType ()
2011-05-13 17:29:18 +01:00
{
$type = new FooType ();
2012-07-12 18:17:14 +01:00
$resolvedType = $this -> getMockResolvedType ();
2011-05-13 17:29:18 +01:00
2012-07-12 18:17:14 +01:00
$resolvedType -> expects ( $this -> once ())
-> method ( 'getInnerType' )
-> will ( $this -> returnValue ( $type ));
2011-05-13 17:29:18 +01:00
2012-07-12 18:17:14 +01:00
$this -> registry -> expects ( $this -> once ())
-> method ( 'getType' )
-> with ( 'name' )
-> will ( $this -> returnValue ( $resolvedType ));
2011-05-13 17:29:18 +01:00
2012-12-14 06:31:09 +00:00
set_error_handler ( array ( 'Symfony\Component\Form\Tests\DeprecationErrorHandler' , 'handle' ));
2012-07-12 18:17:14 +01:00
$this -> assertEquals ( $type , $this -> factory -> getType ( 'name' ));
2012-12-14 06:31:09 +00:00
restore_error_handler ();
2011-05-13 17:29:18 +01:00
}
2012-07-12 18:17:14 +01:00
public function testCreateNamedBuilderWithTypeName ()
2011-05-13 17:29:18 +01:00
{
2012-07-12 18:17:14 +01:00
$options = array ( 'a' => '1' , 'b' => '2' );
$resolvedType = $this -> getMockResolvedType ();
2011-05-13 17:29:18 +01:00
2012-07-12 18:17:14 +01:00
$this -> registry -> expects ( $this -> once ())
-> method ( 'getType' )
-> with ( 'type' )
-> will ( $this -> returnValue ( $resolvedType ));
2011-05-13 17:29:18 +01:00
2012-07-12 18:17:14 +01:00
$resolvedType -> expects ( $this -> once ())
-> method ( 'createBuilder' )
-> with ( $this -> factory , 'name' , $options )
-> will ( $this -> returnValue ( 'BUILDER' ));
2011-05-13 17:29:18 +01:00
2012-07-12 18:17:14 +01:00
$this -> assertSame ( 'BUILDER' , $this -> factory -> createNamedBuilder ( 'name' , 'type' , null , $options ));
2011-05-13 17:29:18 +01:00
}
2012-07-12 18:17:14 +01:00
public function testCreateNamedBuilderWithTypeInstance ()
2011-05-13 17:29:18 +01:00
{
2012-07-12 18:17:14 +01:00
$options = array ( 'a' => '1' , 'b' => '2' );
2012-08-22 15:00:43 +01:00
$type = new FooType ();
2012-07-12 18:17:14 +01:00
$resolvedType = $this -> getMockResolvedType ();
2011-05-13 17:29:18 +01:00
2012-07-29 18:10:47 +01:00
$this -> resolvedTypeFactory -> expects ( $this -> once ())
-> method ( 'createResolvedType' )
2012-07-12 18:17:14 +01:00
-> with ( $type )
-> will ( $this -> returnValue ( $resolvedType ));
2011-05-13 17:29:18 +01:00
2012-07-12 18:17:14 +01:00
$resolvedType -> expects ( $this -> once ())
-> method ( 'createBuilder' )
-> with ( $this -> factory , 'name' , $options )
-> will ( $this -> returnValue ( 'BUILDER' ));
2011-05-13 17:29:18 +01:00
2012-07-12 18:17:14 +01:00
$this -> assertSame ( 'BUILDER' , $this -> factory -> createNamedBuilder ( 'name' , $type , null , $options ));
2011-05-13 17:29:18 +01:00
}
2012-08-22 15:00:43 +01:00
public function testCreateNamedBuilderWithTypeInstanceWithParentType ()
{
$options = array ( 'a' => '1' , 'b' => '2' );
$type = new FooSubType ();
$resolvedType = $this -> getMockResolvedType ();
$parentResolvedType = $this -> getMockResolvedType ();
$this -> registry -> expects ( $this -> once ())
-> method ( 'getType' )
-> with ( 'foo' )
-> will ( $this -> returnValue ( $parentResolvedType ));
$this -> resolvedTypeFactory -> expects ( $this -> once ())
-> method ( 'createResolvedType' )
-> with ( $type , array (), $parentResolvedType )
-> will ( $this -> returnValue ( $resolvedType ));
$resolvedType -> expects ( $this -> once ())
-> method ( 'createBuilder' )
-> with ( $this -> factory , 'name' , $options )
-> will ( $this -> returnValue ( 'BUILDER' ));
$this -> assertSame ( 'BUILDER' , $this -> factory -> createNamedBuilder ( 'name' , $type , null , $options ));
}
public function testCreateNamedBuilderWithTypeInstanceWithParentTypeInstance ()
{
$options = array ( 'a' => '1' , 'b' => '2' );
$type = new FooSubTypeWithParentInstance ();
$resolvedType = $this -> getMockResolvedType ();
$parentResolvedType = $this -> getMockResolvedType ();
$this -> resolvedTypeFactory -> expects ( $this -> at ( 0 ))
-> method ( 'createResolvedType' )
-> with ( $type -> getParent ())
-> will ( $this -> returnValue ( $parentResolvedType ));
$this -> resolvedTypeFactory -> expects ( $this -> at ( 1 ))
-> method ( 'createResolvedType' )
-> with ( $type , array (), $parentResolvedType )
-> will ( $this -> returnValue ( $resolvedType ));
$resolvedType -> expects ( $this -> once ())
-> method ( 'createBuilder' )
-> with ( $this -> factory , 'name' , $options )
-> will ( $this -> returnValue ( 'BUILDER' ));
$this -> assertSame ( 'BUILDER' , $this -> factory -> createNamedBuilder ( 'name' , $type , null , $options ));
}
2012-07-12 18:17:14 +01:00
public function testCreateNamedBuilderWithResolvedTypeInstance ()
2011-05-13 17:29:18 +01:00
{
2012-07-12 18:17:14 +01:00
$options = array ( 'a' => '1' , 'b' => '2' );
$resolvedType = $this -> getMockResolvedType ();
2011-05-13 17:29:18 +01:00
2012-07-12 18:17:14 +01:00
$resolvedType -> expects ( $this -> once ())
-> method ( 'createBuilder' )
-> with ( $this -> factory , 'name' , $options )
-> will ( $this -> returnValue ( 'BUILDER' ));
$this -> assertSame ( 'BUILDER' , $this -> factory -> createNamedBuilder ( 'name' , $resolvedType , null , $options ));
2011-05-13 17:29:18 +01:00
}
2012-07-12 18:17:14 +01:00
public function testCreateNamedBuilderWithParentBuilder ()
2012-07-13 11:12:25 +01:00
{
2012-07-12 18:17:14 +01:00
$options = array ( 'a' => '1' , 'b' => '2' );
$parentBuilder = $this -> getMockFormBuilder ();
$resolvedType = $this -> getMockResolvedType ();
$this -> registry -> expects ( $this -> once ())
-> method ( 'getType' )
-> with ( 'type' )
-> will ( $this -> returnValue ( $resolvedType ));
2012-07-13 11:12:25 +01:00
2012-07-12 18:17:14 +01:00
$resolvedType -> expects ( $this -> once ())
-> method ( 'createBuilder' )
-> with ( $this -> factory , 'name' , $options , $parentBuilder )
-> will ( $this -> returnValue ( 'BUILDER' ));
2012-07-13 11:12:25 +01:00
2012-07-12 18:17:14 +01:00
$this -> assertSame ( 'BUILDER' , $this -> factory -> createNamedBuilder ( 'name' , 'type' , null , $options , $parentBuilder ));
2012-07-13 11:12:25 +01:00
}
2012-07-12 18:17:14 +01:00
public function testCreateNamedBuilderFillsDataOption ()
2011-05-13 17:29:18 +01:00
{
2012-07-12 18:17:14 +01:00
$givenOptions = array ( 'a' => '1' , 'b' => '2' );
$expectedOptions = array_merge ( $givenOptions , array ( 'data' => 'DATA' ));
$resolvedType = $this -> getMockResolvedType ();
$this -> registry -> expects ( $this -> once ())
-> method ( 'getType' )
-> with ( 'type' )
-> will ( $this -> returnValue ( $resolvedType ));
2011-05-13 17:29:18 +01:00
2012-07-12 18:17:14 +01:00
$resolvedType -> expects ( $this -> once ())
-> method ( 'createBuilder' )
-> with ( $this -> factory , 'name' , $expectedOptions )
-> will ( $this -> returnValue ( 'BUILDER' ));
2011-05-13 17:29:18 +01:00
2012-07-12 18:17:14 +01:00
$this -> assertSame ( 'BUILDER' , $this -> factory -> createNamedBuilder ( 'name' , 'type' , 'DATA' , $givenOptions ));
2011-05-13 17:29:18 +01:00
}
2012-07-12 18:17:14 +01:00
public function testCreateNamedBuilderDoesNotOverrideExistingDataOption ()
2011-05-13 17:29:18 +01:00
{
2012-07-12 18:17:14 +01:00
$options = array ( 'a' => '1' , 'b' => '2' , 'data' => 'CUSTOM' );
$resolvedType = $this -> getMockResolvedType ();
2011-05-13 17:29:18 +01:00
2012-07-12 18:17:14 +01:00
$this -> registry -> expects ( $this -> once ())
-> method ( 'getType' )
-> with ( 'type' )
-> will ( $this -> returnValue ( $resolvedType ));
2011-05-13 17:29:18 +01:00
2012-07-12 18:17:14 +01:00
$resolvedType -> expects ( $this -> once ())
-> method ( 'createBuilder' )
-> with ( $this -> factory , 'name' , $options )
-> will ( $this -> returnValue ( 'BUILDER' ));
$this -> assertSame ( 'BUILDER' , $this -> factory -> createNamedBuilder ( 'name' , 'type' , 'DATA' , $options ));
2011-05-13 17:29:18 +01:00
}
/**
2012-07-12 18:17:14 +01:00
* @ expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
* @ expectedExceptionMessage Expected argument of type " string, Symfony \ Component \ Form \R esolvedFormTypeInterface or Symfony \ Component \ Form \ FormTypeInterface " , " stdClass " given
2011-05-13 17:29:18 +01:00
*/
2012-07-12 18:17:14 +01:00
public function testCreateNamedBuilderThrowsUnderstandableException ()
2011-05-13 17:29:18 +01:00
{
2012-07-12 18:17:14 +01:00
$this -> factory -> createNamedBuilder ( 'name' , new \stdClass ());
2011-05-13 17:29:18 +01:00
}
2012-07-12 18:17:14 +01:00
public function testCreateUsesTypeNameIfTypeGivenAsString ()
2011-05-13 17:29:18 +01:00
{
2012-07-12 18:17:14 +01:00
$options = array ( 'a' => '1' , 'b' => '2' );
$resolvedType = $this -> getMockResolvedType ();
$builder = $this -> getMockFormBuilder ();
2011-05-13 17:29:18 +01:00
2012-07-12 18:17:14 +01:00
$this -> registry -> expects ( $this -> once ())
-> method ( 'getType' )
-> with ( 'TYPE' )
-> will ( $this -> returnValue ( $resolvedType ));
2011-05-13 17:29:18 +01:00
2012-07-12 18:17:14 +01:00
$resolvedType -> expects ( $this -> once ())
2011-05-13 17:29:18 +01:00
-> method ( 'createBuilder' )
2012-07-12 18:17:14 +01:00
-> with ( $this -> factory , 'TYPE' , $options )
-> will ( $this -> returnValue ( $builder ));
2011-05-13 17:29:18 +01:00
2012-07-12 18:17:14 +01:00
$builder -> expects ( $this -> once ())
-> method ( 'getForm' )
-> will ( $this -> returnValue ( 'FORM' ));
2011-05-13 17:29:18 +01:00
2012-07-12 18:17:14 +01:00
$this -> assertSame ( 'FORM' , $this -> factory -> create ( 'TYPE' , null , $options ));
2011-05-13 17:29:18 +01:00
}
2012-07-12 18:17:14 +01:00
public function testCreateUsesTypeNameIfTypeGivenAsObject ()
2011-05-13 17:29:18 +01:00
{
2012-07-12 18:17:14 +01:00
$options = array ( 'a' => '1' , 'b' => '2' );
$resolvedType = $this -> getMockResolvedType ();
$builder = $this -> getMockFormBuilder ();
2011-05-13 17:29:18 +01:00
2012-07-12 18:17:14 +01:00
$resolvedType -> expects ( $this -> once ())
-> method ( 'getName' )
-> will ( $this -> returnValue ( 'TYPE' ));
2011-05-13 17:41:23 +01:00
2012-07-12 18:17:14 +01:00
$resolvedType -> expects ( $this -> once ())
-> method ( 'createBuilder' )
-> with ( $this -> factory , 'TYPE' , $options )
-> will ( $this -> returnValue ( $builder ));
2011-05-13 17:41:23 +01:00
2012-07-12 18:17:14 +01:00
$builder -> expects ( $this -> once ())
-> method ( 'getForm' )
-> will ( $this -> returnValue ( 'FORM' ));
2011-05-13 17:41:23 +01:00
2012-07-12 18:17:14 +01:00
$this -> assertSame ( 'FORM' , $this -> factory -> create ( $resolvedType , null , $options ));
2011-05-13 17:41:23 +01:00
}
2012-07-12 18:17:14 +01:00
public function testCreateNamed ()
2011-05-13 17:29:18 +01:00
{
2012-07-12 18:17:14 +01:00
$options = array ( 'a' => '1' , 'b' => '2' );
$resolvedType = $this -> getMockResolvedType ();
$builder = $this -> getMockFormBuilder ();
2011-05-13 17:29:18 +01:00
2012-07-12 18:17:14 +01:00
$this -> registry -> expects ( $this -> once ())
-> method ( 'getType' )
-> with ( 'type' )
-> will ( $this -> returnValue ( $resolvedType ));
2011-05-13 17:29:18 +01:00
2012-07-12 18:17:14 +01:00
$resolvedType -> expects ( $this -> once ())
-> method ( 'createBuilder' )
-> with ( $this -> factory , 'name' , $options )
-> will ( $this -> returnValue ( $builder ));
2011-05-13 17:29:18 +01:00
2012-07-12 18:17:14 +01:00
$builder -> expects ( $this -> once ())
-> method ( 'getForm' )
-> will ( $this -> returnValue ( 'FORM' ));
2011-05-13 17:29:18 +01:00
2012-07-12 18:17:14 +01:00
$this -> assertSame ( 'FORM' , $this -> factory -> createNamed ( 'name' , 'type' , null , $options ));
2011-05-13 17:29:18 +01:00
}
2012-11-08 18:49:46 +00:00
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 );
}
2012-04-13 15:06:32 +01:00
public function testCreateBuilderForPropertyCreatesFormWithHighestConfidence ()
2011-03-18 13:37:54 +00:00
{
2011-04-22 16:41:21 +01:00
$this -> guesser1 -> expects ( $this -> once ())
2011-03-18 15:20:00 +00:00
-> method ( 'guessType' )
2011-03-18 13:37:54 +00:00
-> with ( 'Application\Author' , 'firstName' )
2011-03-19 15:46:03 +00:00
-> will ( $this -> returnValue ( new TypeGuess (
2011-03-18 13:37:54 +00:00
'text' ,
array ( 'max_length' => 10 ),
2011-03-19 15:46:03 +00:00
Guess :: MEDIUM_CONFIDENCE
2011-03-18 13:37:54 +00:00
)));
2011-04-22 16:41:21 +01:00
$this -> guesser2 -> expects ( $this -> once ())
2011-03-18 15:20:00 +00:00
-> method ( 'guessType' )
2011-03-18 13:37:54 +00:00
-> with ( 'Application\Author' , 'firstName' )
2011-03-19 15:46:03 +00:00
-> will ( $this -> returnValue ( new TypeGuess (
2011-03-18 13:37:54 +00:00
'password' ,
array ( 'max_length' => 7 ),
2011-03-19 15:46:03 +00:00
Guess :: HIGH_CONFIDENCE
2011-03-18 13:37:54 +00:00
)));
2012-07-12 18:17:14 +01:00
$factory = $this -> getMockFactory ( array ( 'createNamedBuilder' ));
2011-03-18 13:37:54 +00:00
$factory -> expects ( $this -> once ())
2011-04-22 09:42:21 +01:00
-> method ( 'createNamedBuilder' )
2012-05-23 20:50:13 +01:00
-> with ( 'firstName' , 'password' , null , array ( 'max_length' => 7 ))
2011-03-18 13:37:54 +00:00
-> will ( $this -> returnValue ( 'builderInstance' ));
$builder = $factory -> createBuilderForProperty ( 'Application\Author' , 'firstName' );
$this -> assertEquals ( 'builderInstance' , $builder );
}
2012-04-13 15:06:32 +01:00
public function testCreateBuilderCreatesTextFormIfNoGuess ()
2011-03-18 13:37:54 +00:00
{
2011-04-22 16:41:21 +01:00
$this -> guesser1 -> expects ( $this -> once ())
2011-03-18 15:20:00 +00:00
-> method ( 'guessType' )
2011-03-18 13:37:54 +00:00
-> with ( 'Application\Author' , 'firstName' )
-> will ( $this -> returnValue ( null ));
2012-07-12 18:17:14 +01:00
$factory = $this -> getMockFactory ( array ( 'createNamedBuilder' ));
2011-03-18 13:37:54 +00:00
$factory -> expects ( $this -> once ())
2011-04-22 09:42:21 +01:00
-> method ( 'createNamedBuilder' )
2012-05-23 20:50:13 +01:00
-> with ( 'firstName' , 'text' )
2011-03-18 13:37:54 +00:00
-> will ( $this -> returnValue ( 'builderInstance' ));
$builder = $factory -> createBuilderForProperty ( 'Application\Author' , 'firstName' );
$this -> assertEquals ( 'builderInstance' , $builder );
}
public function testOptionsCanBeOverridden ()
{
2011-04-22 16:41:21 +01:00
$this -> guesser1 -> expects ( $this -> once ())
2011-03-18 15:20:00 +00:00
-> method ( 'guessType' )
2011-03-18 13:37:54 +00:00
-> with ( 'Application\Author' , 'firstName' )
2011-03-19 15:46:03 +00:00
-> will ( $this -> returnValue ( new TypeGuess (
2011-03-18 13:37:54 +00:00
'text' ,
array ( 'max_length' => 10 ),
2011-03-19 15:46:03 +00:00
Guess :: MEDIUM_CONFIDENCE
2011-03-18 13:37:54 +00:00
)));
2012-07-12 18:17:14 +01:00
$factory = $this -> getMockFactory ( array ( 'createNamedBuilder' ));
2011-03-18 13:37:54 +00:00
$factory -> expects ( $this -> once ())
2011-04-22 09:42:21 +01:00
-> method ( 'createNamedBuilder' )
2012-05-23 20:50:13 +01:00
-> with ( 'firstName' , 'text' , null , array ( 'max_length' => 11 ))
2011-03-18 13:37:54 +00:00
-> will ( $this -> returnValue ( 'builderInstance' ));
$builder = $factory -> createBuilderForProperty (
'Application\Author' ,
'firstName' ,
2011-04-22 09:42:21 +01:00
null ,
2011-03-18 13:37:54 +00:00
array ( 'max_length' => 11 )
);
$this -> assertEquals ( 'builderInstance' , $builder );
}
public function testCreateBuilderUsesMaxLengthIfFound ()
{
2011-04-22 16:41:21 +01:00
$this -> guesser1 -> expects ( $this -> once ())
2011-03-18 13:37:54 +00:00
-> method ( 'guessMaxLength' )
-> with ( 'Application\Author' , 'firstName' )
2011-03-19 15:46:03 +00:00
-> will ( $this -> returnValue ( new ValueGuess (
2011-03-18 13:37:54 +00:00
15 ,
2011-03-19 15:46:03 +00:00
Guess :: MEDIUM_CONFIDENCE
2011-03-18 13:37:54 +00:00
)));
2011-04-22 16:41:21 +01:00
$this -> guesser2 -> expects ( $this -> once ())
2011-03-18 13:37:54 +00:00
-> method ( 'guessMaxLength' )
-> with ( 'Application\Author' , 'firstName' )
2011-03-19 15:46:03 +00:00
-> will ( $this -> returnValue ( new ValueGuess (
2011-03-18 13:37:54 +00:00
20 ,
2011-03-19 15:46:03 +00:00
Guess :: HIGH_CONFIDENCE
2011-03-18 13:37:54 +00:00
)));
2012-07-12 18:17:14 +01:00
$factory = $this -> getMockFactory ( array ( 'createNamedBuilder' ));
2011-03-18 13:37:54 +00:00
$factory -> expects ( $this -> once ())
2011-04-22 09:42:21 +01:00
-> method ( 'createNamedBuilder' )
2012-05-23 20:50:13 +01:00
-> with ( 'firstName' , 'text' , null , array ( 'max_length' => 20 ))
2011-03-18 13:37:54 +00:00
-> will ( $this -> returnValue ( 'builderInstance' ));
$builder = $factory -> createBuilderForProperty (
'Application\Author' ,
'firstName'
);
$this -> assertEquals ( 'builderInstance' , $builder );
}
2012-04-23 14:55:54 +01:00
public function testCreateBuilderUsesMinLengthIfFound ()
{
$this -> guesser1 -> expects ( $this -> once ())
-> method ( 'guessMinLength' )
-> with ( 'Application\Author' , 'firstName' )
-> will ( $this -> returnValue ( new ValueGuess (
2 ,
Guess :: MEDIUM_CONFIDENCE
)));
$this -> guesser2 -> expects ( $this -> once ())
-> method ( 'guessMinLength' )
-> with ( 'Application\Author' , 'firstName' )
-> will ( $this -> returnValue ( new ValueGuess (
5 ,
Guess :: HIGH_CONFIDENCE
)));
2012-07-12 18:17:14 +01:00
$factory = $this -> getMockFactory ( array ( 'createNamedBuilder' ));
2012-04-23 14:55:54 +01:00
$factory -> expects ( $this -> once ())
-> method ( 'createNamedBuilder' )
2012-05-23 20:50:13 +01:00
-> with ( 'firstName' , 'text' , null , array ( 'pattern' => '.{5,}' ))
2012-04-23 14:55:54 +01:00
-> will ( $this -> returnValue ( 'builderInstance' ));
$builder = $factory -> createBuilderForProperty (
'Application\Author' ,
'firstName'
);
$this -> assertEquals ( 'builderInstance' , $builder );
}
public function testCreateBuilderPrefersPatternOverMinLength ()
{
// min length is deprecated
$this -> guesser1 -> expects ( $this -> once ())
-> method ( 'guessMinLength' )
-> with ( 'Application\Author' , 'firstName' )
-> will ( $this -> returnValue ( new ValueGuess (
2 ,
Guess :: HIGH_CONFIDENCE
)));
// pattern is preferred even though confidence is lower
$this -> guesser2 -> expects ( $this -> once ())
-> method ( 'guessPattern' )
-> with ( 'Application\Author' , 'firstName' )
-> will ( $this -> returnValue ( new ValueGuess (
'.{5,10}' ,
Guess :: LOW_CONFIDENCE
)));
2012-07-12 18:17:14 +01:00
$factory = $this -> getMockFactory ( array ( 'createNamedBuilder' ));
2012-04-23 14:55:54 +01:00
$factory -> expects ( $this -> once ())
-> method ( 'createNamedBuilder' )
2012-05-23 20:50:13 +01:00
-> with ( 'firstName' , 'text' , null , array ( 'pattern' => '.{5,10}' ))
2012-04-23 14:55:54 +01:00
-> will ( $this -> returnValue ( 'builderInstance' ));
$builder = $factory -> createBuilderForProperty (
'Application\Author' ,
'firstName'
);
$this -> assertEquals ( 'builderInstance' , $builder );
}
2011-03-18 13:37:54 +00:00
public function testCreateBuilderUsesRequiredSettingWithHighestConfidence ()
{
2011-04-22 16:41:21 +01:00
$this -> guesser1 -> expects ( $this -> once ())
2011-03-18 13:37:54 +00:00
-> method ( 'guessRequired' )
-> with ( 'Application\Author' , 'firstName' )
2011-03-19 15:46:03 +00:00
-> will ( $this -> returnValue ( new ValueGuess (
2011-03-18 13:37:54 +00:00
true ,
2011-03-19 15:46:03 +00:00
Guess :: MEDIUM_CONFIDENCE
2011-03-18 13:37:54 +00:00
)));
2011-04-22 16:41:21 +01:00
$this -> guesser2 -> expects ( $this -> once ())
2011-03-18 13:37:54 +00:00
-> method ( 'guessRequired' )
-> with ( 'Application\Author' , 'firstName' )
2011-03-19 15:46:03 +00:00
-> will ( $this -> returnValue ( new ValueGuess (
2011-03-18 13:37:54 +00:00
false ,
2011-03-19 15:46:03 +00:00
Guess :: HIGH_CONFIDENCE
2011-03-18 13:37:54 +00:00
)));
2012-07-12 18:17:14 +01:00
$factory = $this -> getMockFactory ( array ( 'createNamedBuilder' ));
2011-03-18 13:37:54 +00:00
$factory -> expects ( $this -> once ())
2011-04-22 09:42:21 +01:00
-> method ( 'createNamedBuilder' )
2012-05-23 20:50:13 +01:00
-> with ( 'firstName' , 'text' , null , array ( 'required' => false ))
2011-03-18 13:37:54 +00:00
-> will ( $this -> returnValue ( 'builderInstance' ));
$builder = $factory -> createBuilderForProperty (
'Application\Author' ,
'firstName'
);
$this -> assertEquals ( 'builderInstance' , $builder );
}
2012-04-13 16:07:09 +01:00
public function testCreateBuilderUsesPatternIfFound ()
{
$this -> guesser1 -> expects ( $this -> once ())
-> method ( 'guessPattern' )
-> with ( 'Application\Author' , 'firstName' )
-> will ( $this -> returnValue ( new ValueGuess (
2012-06-08 05:32:21 +01:00
'[a-z]' ,
2012-04-13 16:07:09 +01:00
Guess :: MEDIUM_CONFIDENCE
)));
$this -> guesser2 -> expects ( $this -> once ())
-> method ( 'guessPattern' )
-> with ( 'Application\Author' , 'firstName' )
-> will ( $this -> returnValue ( new ValueGuess (
2012-06-08 05:32:21 +01:00
'[a-zA-Z]' ,
2012-04-13 16:07:09 +01:00
Guess :: HIGH_CONFIDENCE
)));
2012-07-12 18:17:14 +01:00
$factory = $this -> getMockFactory ( array ( 'createNamedBuilder' ));
2012-04-13 16:07:09 +01:00
$factory -> expects ( $this -> once ())
-> method ( 'createNamedBuilder' )
2012-06-08 05:32:21 +01:00
-> with ( 'firstName' , 'text' , null , array ( 'pattern' => '[a-zA-Z]' ))
2012-04-13 16:07:09 +01:00
-> will ( $this -> returnValue ( 'builderInstance' ));
$builder = $factory -> createBuilderForProperty (
'Application\Author' ,
'firstName'
);
$this -> assertEquals ( 'builderInstance' , $builder );
}
2012-07-12 18:17:14 +01:00
private function getMockFactory ( array $methods = array ())
2011-12-14 14:01:09 +00:00
{
2012-07-12 18:17:14 +01:00
return $this -> getMockBuilder ( 'Symfony\Component\Form\FormFactory' )
-> setMethods ( $methods )
2012-07-29 18:10:47 +01:00
-> setConstructorArgs ( array ( $this -> registry , $this -> resolvedTypeFactory ))
2012-07-12 18:17:14 +01:00
-> getMock ();
2011-12-14 14:01:09 +00:00
}
2012-07-12 18:17:14 +01:00
private function getMockResolvedType ()
2012-04-13 16:07:09 +01:00
{
2012-07-12 18:17:14 +01:00
return $this -> getMock ( 'Symfony\Component\Form\ResolvedFormTypeInterface' );
}
2012-04-13 16:07:09 +01:00
2012-07-12 18:17:14 +01:00
private function getMockType ()
{
return $this -> getMock ( 'Symfony\Component\Form\FormTypeInterface' );
2012-03-23 15:21:47 +00:00
}
2012-07-12 18:17:14 +01:00
private function getMockFormBuilder ()
2011-03-18 13:37:54 +00:00
{
2012-07-12 18:17:14 +01:00
return $this -> getMock ( 'Symfony\Component\Form\Tests\FormBuilderInterface' );
2011-03-18 13:37:54 +00:00
}
2011-03-24 21:20:54 +00:00
}