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-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 ;
2013-09-17 10:39:17 +01:00
/**
* @ var \PHPUnit_Framework_MockObject_MockObject
*/
private $builder ;
2012-07-12 18:17:14 +01:00
/**
* @ var FormFactory
*/
2011-03-18 13:37:54 +00:00
private $factory ;
protected function setUp ()
{
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' );
2013-09-17 10:39:17 +01:00
$this -> builder = $this -> getMock ( 'Symfony\Component\Form\Test\FormBuilderInterface' );
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
}
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' );
2013-09-17 10:39:17 +01:00
$resolvedOptions = array ( 'a' => '2' , 'b' => '3' );
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
$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 )
2013-09-17 10:39:17 +01:00
-> will ( $this -> returnValue ( $this -> builder ));
$this -> builder -> expects ( $this -> any ())
-> method ( 'getOptions' )
-> will ( $this -> returnValue ( $resolvedOptions ));
2011-05-13 17:29:18 +01:00
2013-09-17 10:39:17 +01:00
$resolvedType -> expects ( $this -> once ())
-> method ( 'buildForm' )
-> with ( $this -> builder , $resolvedOptions );
$this -> assertSame ( $this -> 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' );
2013-09-17 10:39:17 +01:00
$resolvedOptions = array ( 'a' => '2' , 'b' => '3' );
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 )
2013-09-17 10:39:17 +01:00
-> 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 );
2011-05-13 17:29:18 +01:00
2013-09-17 10:39:17 +01:00
$this -> assertSame ( $this -> 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' );
2013-09-17 10:39:17 +01:00
$resolvedOptions = array ( 'a' => '2' , 'b' => '3' );
2012-08-22 15:00:43 +01:00
$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 )
2013-09-17 10:39:17 +01:00
-> will ( $this -> returnValue ( $this -> builder ));
2012-08-22 15:00:43 +01:00
2013-09-17 10:39:17 +01:00
$this -> builder -> expects ( $this -> any ())
-> method ( 'getOptions' )
-> will ( $this -> returnValue ( $resolvedOptions ));
$resolvedType -> expects ( $this -> once ())
-> method ( 'buildForm' )
-> with ( $this -> builder , $resolvedOptions );
$this -> assertSame ( $this -> builder , $this -> factory -> createNamedBuilder ( 'name' , $type , null , $options ));
2012-08-22 15:00:43 +01:00
}
public function testCreateNamedBuilderWithTypeInstanceWithParentTypeInstance ()
{
$options = array ( 'a' => '1' , 'b' => '2' );
2013-09-17 10:39:17 +01:00
$resolvedOptions = array ( 'a' => '2' , 'b' => '3' );
2012-08-22 15:00:43 +01:00
$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 )
2013-09-17 10:39:17 +01:00
-> will ( $this -> returnValue ( $this -> builder ));
2012-08-22 15:00:43 +01:00
2013-09-17 10:39:17 +01:00
$this -> builder -> expects ( $this -> any ())
-> method ( 'getOptions' )
-> will ( $this -> returnValue ( $resolvedOptions ));
$resolvedType -> expects ( $this -> once ())
-> method ( 'buildForm' )
-> with ( $this -> builder , $resolvedOptions );
$this -> assertSame ( $this -> builder , $this -> factory -> createNamedBuilder ( 'name' , $type , null , $options ));
2012-08-22 15:00:43 +01:00
}
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' );
2013-09-17 10:39:17 +01:00
$resolvedOptions = array ( 'a' => '2' , 'b' => '3' );
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 ( 'createBuilder' )
-> with ( $this -> factory , 'name' , $options )
2013-09-17 10:39:17 +01:00
-> 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 );
2012-07-12 18:17:14 +01:00
2013-09-17 10:39:17 +01:00
$this -> assertSame ( $this -> 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 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' ));
2013-09-17 10:39:17 +01:00
$resolvedOptions = array ( 'a' => '2' , 'b' => '3' , 'data' => 'DATA' );
2012-07-12 18:17:14 +01:00
$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 )
2013-09-17 10:39:17 +01:00
-> will ( $this -> returnValue ( $this -> builder ));
2011-05-13 17:29:18 +01:00
2013-09-17 10:39:17 +01:00
$this -> builder -> expects ( $this -> any ())
-> method ( 'getOptions' )
-> will ( $this -> returnValue ( $resolvedOptions ));
$resolvedType -> expects ( $this -> once ())
-> method ( 'buildForm' )
-> with ( $this -> builder , $resolvedOptions );
$this -> assertSame ( $this -> 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' );
2013-09-17 10:39:17 +01:00
$resolvedOptions = array ( 'a' => '2' , 'b' => '3' , 'data' => 'CUSTOM' );
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
$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 )
2013-09-17 10:39:17 +01:00
-> will ( $this -> returnValue ( $this -> builder ));
2012-07-12 18:17:14 +01:00
2013-09-17 10:39:17 +01:00
$this -> builder -> expects ( $this -> any ())
-> method ( 'getOptions' )
-> will ( $this -> returnValue ( $resolvedOptions ));
$resolvedType -> expects ( $this -> once ())
-> method ( 'buildForm' )
-> with ( $this -> builder , $resolvedOptions );
$this -> assertSame ( $this -> builder , $this -> factory -> createNamedBuilder ( 'name' , 'type' , 'DATA' , $options ));
2011-05-13 17:29:18 +01:00
}
/**
2013-04-13 14:20:03 +01:00
* @ expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
2012-07-12 18:17:14 +01:00
* @ 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' );
2013-09-17 10:39:17 +01:00
$resolvedOptions = array ( 'a' => '2' , 'b' => '3' );
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
$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 )
2013-09-17 10:39:17 +01:00
-> 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 );
2011-05-13 17:29:18 +01:00
2013-09-17 10:39:17 +01:00
$this -> builder -> expects ( $this -> once ())
2012-07-12 18:17:14 +01:00
-> 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' );
2013-09-17 10:39:17 +01:00
$resolvedOptions = array ( 'a' => '2' , 'b' => '3' );
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 ( '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 )
2013-09-17 10:39:17 +01:00
-> will ( $this -> returnValue ( $this -> builder ));
2011-05-13 17:41:23 +01:00
2013-09-17 10:39:17 +01:00
$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 ())
2012-07-12 18:17:14 +01:00
-> 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' );
2013-09-17 10:39:17 +01:00
$resolvedOptions = array ( 'a' => '2' , 'b' => '3' );
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
$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 )
2013-09-17 10:39:17 +01:00
-> will ( $this -> returnValue ( $this -> builder ));
2011-05-13 17:29:18 +01:00
2013-09-17 10:39:17 +01:00
$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 ())
2012-07-12 18:17:14 +01:00
-> 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' ));
2013-09-17 10:39:17 +01:00
$this -> builder = $factory -> createBuilderForProperty ( 'Application\Author' , 'firstName' );
2012-11-08 18:49:46 +00:00
2013-09-17 10:39:17 +01:00
$this -> assertEquals ( 'builderInstance' , $this -> builder );
2012-11-08 18:49:46 +00:00
}
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' ,
2014-01-11 09:24:07 +00:00
array ( 'attr' => array ( 'maxlength' => 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' ,
2014-01-11 09:24:07 +00:00
array ( 'attr' => array ( 'maxlength' => 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' )
2014-01-11 09:24:07 +00:00
-> with ( 'firstName' , 'password' , null , array ( 'attr' => array ( 'maxlength' => 7 )))
2011-03-18 13:37:54 +00:00
-> will ( $this -> returnValue ( 'builderInstance' ));
2013-09-17 10:39:17 +01:00
$this -> builder = $factory -> createBuilderForProperty ( 'Application\Author' , 'firstName' );
2011-03-18 13:37:54 +00:00
2013-09-17 10:39:17 +01:00
$this -> assertEquals ( 'builderInstance' , $this -> builder );
2011-03-18 13:37:54 +00:00
}
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' ));
2013-09-17 10:39:17 +01:00
$this -> builder = $factory -> createBuilderForProperty ( 'Application\Author' , 'firstName' );
2011-03-18 13:37:54 +00:00
2013-09-17 10:39:17 +01:00
$this -> assertEquals ( 'builderInstance' , $this -> builder );
2011-03-18 13:37:54 +00:00
}
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' ,
2014-01-11 09:24:07 +00:00
array ( 'attr' => array ( 'maxlength' => 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' )
2014-01-11 09:24:07 +00:00
-> with ( 'firstName' , 'text' , null , array ( 'attr' => array ( 'maxlength' => 11 )))
2011-03-18 13:37:54 +00:00
-> will ( $this -> returnValue ( 'builderInstance' ));
2013-09-17 10:39:17 +01:00
$this -> builder = $factory -> createBuilderForProperty (
2011-03-18 13:37:54 +00:00
'Application\Author' ,
'firstName' ,
2011-04-22 09:42:21 +01:00
null ,
2014-01-11 09:24:07 +00:00
array ( 'attr' => array ( 'maxlength' => 11 ))
2011-03-18 13:37:54 +00:00
);
2013-09-17 10:39:17 +01:00
$this -> assertEquals ( 'builderInstance' , $this -> builder );
2011-03-18 13:37:54 +00:00
}
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' )
2014-01-11 09:24:07 +00:00
-> with ( 'firstName' , 'text' , null , array ( 'attr' => array ( 'maxlength' => 20 )))
2011-03-18 13:37:54 +00:00
-> will ( $this -> returnValue ( 'builderInstance' ));
2013-09-17 10:39:17 +01:00
$this -> builder = $factory -> createBuilderForProperty (
2011-03-18 13:37:54 +00:00
'Application\Author' ,
'firstName'
);
2013-09-17 10:39:17 +01:00
$this -> assertEquals ( 'builderInstance' , $this -> builder );
2012-04-23 14:55:54 +01:00
}
2014-11-23 03:56:17 +00:00
public function testCreateBuilderUsesMaxLengthAndPattern ()
{
$this -> guesser1 -> expects ( $this -> once ())
-> method ( 'guessMaxLength' )
-> with ( 'Application\Author' , 'firstName' )
-> will ( $this -> returnValue ( new ValueGuess (
20 ,
Guess :: HIGH_CONFIDENCE
)));
$this -> guesser2 -> expects ( $this -> once ())
-> method ( 'guessPattern' )
-> with ( 'Application\Author' , 'firstName' )
-> will ( $this -> returnValue ( new ValueGuess (
'.{5,}' ,
Guess :: HIGH_CONFIDENCE
)));
$factory = $this -> getMockFactory ( array ( 'createNamedBuilder' ));
$factory -> expects ( $this -> once ())
-> method ( 'createNamedBuilder' )
-> with ( 'firstName' , 'text' , null , array ( 'attr' => array ( 'maxlength' => 20 , 'pattern' => '.{5,}' , 'class' => 'tinymce' )))
-> will ( $this -> returnValue ( 'builderInstance' ));
$this -> builder = $factory -> createBuilderForProperty (
'Application\Author' ,
'firstName' ,
null ,
array ( 'attr' => array ( 'class' => 'tinymce' ))
);
$this -> assertEquals ( 'builderInstance' , $this -> 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' ));
2013-09-17 10:39:17 +01:00
$this -> builder = $factory -> createBuilderForProperty (
2011-03-18 13:37:54 +00:00
'Application\Author' ,
'firstName'
);
2013-09-17 10:39:17 +01:00
$this -> assertEquals ( 'builderInstance' , $this -> builder );
2011-03-18 13:37:54 +00:00
}
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' )
2014-01-11 09:24:07 +00:00
-> with ( 'firstName' , 'text' , null , array ( 'attr' => array ( 'pattern' => '[a-zA-Z]' )))
2012-04-13 16:07:09 +01:00
-> will ( $this -> returnValue ( 'builderInstance' ));
2013-09-17 10:39:17 +01:00
$this -> builder = $factory -> createBuilderForProperty (
2012-04-13 16:07:09 +01:00
'Application\Author' ,
'firstName'
);
2013-09-17 10:39:17 +01:00
$this -> assertEquals ( 'builderInstance' , $this -> builder );
2012-04-13 16:07:09 +01:00
}
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' );
}
2011-03-24 21:20:54 +00:00
}