8000 [Form] Passing FormFactory to FormBuilder in its constructor · ivanrey/symfony@e3db366 · GitHub
[go: up one dir, main page]

Skip to content

Commit e3db366

Browse files
committed
[Form] Passing FormFactory to FormBuilder in its constructor
1 parent 2ddc85a commit e3db366

File tree

9 files changed

+27
-27
lines changed

9 files changed

+27
-27
lines changed

src/Symfony/Component/Form/FormBuilder.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,14 @@ class FormBuilder
5656

5757
private $emptyData = '';
5858

59-
public function __construct($name, EventDispatcherInterface $dispatcher, $dataClass = null)
59+
public function __construct($name, FormFactoryInterface $factory, EventDispatcherInterface $dispatcher, $dataClass = null)
6060
{
6161
$this->name = $name;
62+
$this->factory = $factory;
6263
$this->dispatcher = $dispatcher;
6364
$this->dataClass = $dataClass;
6465
}
6566

66-
public function setFormFactory(FormFactoryInterface $factory)
67-
{
68-
$this->factory = $factory;
69-
70-
return $this;
71-
}
72-
7367
public function getFormFactory()
7468
{
7569
return $this->factory;

src/Symfony/Component/Form/FormFactory.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,12 @@ public function createBuilder($type, $name = null, array $options = array())
7373
}
7474

7575
for ($i = 0, $l = count($types); $i < $l && !$builder; ++$i) {
76-
$builder = $types[$i]->createBuilder($name, $options);
76+
$builder = $types[$i]->createBuilder($name, $this, $options);
7777
}
7878

7979
// TODO check if instance exists
8080

8181
$builder->setTypes($types);
82-
$builder->setFormFactory($this);
8382

8483
foreach ($types as $type) {
8584
$type->buildForm($builder, $options);

src/Symfony/Component/Form/Type/AbstractType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
namespace Symfony\Component\Form\Type;
1313

1414
use Symfony\Component\Form\FormBuilder;
15-
use Symfony\Component\Form\FormFactoryInterface;
1615
use Symfony\Component\Form\FormInterface;
16+
use Symfony\Component\Form\FormFactoryInterface;
1717
use Symfony\Component\Form\FormView;
1818

1919
abstract class AbstractType implements FormTypeInterface
@@ -30,7 +30,7 @@ public function buildViewBottomUp(FormView $view, FormInterface $form)
3030
{
3131
}
3232

33-
public function createBuilder($name, array $options)
33+
public function createBuilder($name, FormFactoryInterface $factory, array $options)
3434
{
3535
return null;
3636
}

src/Symfony/Component/Form/Type/FieldType.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Form\Util\PropertyPath;
1515
use Symfony\Component\Form\FormBuilder;
1616
use Symfony\Component\Form\FormInterface;
17+
use Symfony\Component\Form\FormFactoryInterface;
1718
use Symfony\Component\Form\FormView;
1819
use Symfony\Component\Form\EventListener\TrimListener;
1920
use Symfony\Component\Form\Validator\DefaultValidator;
@@ -124,9 +125,9 @@ public function getDefaultOptions(array $options)
124125
return $defaultOptions;
125126
}
126127

127-
public function createBuilder($name, array $options)
128+
public function createBuilder($name, FormFactoryInterface $factory, array $options)
128129
{
129-
return new FormBuilder($name, new EventDispatcher(), $options['data_class']);
130+
return new FormBuilder($name, $factory, new EventDispatcher(), $options['data_class']);
130131
}
131132

132133
public function getParent(array $options)

src/Symfony/Component/Form/Type/FormTypeInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Form\FormBuilder;
1515
use Symfony\Component\Form\FormInterface;
16+
use Symfony\Component\Form\FormFactoryInterface;
1617
use Symfony\Component\Form\FormView;
1718

1819
interface FormTypeInterface
@@ -23,7 +24,7 @@ function buildView(FormView $view, FormInterface $form);
2324

2425
function buildViewBottomUp(FormView $view, FormInterface $form);
2526

26-
function createBuilder($name, array $options);
27+
function createBuilder($name, FormFactoryInterface $factory, array $options);
2728

2829
function getDefaultOptions(array $options);
2930

tests/Symfony/Tests/Component/Form/FormBuilderTest.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@ class FormBuilderTest extends \PHPUnit_Framework_TestCase
2121
{
2222
private $dispatcher;
2323

24+
private $factory;
25+
2426
private $builder;
2527

2628
public function setUp()
2729
{
2830
$this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
29-
$this->builder = new FormBuilder('name', $this->dispatcher);
31+
$this->factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
32+
$this->builder = new FormBuilder('name', $this->factory, $this->dispatcher);
3033
}
3134

3235
/**
@@ -54,7 +57,7 @@ public function testAddTypeNoString()
5457

5558
public function testAddWithGuessFluent()
5659
{
57-
$this->builder = new FormBuilder('name', $this->dispatcher, 'stdClass');
60+
$this->builder = new FormBuilder('name', $this->factory, $this->dispatcher, 'stdClass');
5861
$builder = $this->builder->add('foo');
5962
$this->assertSame($builder, $this->builder);
6063
}
@@ -110,12 +113,10 @@ public function testGetTyped()
110113
$expectedName = 'foo';
111114
$expectedOptions = array('bar' => 'baz');
112115

113-
$factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
114-
$factory->expects($this->once())
116+
$this->factory->expects($this->once())
115117
->method('createBuilder')
116118
->with($this->equalTo($expectedType), $this->equalTo($expectedName), $this->equalTo($expectedOptions))
117119
->will($this->returnValue($this->getFormBuilder()));
118-
$this->builder->setFormFactory($factory);
119120

120121
$this->builder->add($expectedName, $expectedType, $expectedOptions);
121122
$builder = $this->builder->get($expectedName);
@@ -128,14 +129,12 @@ public function testGetGuessed()
128129
$expectedName = 'foo';
129130
$expectedOptions = array('bar' => 'baz');
130131

131-
$factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
132-
$factory->expects($this->once())
132+
$this->factory->expects($this->once())
133133
->method('createBuilderForProperty')
134134
->with($this->equalTo('stdClass'), $this->equalTo($expectedName), $this->equalTo($expectedOptions))
135135
->will($this->returnValue($this->getFormBuilder()));
136136

137-
$this->builder = new FormBuilder('name', $this->dispatcher, 'stdClass');
138-
$this->builder->setFormFactory($factory);
137+
$this->builder = new FormBuilder('name', $this->factory, $this->dispatcher, 'stdClass');
139138
$this->builder->add($expectedName, null, $expectedOptions);
140139
$builder = $this->builder->get($expectedName);
141140

tests/Symfony/Tests/Component/Form/FormTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,16 @@ class FormTest extends \PHPUnit_Framework_TestCase
2929
{
3030
private $dispatcher;
3131

32+
private $factory;
33+
3234
private $builder;
3335

3436
private $form;
3537

3638
protected function setUp()
3739
{
3840
$this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
41+
$this->factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
3942
$this->form = $this->getBuilder()->getForm();
4043
}
4144

@@ -863,7 +866,7 @@ public function testBindResetsErrors()
863866

864867
protected function getBuilder($name = 'name', EventDispatcherInterface $dispatcher = null)
865868
{
866-
return new FormBuilder($name, $dispatcher ?: $this->dispatcher);
869+
return new FormBuilder($name, $this->factory, $dispatcher ?: $this->dispatcher);
867870
}
868871

869872
protected function getMockForm($name = 'name')

tests/Symfony/Tests/Component/Form/Type/TestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ protected function setUp()
5252

5353
$this->factory = new FormFactory($this->typeLoader);
5454

55-
$this->builder = new FormBuilder('name', $this->dispatcher);
55+
$this->builder = new FormBuilder('name', $this->factory, $this->dispatcher);
5656
}
5757

5858
protected function getTypeLoaders()

tests/Symfony/Tests/Component/Form/Validator/DelegatingValidatorTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class DelegatingValidatorTest extends \PHPUnit_Framework_TestCase
2323
{
2424
private $dispatcher;
2525

26+
private $factory;
27+
2628
private $builder;
2729

2830
private $delegate;
@@ -36,6 +38,7 @@ class DelegatingValidatorTest extends \PHPUnit_Framework_TestCase
3638
protected function setUp()
3739
{
38< 8F09 /code>40
$this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
41+
$this->factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
3942
$this->delegate = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
4043
$this->validator = new DelegatingValidator($this->delegate);
4144
$this->message = 'Message';
@@ -71,7 +74,7 @@ protected function getFormError()
7174

7275
protected function getBuilder($name = 'name', $propertyPath = null)
7376
{
74-
$builder = new FormBuilder($name, $this->dispatcher);
77+
$builder = new FormBuilder($name, $this->factory, $this->dispatcher);
7578
$builder->setAttribute('property_path', new PropertyPath($propertyPath ?: $name));
7679
$builder->setAttribute('error_mapping', array());
7780

0 commit comments

Comments
 (0)
0