[Form] Decoupled methods of ResolvedFormType so that they can be overridden individually by decorators
This commit is contained in:
parent
a994a5d410
commit
56d78eda56
@ -412,7 +412,16 @@ class Button implements \IteratorAggregate, FormInterface
|
|||||||
$parent = $this->parent->createView();
|
$parent = $this->parent->createView();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->config->getType()->createView($this, $parent);
|
|
||||||
|
$type = $this->config->getType();
|
||||||
|
$options = $this->config->getOptions();
|
||||||
|
|
||||||
|
$view = $type->createView($this, $parent);
|
||||||
|
|
||||||
|
$type->buildView($view, $this, $options);
|
||||||
|
$type->finishView($view, $this, $options);
|
||||||
|
|
||||||
|
return $view;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -969,7 +969,23 @@ class Form implements \IteratorAggregate, FormInterface
|
|||||||
$parent = $this->parent->createView();
|
$parent = $this->parent->createView();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->config->getType()->createView($this, $parent);
|
$type = $this->config->getType();
|
||||||
|
$options = $this->config->getOptions();
|
||||||
|
|
||||||
|
// The methods createView(), buildView() and finishView() are called
|
||||||
|
// explicitly here in order to be able to override either of them
|
||||||
|
// in a custom resolved form type.
|
||||||
|
$view = $type->createView($this, $parent);
|
||||||
|
|
||||||
|
$type->buildView($view, $this, $options);
|
||||||
|
|
||||||
|
foreach ($this->children as $name => $child) {
|
||||||
|
$view->children[$name] = $child->createView($view);
|
||||||
|
}
|
||||||
|
|
||||||
|
$type->finishView($view, $this, $options);
|
||||||
|
|
||||||
|
return $view;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -84,7 +84,13 @@ class FormFactory implements FormFactoryInterface
|
|||||||
throw new UnexpectedTypeException($type, 'string, Symfony\Component\Form\ResolvedFormTypeInterface or Symfony\Component\Form\FormTypeInterface');
|
throw new UnexpectedTypeException($type, 'string, Symfony\Component\Form\ResolvedFormTypeInterface or Symfony\Component\Form\FormTypeInterface');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $type->createBuilder($this, $name, $options);
|
$builder = $type->createBuilder($this, $name, $options);
|
||||||
|
|
||||||
|
// Explicitly call buildForm() in order to be able to override either
|
||||||
|
// createBuilder() or buildForm() in the resolved form type
|
||||||
|
$type->buildForm($builder, $builder->getOptions());
|
||||||
|
|
||||||
|
return $builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -114,8 +114,6 @@ class ResolvedFormType implements ResolvedFormTypeInterface
|
|||||||
$builder = $this->newBuilder($name, $dataClass, $factory, $options);
|
$builder = $this->newBuilder($name, $dataClass, $factory, $options);
|
||||||
$builder->setType($this);
|
$builder->setType($this);
|
||||||
|
|
||||||
$this->buildForm($builder, $options);
|
|
||||||
|
|
||||||
return $builder;
|
return $builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,28 +122,12 @@ class ResolvedFormType implements ResolvedFormTypeInterface
|
|||||||
*/
|
*/
|
||||||
public function createView(FormInterface $form, FormView $parent = null)
|
public function createView(FormInterface $form, FormView $parent = null)
|
||||||
{
|
{
|
||||||
$options = $form->getConfig()->getOptions();
|
return $this->newView($parent);
|
||||||
|
|
||||||
$view = $this->newView($parent);
|
|
||||||
|
|
||||||
$this->buildView($view, $form, $options);
|
|
||||||
|
|
||||||
foreach ($form as $name => $child) {
|
|
||||||
/* @var FormInterface $child */
|
|
||||||
$view->children[$name] = $child->createView($view);
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->finishView($view, $form, $options);
|
|
||||||
|
|
||||||
return $view;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configures a form builder for the type hierarchy.
|
* Configures a form builder for the type hierarchy.
|
||||||
*
|
*
|
||||||
* This method is protected in order to allow implementing classes
|
|
||||||
* to change or call it in re-implementations of {@link createBuilder()}.
|
|
||||||
*
|
|
||||||
* @param FormBuilderInterface $builder The builder to configure.
|
* @param FormBuilderInterface $builder The builder to configure.
|
||||||
* @param array $options The options used for the configuration.
|
* @param array $options The options used for the configuration.
|
||||||
*/
|
*/
|
||||||
@ -166,10 +148,7 @@ class ResolvedFormType implements ResolvedFormTypeInterface
|
|||||||
/**
|
/**
|
||||||
* Configures a form view for the type hierarchy.
|
* Configures a form view for the type hierarchy.
|
||||||
*
|
*
|
||||||
* This method is protected in order to allow implementing classes
|
* This method is called before the children of the view are built.
|
||||||
* to change or call it in re-implementations of {@link createView()}.
|
|
||||||
*
|
|
||||||
* It is called before the children of the view are built.
|
|
||||||
*
|
*
|
||||||
* @param FormView $view The form view to configure.
|
* @param FormView $view The form view to configure.
|
||||||
* @param FormInterface $form The form corresponding to the view.
|
* @param FormInterface $form The form corresponding to the view.
|
||||||
@ -192,10 +171,7 @@ class ResolvedFormType implements ResolvedFormTypeInterface
|
|||||||
/**
|
/**
|
||||||
* Finishes a form view for the type hierarchy.
|
* Finishes a form view for the type hierarchy.
|
||||||
*
|
*
|
||||||
* This method is protected in order to allow implementing classes
|
* This method is called after the children of the view have been built.
|
||||||
* to change or call it in re-implementations of {@link createView()}.
|
|
||||||
*
|
|
||||||
* It is called after the children of the view have been built.
|
|
||||||
*
|
*
|
||||||
* @param FormView $view The form view to configure.
|
* @param FormView $view The form view to configure.
|
||||||
* @param FormInterface $form The form corresponding to the view.
|
* @param FormInterface $form The form corresponding to the view.
|
||||||
@ -218,9 +194,6 @@ class ResolvedFormType implements ResolvedFormTypeInterface
|
|||||||
/**
|
/**
|
||||||
* Returns the configured options resolver used for this type.
|
* Returns the configured options resolver used for this type.
|
||||||
*
|
*
|
||||||
* This method is protected in order to allow implementing classes
|
|
||||||
* to change or call it in re-implementations of {@link createBuilder()}.
|
|
||||||
*
|
|
||||||
* @return \Symfony\Component\OptionsResolver\OptionsResolverInterface The options resolver.
|
* @return \Symfony\Component\OptionsResolver\OptionsResolverInterface The options resolver.
|
||||||
*/
|
*/
|
||||||
public function getOptionsResolver()
|
public function getOptionsResolver()
|
||||||
|
@ -60,9 +60,9 @@ abstract class AbstractFormTest extends \PHPUnit_Framework_TestCase
|
|||||||
*
|
*
|
||||||
* @return FormBuilder
|
* @return FormBuilder
|
||||||
*/
|
*/
|
||||||
protected function getBuilder($name = 'name', EventDispatcherInterface $dispatcher = null, $dataClass = null)
|
protected function getBuilder($name = 'name', EventDispatcherInterface $dispatcher = null, $dataClass = null, array $options = array())
|
||||||
{
|
{
|
||||||
return new FormBuilder($name, $dataClass, $dispatcher ?: $this->dispatcher, $this->factory);
|
return new FormBuilder($name, $dataClass, $dispatcher ?: $this->dispatcher, $this->factory, $options);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -15,6 +15,7 @@ use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
|
|||||||
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler;
|
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler;
|
||||||
use Symfony\Component\Form\FormError;
|
use Symfony\Component\Form\FormError;
|
||||||
use Symfony\Component\Form\Forms;
|
use Symfony\Component\Form\Forms;
|
||||||
|
use Symfony\Component\Form\FormView;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||||
use Symfony\Component\Form\Tests\Fixtures\FixedDataTransformer;
|
use Symfony\Component\Form\Tests\Fixtures\FixedDataTransformer;
|
||||||
@ -806,6 +807,65 @@ class CompoundFormTest extends AbstractFormTest
|
|||||||
$this->assertEquals("name:\n ERROR: Error!\nfoo:\n No errors\n", $parent->getErrorsAsString());
|
$this->assertEquals("name:\n ERROR: Error!\nfoo:\n No errors\n", $parent->getErrorsAsString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Basic cases are covered in SimpleFormTest
|
||||||
|
public function testCreateViewWithChildren()
|
||||||
|
{
|
||||||
|
$type = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
|
||||||
|
$options = array('a' => 'Foo', 'b' => 'Bar');
|
||||||
|
$field1 = $this->getMockForm('foo');
|
||||||
|
$field2 = $this->getMockForm('bar');
|
||||||
|
$view = new FormView();
|
||||||
|
$field1View = new FormView();
|
||||||
|
$field2View = new FormView();
|
||||||
|
|
||||||
|
$this->form = $this->getBuilder('form', null, null, $options)
|
||||||
|
->setCompound(true)
|
||||||
|
->setDataMapper($this->getDataMapper())
|
||||||
|
->setType($type)
|
||||||
|
->getForm();
|
||||||
|
$this->form->add($field1);
|
||||||
|
$this->form->add($field2);
|
||||||
|
|
||||||
|
$test = $this;
|
||||||
|
|
||||||
|
$assertChildViewsEqual = function (array $childViews) use ($test) {
|
||||||
|
return function (FormView $view) use ($test, $childViews) {
|
||||||
|
/* @var \PHPUnit_Framework_TestCase $test */
|
||||||
|
$test->assertSame($childViews, $view->children);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
// First create the view
|
||||||
|
$type->expects($this->once())
|
||||||
|
->method('createView')
|
||||||
|
->will($this->returnValue($view));
|
||||||
|
|
||||||
|
// Then build it for the form itself
|
||||||
|
$type->expects($this->once())
|
||||||
|
->method('buildView')
|
||||||
|
->with($view, $this->form, $options)
|
||||||
|
->will($this->returnCallback($assertChildViewsEqual(array())));
|
||||||
|
|
||||||
|
// Then add the first child form
|
||||||
|
$field1->expects($this->once())
|
||||||
|
->method('createView')
|
||||||
|
->will($this->returnValue($field1View));
|
||||||
|
|
||||||
|
// Then the second child form
|
||||||
|
$field2->expects($this->once())
|
||||||
|
->method('createView')
|
||||||
|
->will($this->returnValue($field2View));
|
||||||
|
|
||||||
|
// Again build the view for the form itself. This time the child views
|
||||||
|
// exist.
|
||||||
|
$type->expects($this->once())
|
||||||
|
->method('finishView')
|
||||||
|
->with($view, $this->form, $options)
|
||||||
|
->will($this->returnCallback($assertChildViewsEqual(array('foo' => $field1View, 'bar' => $field2View))));
|
||||||
|
|
||||||
|
$this->assertSame($view, $this->form->createView());
|
||||||
|
}
|
||||||
|
|
||||||
protected function createForm()
|
protected function createForm()
|
||||||
{
|
{
|
||||||
return $this->getBuilder()
|
return $this->getBuilder()
|
||||||
|
@ -46,6 +46,11 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
|
|||||||
*/
|
*/
|
||||||
private $resolvedTypeFactory;
|
private $resolvedTypeFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \PHPUnit_Framework_MockObject_MockObject
|
||||||
|
*/
|
||||||
|
private $builder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var FormFactory
|
* @var FormFactory
|
||||||
*/
|
*/
|
||||||
@ -57,6 +62,7 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->guesser1 = $this->getMock('Symfony\Component\Form\FormTypeGuesserInterface');
|
$this->guesser1 = $this->getMock('Symfony\Component\Form\FormTypeGuesserInterface');
|
||||||
$this->guesser2 = $this->getMock('Symfony\Component\Form\FormTypeGuesserInterface');
|
$this->guesser2 = $this->getMock('Symfony\Component\Form\FormTypeGuesserInterface');
|
||||||
$this->registry = $this->getMock('Symfony\Component\Form\FormRegistryInterface');
|
$this->registry = $this->getMock('Symfony\Component\Form\FormRegistryInterface');
|
||||||
|
$this->builder = $this->getMock('Symfony\Component\Form\Test\FormBuilderInterface');
|
||||||
$this->factory = new FormFactory($this->registry, $this->resolvedTypeFactory);
|
$this->factory = new FormFactory($this->registry, $this->resolvedTypeFactory);
|
||||||
|
|
||||||
$this->registry->expects($this->any())
|
$this->registry->expects($this->any())
|
||||||
@ -70,6 +76,7 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testCreateNamedBuilderWithTypeName()
|
public function testCreateNamedBuilderWithTypeName()
|
||||||
{
|
{
|
||||||
$options = array('a' => '1', 'b' => '2');
|
$options = array('a' => '1', 'b' => '2');
|
||||||
|
$resolvedOptions = array('a' => '2', 'b' => '3');
|
||||||
$resolvedType = $this->getMockResolvedType();
|
$resolvedType = $this->getMockResolvedType();
|
||||||
|
|
||||||
$this->registry->expects($this->once())
|
$this->registry->expects($this->once())
|
||||||
@ -80,14 +87,23 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
|
|||||||
$resolvedType->expects($this->once())
|
$resolvedType->expects($this->once())
|
||||||
->method('createBuilder')
|
->method('createBuilder')
|
||||||
->with($this->factory, 'name', $options)
|
->with($this->factory, 'name', $options)
|
||||||
->will($this->returnValue('BUILDER'));
|
->will($this->returnValue($this->builder));
|
||||||
|
|
||||||
$this->assertSame('BUILDER', $this->factory->createNamedBuilder('name', 'type', null, $options));
|
$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));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCreateNamedBuilderWithTypeInstance()
|
public function testCreateNamedBuilderWithTypeInstance()
|
||||||
{
|
{
|
||||||
$options = array('a' => '1', 'b' => '2');
|
$options = array('a' => '1', 'b' => '2');
|
||||||
|
$resolvedOptions = array('a' => '2', 'b' => '3');
|
||||||
$type = new FooType();
|
$type = new FooType();
|
||||||
$resolvedType = $this->getMockResolvedType();
|
$resolvedType = $this->getMockResolvedType();
|
||||||
|
|
||||||
@ -99,14 +115,23 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
|
|||||||
$resolvedType->expects($this->once())
|
$resolvedType->expects($this->once())
|
||||||
->method('createBuilder')
|
->method('createBuilder')
|
||||||
->with($this->factory, 'name', $options)
|
->with($this->factory, 'name', $options)
|
||||||
->will($this->returnValue('BUILDER'));
|
->will($this->returnValue($this->builder));
|
||||||
|
|
||||||
$this->assertSame('BUILDER', $this->factory->createNamedBuilder('name', $type, null, $options));
|
$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));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCreateNamedBuilderWithTypeInstanceWithParentType()
|
public function testCreateNamedBuilderWithTypeInstanceWithParentType()
|
||||||
{
|
{
|
||||||
$options = array('a' => '1', 'b' => '2');
|
$options = array('a' => '1', 'b' => '2');
|
||||||
|
$resolvedOptions = array('a' => '2', 'b' => '3');
|
||||||
$type = new FooSubType();
|
$type = new FooSubType();
|
||||||
$resolvedType = $this->getMockResolvedType();
|
$resolvedType = $this->getMockResolvedType();
|
||||||
$parentResolvedType = $this->getMockResolvedType();
|
$parentResolvedType = $this->getMockResolvedType();
|
||||||
@ -124,14 +149,23 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
|
|||||||
$resolvedType->expects($this->once())
|
$resolvedType->expects($this->once())
|
||||||
->method('createBuilder')
|
->method('createBuilder')
|
||||||
->with($this->factory, 'name', $options)
|
->with($this->factory, 'name', $options)
|
||||||
->will($this->returnValue('BUILDER'));
|
->will($this->returnValue($this->builder));
|
||||||
|
|
||||||
$this->assertSame('BUILDER', $this->factory->createNamedBuilder('name', $type, null, $options));
|
$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));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCreateNamedBuilderWithTypeInstanceWithParentTypeInstance()
|
public function testCreateNamedBuilderWithTypeInstanceWithParentTypeInstance()
|
||||||
{
|
{
|
||||||
$options = array('a' => '1', 'b' => '2');
|
$options = array('a' => '1', 'b' => '2');
|
||||||
|
$resolvedOptions = array('a' => '2', 'b' => '3');
|
||||||
$type = new FooSubTypeWithParentInstance();
|
$type = new FooSubTypeWithParentInstance();
|
||||||
$resolvedType = $this->getMockResolvedType();
|
$resolvedType = $this->getMockResolvedType();
|
||||||
$parentResolvedType = $this->getMockResolvedType();
|
$parentResolvedType = $this->getMockResolvedType();
|
||||||
@ -149,28 +183,46 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
|
|||||||
$resolvedType->expects($this->once())
|
$resolvedType->expects($this->once())
|
||||||
->method('createBuilder')
|
->method('createBuilder')
|
||||||
->with($this->factory, 'name', $options)
|
->with($this->factory, 'name', $options)
|
||||||
->will($this->returnValue('BUILDER'));
|
->will($this->returnValue($this->builder));
|
||||||
|
|
||||||
$this->assertSame('BUILDER', $this->factory->createNamedBuilder('name', $type, null, $options));
|
$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));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCreateNamedBuilderWithResolvedTypeInstance()
|
public function testCreateNamedBuilderWithResolvedTypeInstance()
|
||||||
{
|
{
|
||||||
$options = array('a' => '1', 'b' => '2');
|
$options = array('a' => '1', 'b' => '2');
|
||||||
|
$resolvedOptions = array('a' => '2', 'b' => '3');
|
||||||
$resolvedType = $this->getMockResolvedType();
|
$resolvedType = $this->getMockResolvedType();
|
||||||
|
|
||||||
$resolvedType->expects($this->once())
|
$resolvedType->expects($this->once())
|
||||||
->method('createBuilder')
|
->method('createBuilder')
|
||||||
->with($this->factory, 'name', $options)
|
->with($this->factory, 'name', $options)
|
||||||
->will($this->returnValue('BUILDER'));
|
->will($this->returnValue($this->builder));
|
||||||
|
|
||||||
$this->assertSame('BUILDER', $this->factory->createNamedBuilder('name', $resolvedType, null, $options));
|
$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', $resolvedType, null, $options));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCreateNamedBuilderFillsDataOption()
|
public function testCreateNamedBuilderFillsDataOption()
|
||||||
{
|
{
|
||||||
$givenOptions = array('a' => '1', 'b' => '2');
|
$givenOptions = array('a' => '1', 'b' => '2');
|
||||||
$expectedOptions = array_merge($givenOptions, array('data' => 'DATA'));
|
$expectedOptions = array_merge($givenOptions, array('data' => 'DATA'));
|
||||||
|
$resolvedOptions = array('a' => '2', 'b' => '3', 'data' => 'DATA');
|
||||||
$resolvedType = $this->getMockResolvedType();
|
$resolvedType = $this->getMockResolvedType();
|
||||||
|
|
||||||
$this->registry->expects($this->once())
|
$this->registry->expects($this->once())
|
||||||
@ -181,14 +233,23 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
|
|||||||
$resolvedType->expects($this->once())
|
$resolvedType->expects($this->once())
|
||||||
->method('createBuilder')
|
->method('createBuilder')
|
||||||
->with($this->factory, 'name', $expectedOptions)
|
->with($this->factory, 'name', $expectedOptions)
|
||||||
->will($this->returnValue('BUILDER'));
|
->will($this->returnValue($this->builder));
|
||||||
|
|
||||||
$this->assertSame('BUILDER', $this->factory->createNamedBuilder('name', 'type', 'DATA', $givenOptions));
|
$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));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCreateNamedBuilderDoesNotOverrideExistingDataOption()
|
public function testCreateNamedBuilderDoesNotOverrideExistingDataOption()
|
||||||
{
|
{
|
||||||
$options = array('a' => '1', 'b' => '2', 'data' => 'CUSTOM');
|
$options = array('a' => '1', 'b' => '2', 'data' => 'CUSTOM');
|
||||||
|
$resolvedOptions = array('a' => '2', 'b' => '3', 'data' => 'CUSTOM');
|
||||||
$resolvedType = $this->getMockResolvedType();
|
$resolvedType = $this->getMockResolvedType();
|
||||||
|
|
||||||
$this->registry->expects($this->once())
|
$this->registry->expects($this->once())
|
||||||
@ -199,9 +260,17 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
|
|||||||
$resolvedType->expects($this->once())
|
$resolvedType->expects($this->once())
|
||||||
->method('createBuilder')
|
->method('createBuilder')
|
||||||
->with($this->factory, 'name', $options)
|
->with($this->factory, 'name', $options)
|
||||||
->will($this->returnValue('BUILDER'));
|
->will($this->returnValue($this->builder));
|
||||||
|
|
||||||
$this->assertSame('BUILDER', $this->factory->createNamedBuilder('name', 'type', 'DATA', $options));
|
$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));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -216,8 +285,8 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testCreateUsesTypeNameIfTypeGivenAsString()
|
public function testCreateUsesTypeNameIfTypeGivenAsString()
|
||||||
{
|
{
|
||||||
$options = array('a' => '1', 'b' => '2');
|
$options = array('a' => '1', 'b' => '2');
|
||||||
|
$resolvedOptions = array('a' => '2', 'b' => '3');
|
||||||
$resolvedType = $this->getMockResolvedType();
|
$resolvedType = $this->getMockResolvedType();
|
||||||
$builder = $this->getMockFormBuilder();
|
|
||||||
|
|
||||||
$this->registry->expects($this->once())
|
$this->registry->expects($this->once())
|
||||||
->method('getType')
|
->method('getType')
|
||||||
@ -227,9 +296,17 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
|
|||||||
$resolvedType->expects($this->once())
|
$resolvedType->expects($this->once())
|
||||||
->method('createBuilder')
|
->method('createBuilder')
|
||||||
->with($this->factory, 'TYPE', $options)
|
->with($this->factory, 'TYPE', $options)
|
||||||
->will($this->returnValue($builder));
|
->will($this->returnValue($this->builder));
|
||||||
|
|
||||||
$builder->expects($this->once())
|
$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())
|
||||||
->method('getForm')
|
->method('getForm')
|
||||||
->will($this->returnValue('FORM'));
|
->will($this->returnValue('FORM'));
|
||||||
|
|
||||||
@ -239,8 +316,8 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testCreateUsesTypeNameIfTypeGivenAsObject()
|
public function testCreateUsesTypeNameIfTypeGivenAsObject()
|
||||||
{
|
{
|
||||||
$options = array('a' => '1', 'b' => '2');
|
$options = array('a' => '1', 'b' => '2');
|
||||||
|
$resolvedOptions = array('a' => '2', 'b' => '3');
|
||||||
$resolvedType = $this->getMockResolvedType();
|
$resolvedType = $this->getMockResolvedType();
|
||||||
$builder = $this->getMockFormBuilder();
|
|
||||||
|
|
||||||
$resolvedType->expects($this->once())
|
$resolvedType->expects($this->once())
|
||||||
->method('getName')
|
->method('getName')
|
||||||
@ -249,9 +326,17 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
|
|||||||
$resolvedType->expects($this->once())
|
$resolvedType->expects($this->once())
|
||||||
->method('createBuilder')
|
->method('createBuilder')
|
||||||
->with($this->factory, 'TYPE', $options)
|
->with($this->factory, 'TYPE', $options)
|
||||||
->will($this->returnValue($builder));
|
->will($this->returnValue($this->builder));
|
||||||
|
|
||||||
$builder->expects($this->once())
|
$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())
|
||||||
->method('getForm')
|
->method('getForm')
|
||||||
->will($this->returnValue('FORM'));
|
->will($this->returnValue('FORM'));
|
||||||
|
|
||||||
@ -261,8 +346,8 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testCreateNamed()
|
public function testCreateNamed()
|
||||||
{
|
{
|
||||||
$options = array('a' => '1', 'b' => '2');
|
$options = array('a' => '1', 'b' => '2');
|
||||||
|
$resolvedOptions = array('a' => '2', 'b' => '3');
|
||||||
$resolvedType = $this->getMockResolvedType();
|
$resolvedType = $this->getMockResolvedType();
|
||||||
$builder = $this->getMockFormBuilder();
|
|
||||||
|
|
||||||
$this->registry->expects($this->once())
|
$this->registry->expects($this->once())
|
||||||
->method('getType')
|
->method('getType')
|
||||||
@ -272,9 +357,17 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
|
|||||||
$resolvedType->expects($this->once())
|
$resolvedType->expects($this->once())
|
||||||
->method('createBuilder')
|
->method('createBuilder')
|
||||||
->with($this->factory, 'name', $options)
|
->with($this->factory, 'name', $options)
|
||||||
->will($this->returnValue($builder));
|
->will($this->returnValue($this->builder));
|
||||||
|
|
||||||
$builder->expects($this->once())
|
$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())
|
||||||
->method('getForm')
|
->method('getForm')
|
||||||
->will($this->returnValue('FORM'));
|
->will($this->returnValue('FORM'));
|
||||||
|
|
||||||
@ -294,9 +387,9 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
|
|||||||
->with('firstName', 'text', null, array())
|
->with('firstName', 'text', null, array())
|
||||||
->will($this->returnValue('builderInstance'));
|
->will($this->returnValue('builderInstance'));
|
||||||
|
|
||||||
$builder = $factory->createBuilderForProperty('Application\Author', 'firstName');
|
$this->builder = $factory->createBuilderForProperty('Application\Author', 'firstName');
|
||||||
|
|
||||||
$this->assertEquals('builderInstance', $builder);
|
$this->assertEquals('builderInstance', $this->builder);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCreateBuilderForPropertyCreatesFormWithHighestConfidence()
|
public function testCreateBuilderForPropertyCreatesFormWithHighestConfidence()
|
||||||
@ -326,9 +419,9 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
|
|||||||
->with('firstName', 'password', null, array('max_length' => 7))
|
->with('firstName', 'password', null, array('max_length' => 7))
|
||||||
->will($this->returnValue('builderInstance'));
|
->will($this->returnValue('builderInstance'));
|
||||||
|
|
||||||
$builder = $factory->createBuilderForProperty('Application\Author', 'firstName');
|
$this->builder = $factory->createBuilderForProperty('Application\Author', 'firstName');
|
||||||
|
|
||||||
$this->assertEquals('builderInstance', $builder);
|
$this->assertEquals('builderInstance', $this->builder);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCreateBuilderCreatesTextFormIfNoGuess()
|
public function testCreateBuilderCreatesTextFormIfNoGuess()
|
||||||
@ -345,9 +438,9 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
|
|||||||
->with('firstName', 'text')
|
->with('firstName', 'text')
|
||||||
->will($this->returnValue('builderInstance'));
|
->will($this->returnValue('builderInstance'));
|
||||||
|
|
||||||
$builder = $factory->createBuilderForProperty('Application\Author', 'firstName');
|
$this->builder = $factory->createBuilderForProperty('Application\Author', 'firstName');
|
||||||
|
|
||||||
$this->assertEquals('builderInstance', $builder);
|
$this->assertEquals('builderInstance', $this->builder);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testOptionsCanBeOverridden()
|
public function testOptionsCanBeOverridden()
|
||||||
@ -368,14 +461,14 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
|
|||||||
->with('firstName', 'text', null, array('max_length' => 11))
|
->with('firstName', 'text', null, array('max_length' => 11))
|
||||||
->will($this->returnValue('builderInstance'));
|
->will($this->returnValue('builderInstance'));
|
||||||
|
|
||||||
$builder = $factory->createBuilderForProperty(
|
$this->builder = $factory->createBuilderForProperty(
|
||||||
'Application\Author',
|
'Application\Author',
|
||||||
'firstName',
|
'firstName',
|
||||||
null,
|
null,
|
||||||
array('max_length' => 11)
|
array('max_length' => 11)
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals('builderInstance', $builder);
|
$this->assertEquals('builderInstance', $this->builder);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCreateBuilderUsesMaxLengthIfFound()
|
public function testCreateBuilderUsesMaxLengthIfFound()
|
||||||
@ -403,12 +496,12 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
|
|||||||
->with('firstName', 'text', null, array('max_length' => 20))
|
->with('firstName', 'text', null, array('max_length' => 20))
|
||||||
->will($this->returnValue('builderInstance'));
|
->will($this->returnValue('builderInstance'));
|
||||||
|
|
||||||
$builder = $factory->createBuilderForProperty(
|
$this->builder = $factory->createBuilderForProperty(
|
||||||
'Application\Author',
|
'Application\Author',
|
||||||
'firstName'
|
'firstName'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals('builderInstance', $builder);
|
$this->assertEquals('builderInstance', $this->builder);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCreateBuilderUsesRequiredSettingWithHighestConfidence()
|
public function testCreateBuilderUsesRequiredSettingWithHighestConfidence()
|
||||||
@ -436,12 +529,12 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
|
|||||||
->with('firstName', 'text', null, array('required' => false))
|
->with('firstName', 'text', null, array('required' => false))
|
||||||
->will($this->returnValue('builderInstance'));
|
->will($this->returnValue('builderInstance'));
|
||||||
|
|
||||||
$builder = $factory->createBuilderForProperty(
|
$this->builder = $factory->createBuilderForProperty(
|
||||||
'Application\Author',
|
'Application\Author',
|
||||||
'firstName'
|
'firstName'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals('builderInstance', $builder);
|
$this->assertEquals('builderInstance', $this->builder);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCreateBuilderUsesPatternIfFound()
|
public function testCreateBuilderUsesPatternIfFound()
|
||||||
@ -469,12 +562,12 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
|
|||||||
->with('firstName', 'text', null, array('pattern' => '[a-zA-Z]'))
|
->with('firstName', 'text', null, array('pattern' => '[a-zA-Z]'))
|
||||||
->will($this->returnValue('builderInstance'));
|
->will($this->returnValue('builderInstance'));
|
||||||
|
|
||||||
$builder = $factory->createBuilderForProperty(
|
$this->builder = $factory->createBuilderForProperty(
|
||||||
'Application\Author',
|
'Application\Author',
|
||||||
'firstName'
|
'firstName'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals('builderInstance', $builder);
|
$this->assertEquals('builderInstance', $this->builder);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getMockFactory(array $methods = array())
|
private function getMockFactory(array $methods = array())
|
||||||
@ -494,9 +587,4 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
return $this->getMock('Symfony\Component\Form\FormTypeInterface');
|
return $this->getMock('Symfony\Component\Form\FormTypeInterface');
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getMockFormBuilder()
|
|
||||||
{
|
|
||||||
return $this->getMock('Symfony\Component\Form\Test\FormBuilderInterface');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -36,12 +36,71 @@ class ResolvedFormTypeTest extends \PHPUnit_Framework_TestCase
|
|||||||
* @var \PHPUnit_Framework_MockObject_MockObject
|
* @var \PHPUnit_Framework_MockObject_MockObject
|
||||||
*/
|
*/
|
||||||
private $dataMapper;
|
private $dataMapper;
|
||||||
|
private $parentType;
|
||||||
|
private $type;
|
||||||
|
private $extension1;
|
||||||
|
private $extension2;
|
||||||
|
private $parentResolvedType;
|
||||||
|
private $resolvedType;
|
||||||
|
|
||||||
protected function setUp()
|
protected function setUp()
|
||||||
{
|
{
|
||||||
$this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
|
$this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
|
||||||
$this->factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
|
$this->factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
|
||||||
$this->dataMapper = $this->getMock('Symfony\Component\Form\DataMapperInterface');
|
$this->dataMapper = $this->getMock('Symfony\Component\Form\DataMapperInterface');
|
||||||
|
$this->parentType = $this->getMockFormType();
|
||||||
|
$this->type = $this->getMockFormType();
|
||||||
|
$this->extension1 = $this->getMockFormTypeExtension();
|
||||||
|
$this->extension2 = $this->getMockFormTypeExtension();
|
||||||
|
$this->parentResolvedType = new ResolvedFormType($this->parentType);
|
||||||
|
$this->resolvedType = new ResolvedFormType($this->type, array($this->extension1, $this->extension2), $this->parentResolvedType);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetOptionsResolver()
|
||||||
|
{
|
||||||
|
if (version_compare(\PHPUnit_Runner_Version::id(), '3.7', '<')) {
|
||||||
|
$this->markTestSkipped('This test requires PHPUnit 3.7.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$test = $this;
|
||||||
|
$i = 0;
|
||||||
|
|
||||||
|
$assertIndexAndAddOption = function ($index, $option, $default) use (&$i, $test) {
|
||||||
|
return function (OptionsResolverInterface $resolver) use (&$i, $test, $index, $option, $default) {
|
||||||
|
/* @var \PHPUnit_Framework_TestCase $test */
|
||||||
|
$test->assertEquals($index, $i, 'Executed at index '.$index);
|
||||||
|
|
||||||
|
++$i;
|
||||||
|
|
||||||
|
$resolver->setDefaults(array($option => $default));
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
// First the default options are generated for the super type
|
||||||
|
$this->parentType->expects($this->once())
|
||||||
|
->method('setDefaultOptions')
|
||||||
|
->will($this->returnCallback($assertIndexAndAddOption(0, 'a', 'a_default')));
|
||||||
|
|
||||||
|
// The form type itself
|
||||||
|
$this->type->expects($this->once())
|
||||||
|
->method('setDefaultOptions')
|
||||||
|
->will($this->returnCallback($assertIndexAndAddOption(1, 'b', 'b_default')));
|
||||||
|
|
||||||
|
// And its extensions
|
||||||
|
$this->extension1->expects($this->once())
|
||||||
|
->method('setDefaultOptions')
|
||||||
|
->will($this->returnCallback($assertIndexAndAddOption(2, 'c', 'c_default')));
|
||||||
|
|
||||||
|
$this->extension2->expects($this->once())
|
||||||
|
->method('setDefaultOptions')
|
||||||
|
->will($this->returnCallback($assertIndexAndAddOption(3, 'd', 'd_default')));
|
||||||
|
|
||||||
|
$givenOptions = array('a' => 'a_custom', 'c' => 'c_custom');
|
||||||
|
$resolvedOptions = array('a' => 'a_custom', 'b' => 'b_default', 'c' => 'c_custom', 'd' => 'd_default');
|
||||||
|
|
||||||
|
$resolver = $this->resolvedType->getOptionsResolver();
|
||||||
|
|
||||||
|
$this->assertEquals($resolvedOptions, $resolver->resolve($givenOptions));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCreateBuilder()
|
public function testCreateBuilder()
|
||||||
@ -50,13 +109,70 @@ class ResolvedFormTypeTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->markTestSkipped('This test requires PHPUnit 3.7.');
|
$this->markTestSkipped('This test requires PHPUnit 3.7.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$parentType = $this->getMockFormType();
|
$givenOptions = array('a' => 'a_custom', 'c' => 'c_custom');
|
||||||
$type = $this->getMockFormType();
|
$resolvedOptions = array('a' => 'a_custom', 'b' => 'b_default', 'c' => 'c_custom', 'd' => 'd_default');
|
||||||
$extension1 = $this->getMockFormTypeExtension();
|
$optionsResolver = $this->getMock('Symfony\Component\OptionsResolver\OptionsResolverInterface');
|
||||||
$extension2 = $this->getMockFormTypeExtension();
|
|
||||||
|
|
||||||
$parentResolvedType = new ResolvedFormType($parentType);
|
$this->resolvedType = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormType')
|
||||||
$resolvedType = new ResolvedFormType($type, array($extension1, $extension2), $parentResolvedType);
|
->setConstructorArgs(array($this->type, array($this->extension1, $this->extension2), $this->parentResolvedType))
|
||||||
|
->setMethods(array('getOptionsResolver'))
|
||||||
|
->getMock();
|
||||||
|
|
||||||
|
$this->resolvedType->expects($this->once())
|
||||||
|
->method('getOptionsResolver')
|
||||||
|
->will($this->returnValue($optionsResolver));
|
||||||
|
|
||||||
|
$optionsResolver->expects($this->once())
|
||||||
|
->method('resolve')
|
||||||
|
->with($givenOptions)
|
||||||
|
->will($this->returnValue($resolvedOptions));
|
||||||
|
|
||||||
|
$factory = $this->getMockFormFactory();
|
||||||
|
$builder = $this->resolvedType->createBuilder($factory, 'name', $givenOptions);
|
||||||
|
|
||||||
|
$this->assertSame($this->resolvedType, $builder->getType());
|
||||||
|
$this->assertSame($resolvedOptions, $builder->getOptions());
|
||||||
|
$this->assertNull($builder->getDataClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCreateBuilderWithDataClassOption()
|
||||||
|
{
|
||||||
|
if (version_compare(\PHPUnit_Runner_Version::id(), '3.7', '<')) {
|
||||||
|
$this->markTestSkipped('This test requires PHPUnit 3.7.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$givenOptions = array('data_class' => 'Foo');
|
||||||
|
$resolvedOptions = array('data_class' => '\stdClass');
|
||||||
|
$optionsResolver = $this->getMock('Symfony\Component\OptionsResolver\OptionsResolverInterface');
|
||||||
|
|
||||||
|
$this->resolvedType = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormType')
|
||||||
|
->setConstructorArgs(array($this->type, array($this->extension1, $this->extension2), $this->parentResolvedType))
|
||||||
|
->setMethods(array('getOptionsResolver'))
|
||||||
|
->getMock();
|
||||||
|
|
||||||
|
$this->resolvedType->expects($this->once())
|
||||||
|
->method('getOptionsResolver')
|
||||||
|
->will($this->returnValue($optionsResolver));
|
||||||
|
|
||||||
|
$optionsResolver->expects($this->once())
|
||||||
|
->method('resolve')
|
||||||
|
->with($givenOptions)
|
||||||
|
->will($this->returnValue($resolvedOptions));
|
||||||
|
|
||||||
|
$factory = $this->getMockFormFactory();
|
||||||
|
$builder = $this->resolvedType->createBuilder($factory, 'name', $givenOptions);
|
||||||
|
|
||||||
|
$this->assertSame($this->resolvedType, $builder->getType());
|
||||||
|
$this->assertSame($resolvedOptions, $builder->getOptions());
|
||||||
|
$this->assertSame('\stdClass', $builder->getDataClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function testBuildForm()
|
||||||
|
{
|
||||||
|
if (version_compare(\PHPUnit_Runner_Version::id(), '3.7', '<')) {
|
||||||
|
$this->markTestSkipped('This test requires PHPUnit 3.7.');
|
||||||
|
}
|
||||||
|
|
||||||
$test = $this;
|
$test = $this;
|
||||||
$i = 0;
|
$i = 0;
|
||||||
@ -70,169 +186,142 @@ class ResolvedFormTypeTest extends \PHPUnit_Framework_TestCase
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
$assertIndexAndAddOption = function ($index, $option, $default) use ($assertIndex) {
|
$options = array('a' => 'Foo', 'b' => 'Bar');
|
||||||
$assertIndex = $assertIndex($index);
|
$builder = $this->getMock('Symfony\Component\Form\Test\FormBuilderInterface');
|
||||||
|
|
||||||
return function (OptionsResolverInterface $resolver) use ($assertIndex, $index, $option, $default) {
|
// First the form is built for the super type
|
||||||
$assertIndex();
|
$this->parentType->expects($this->once())
|
||||||
|
|
||||||
$resolver->setDefaults(array($option => $default));
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
// First the default options are generated for the super type
|
|
||||||
$parentType->expects($this->once())
|
|
||||||
->method('setDefaultOptions')
|
|
||||||
->will($this->returnCallback($assertIndexAndAddOption(0, 'a', 'a_default')));
|
|
||||||
|
|
||||||
// The form type itself
|
|
||||||
$type->expects($this->once())
|
|
||||||
->method('setDefaultOptions')
|
|
||||||
->will($this->returnCallback($assertIndexAndAddOption(1, 'b', 'b_default')));
|
|
||||||
|
|
||||||
// And its extensions
|
|
||||||
$extension1->expects($this->once())
|
|
||||||
->method('setDefaultOptions')
|
|
||||||
->will($this->returnCallback($assertIndexAndAddOption(2, 'c', 'c_default')));
|
|
||||||
|
|
||||||
$extension2->expects($this->once())
|
|
||||||
->method('setDefaultOptions')
|
|
||||||
->will($this->returnCallback($assertIndexAndAddOption(3, 'd', 'd_default')));
|
|
||||||
|
|
||||||
$givenOptions = array('a' => 'a_custom', 'c' => 'c_custom');
|
|
||||||
$resolvedOptions = array('a' => 'a_custom', 'b' => 'b_default', 'c' => 'c_custom', 'd' => 'd_default');
|
|
||||||
|
|
||||||
// Then the form is built for the super type
|
|
||||||
$parentType->expects($this->once())
|
|
||||||
->method('buildForm')
|
->method('buildForm')
|
||||||
->with($this->anything(), $resolvedOptions)
|
->with($builder, $options)
|
||||||
->will($this->returnCallback($assertIndex(4)));
|
->will($this->returnCallback($assertIndex(0)));
|
||||||
|
|
||||||
// Then the type itself
|
// Then the type itself
|
||||||
$type->expects($this->once())
|
$this->type->expects($this->once())
|
||||||
->method('buildForm')
|
->method('buildForm')
|
||||||
->with($this->anything(), $resolvedOptions)
|
->with($builder, $options)
|
||||||
->will($this->returnCallback($assertIndex(5)));
|
->will($this->returnCallback($assertIndex(1)));
|
||||||
|
|
||||||
// Then its extensions
|
// Then its extensions
|
||||||
$extension1->expects($this->once())
|
$this->extension1->expects($this->once())
|
||||||
->method('buildForm')
|
->method('buildForm')
|
||||||
->with($this->anything(), $resolvedOptions)
|
->with($builder, $options)
|
||||||
->will($this->returnCallback($assertIndex(6)));
|
->will($this->returnCallback($assertIndex(2)));
|
||||||
|
|
||||||
$extension2->expects($this->once())
|
$this->extension2->expects($this->once())
|
||||||
->method('buildForm')
|
->method('buildForm')
|
||||||
->with($this->anything(), $resolvedOptions)
|
->with($builder, $options)
|
||||||
->will($this->returnCallback($assertIndex(7)));
|
->will($this->returnCallback($assertIndex(3)));
|
||||||
|
|
||||||
$factory = $this->getMockFormFactory();
|
$this->resolvedType->buildForm($builder, $options);
|
||||||
$builder = $resolvedType->createBuilder($factory, 'name', $givenOptions);
|
|
||||||
|
|
||||||
$this->assertSame($resolvedType, $builder->getType());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCreateView()
|
public function testCreateView()
|
||||||
{
|
{
|
||||||
$parentType = $this->getMockFormType();
|
$form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
|
||||||
$type = $this->getMockFormType();
|
|
||||||
$field1Type = $this->getMockFormType();
|
|
||||||
$field2Type = $this->getMockFormType();
|
|
||||||
$extension1 = $this->getMockFormTypeExtension();
|
|
||||||
$extension2 = $this->getMockFormTypeExtension();
|
|
||||||
|
|
||||||
$parentResolvedType = new ResolvedFormType($parentType);
|
$view = $this->resolvedType->createView($form);
|
||||||
$resolvedType = new ResolvedFormType($type, array($extension1, $extension2), $parentResolvedType);
|
|
||||||
$field1ResolvedType = new ResolvedFormType($field1Type);
|
|
||||||
$field2ResolvedType = new ResolvedFormType($field2Type);
|
|
||||||
|
|
||||||
|
$this->assertInstanceOf('Symfony\Component\Form\FormView', $view);
|
||||||
|
$this->assertNull($view->parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCreateViewWithParent()
|
||||||
|
{
|
||||||
|
$form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
|
||||||
|
$parentView = $this->getMock('Symfony\Component\Form\FormView');
|
||||||
|
|
||||||
|
$view = $this->resolvedType->createView($form, $parentView);
|
||||||
|
|
||||||
|
$this->assertInstanceOf('Symfony\Component\Form\FormView', $view);
|
||||||
|
$this->assertSame($parentView, $view->parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testBuildView()
|
||||||
|
{
|
||||||
$options = array('a' => '1', 'b' => '2');
|
$options = array('a' => '1', 'b' => '2');
|
||||||
$form = $this->getBuilder('name', $options)
|
$form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
|
||||||
->setCompound(true)
|
$view = $this->getMock('Symfony\Component\Form\FormView');
|
||||||
->setDataMapper($this->dataMapper)
|
|
||||||
->setType($resolvedType)
|
|
||||||
->add($this->getBuilder('foo')->setType($field1ResolvedType))
|
|
||||||
->add($this->getBuilder('bar')->setType($field2ResolvedType))
|
|
||||||
->getForm();
|
|
||||||
|
|
||||||
$test = $this;
|
$test = $this;
|
||||||
$i = 0;
|
$i = 0;
|
||||||
|
|
||||||
$assertIndexAndNbOfChildViews = function ($index, $nbOfChildViews) use (&$i, $test) {
|
$assertIndex = function ($index) use (&$i, $test) {
|
||||||
return function (FormView $view) use (&$i, $test, $index, $nbOfChildViews) {
|
return function () use (&$i, $test, $index) {
|
||||||
/* @var \PHPUnit_Framework_TestCase $test */
|
/* @var \PHPUnit_Framework_TestCase $test */
|
||||||
$test->assertEquals($index, $i, 'Executed at index '.$index);
|
$test->assertEquals($index, $i, 'Executed at index '.$index);
|
||||||
$test->assertCount($nbOfChildViews, $view);
|
|
||||||
|
|
||||||
++$i;
|
++$i;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
// First the super type
|
// First the super type
|
||||||
$parentType->expects($this->once())
|
$this->parentType->expects($this->once())
|
||||||
->method('buildView')
|
->method('buildView')
|
||||||
->with($this->anything(), $form, $options)
|
->with($view, $form, $options)
|
||||||
->will($this->returnCallback($assertIndexAndNbOfChildViews(0, 0)));
|
->will($this->returnCallback($assertIndex(0)));
|
||||||
|
|
||||||
// Then the type itself
|
// Then the type itself
|
||||||
$type->expects($this->once())
|
$this->type->expects($this->once())
|
||||||
->method('buildView')
|
->method('buildView')
|
||||||
->with($this->anything(), $form, $options)
|
->with($view, $form, $options)
|
||||||
->will($this->returnCallback($assertIndexAndNbOfChildViews(1, 0)));
|
->will($this->returnCallback($assertIndex(1)));
|
||||||
|
|
||||||
// Then its extensions
|
// Then its extensions
|
||||||
$extension1->expects($this->once())
|
$this->extension1->expects($this->once())
|
||||||
->method('buildView')
|
->method('buildView')
|
||||||
->with($this->anything(), $form, $options)
|
->with($view, $form, $options)
|
||||||
->will($this->returnCallback($assertIndexAndNbOfChildViews(2, 0)));
|
->will($this->returnCallback($assertIndex(2)));
|
||||||
|
|
||||||
$extension2->expects($this->once())
|
$this->extension2->expects($this->once())
|
||||||
->method('buildView')
|
->method('buildView')
|
||||||
->with($this->anything(), $form, $options)
|
->with($view, $form, $options)
|
||||||
->will($this->returnCallback($assertIndexAndNbOfChildViews(3, 0)));
|
->will($this->returnCallback($assertIndex(3)));
|
||||||
|
|
||||||
// Now the first child form
|
$this->resolvedType->buildView($view, $form, $options);
|
||||||
$field1Type->expects($this->once())
|
}
|
||||||
->method('buildView')
|
|
||||||
->will($this->returnCallback($assertIndexAndNbOfChildViews(4, 0)));
|
public function testFinishView()
|
||||||
$field1Type->expects($this->once())
|
{
|
||||||
|
$options = array('a' => '1', 'b' => '2');
|
||||||
|
$form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
|
||||||
|
$view = $this->getMock('Symfony\Component\Form\FormView');
|
||||||
|
|
||||||
|
$test = $this;
|
||||||
|
$i = 0;
|
||||||
|
|
||||||
|
$assertIndex = function ($index) use (&$i, $test) {
|
||||||
|
return function () use (&$i, $test, $index) {
|
||||||
|
/* @var \PHPUnit_Framework_TestCase $test */
|
||||||
|
$test->assertEquals($index, $i, 'Executed at index '.$index);
|
||||||
|
|
||||||
|
++$i;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
// First the super type
|
||||||
|
$this->parentType->expects($this->once())
|
||||||
->method('finishView')
|
->method('finishView')
|
||||||
->will($this->returnCallback($assertIndexAndNbOfChildViews(5, 0)));
|
->with($view, $form, $options)
|
||||||
|
->will($this->returnCallback($assertIndex(0)));
|
||||||
// And the second child form
|
|
||||||
$field2Type->expects($this->once())
|
|
||||||
->method('buildView')
|
|
||||||
->will($this->returnCallback($assertIndexAndNbOfChildViews(6, 0)));
|
|
||||||
$field2Type->expects($this->once())
|
|
||||||
->method('finishView')
|
|
||||||
->will($this->returnCallback($assertIndexAndNbOfChildViews(7, 0)));
|
|
||||||
|
|
||||||
// Again first the parent
|
|
||||||
$parentType->expects($this->once())
|
|
||||||
->method('finishView')
|
|
||||||
->with($this->anything(), $form, $options)
|
|
||||||
->will($this->returnCallback($assertIndexAndNbOfChildViews(8, 2)));
|
|
||||||
|
|
||||||
// Then the type itself
|
// Then the type itself
|
||||||
$type->expects($this->once())
|
$this->type->expects($this->once())
|
||||||
->method('finishView')
|
->method('finishView')
|
||||||
->with($this->anything(), $form, $options)
|
->with($view, $form, $options)
|
||||||
->will($this->returnCallback($assertIndexAndNbOfChildViews(9, 2)));
|
->will($this->returnCallback($assertIndex(1)));
|
||||||
|
|
||||||
// Then its extensions
|
// Then its extensions
|
||||||
$extension1->expects($this->once())
|
$this->extension1->expects($this->once())
|
||||||
->method('finishView')
|
->method('finishView')
|
||||||
->with($this->anything(), $form, $options)
|
->with($view, $form, $options)
|
||||||
->will($this->returnCallback($assertIndexAndNbOfChildViews(10, 2)));
|
->will($this->returnCallback($assertIndex(2)));
|
||||||
|
|
||||||
$extension2->expects($this->once())
|
$this->extension2->expects($this->once())
|
||||||
->method('finishView')
|
->method('finishView')
|
||||||
->with($this->anything(), $form, $options)
|
->with($view, $form, $options)
|
||||||
->will($this->returnCallback($assertIndexAndNbOfChildViews(11, 2)));
|
->will($this->returnCallback($assertIndex(3)));
|
||||||
|
|
||||||
$parentView = new FormView();
|
$this->resolvedType->finishView($view, $form, $options);
|
||||||
$view = $resolvedType->createView($form, $parentView);
|
|
||||||
|
|
||||||
$this->assertSame($parentView, $view->parent);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user