Browse Source

[Form] Decoupled FormBuilder from ThemeInterface

Bernhard Schussek 14 years ago
parent
commit
321d40b21a

+ 4 - 2
src/Symfony/Component/Form/Form.php

@@ -107,7 +107,7 @@ class Form implements \IteratorAggregate, FormInterface
     private $attributes;
 
     public function __construct($name, EventDispatcherInterface $dispatcher,
-        RendererInterface $renderer, DataTransformerInterface $clientTransformer = null,
+        RendererInterface $renderer = null, DataTransformerInterface $clientTransformer = null,
         DataTransformerInterface $normTransformer = null,
         DataMapperInterface $dataMapper = null, array $validators = array(),
         $required = false, $readOnly = false, array $attributes = array())
@@ -129,7 +129,9 @@ class Form implements \IteratorAggregate, FormInterface
         $this->readOnly = $readOnly;
         $this->attributes = $attributes;
 
-        $renderer->setField($this);
+        if ($renderer) {
+            $renderer->setField($this);
+        }
 
         $this->setData(null);
     }

+ 6 - 11
src/Symfony/Component/Form/FormBuilder.php

@@ -11,13 +11,12 @@
 
 namespace Symfony\Component\Form;
 
+use Symfony\Component\Form\CsrfProvider\CsrfProviderInterface;
 use Symfony\Component\Form\DataMapper\DataMapperInterface;
 use Symfony\Component\Form\DataTransformer\DataTransformerInterface;
 use Symfony\Component\Form\Renderer\DefaultRenderer;
 use Symfony\Component\Form\Renderer\RendererInterface;
 use Symfony\Component\Form\Renderer\Plugin\RendererPluginInterface;
-use Symfony\Component\Form\Renderer\Theme\ThemeInterface;
-use Symfony\Component\Form\CsrfProvider\CsrfProviderInterface;
 use Symfony\Component\Form\Validator\FormValidatorInterface;
 use Symfony\Component\Form\Exception\FormException;
 use Symfony\Component\Form\Exception\UnexpectedTypeException;
@@ -46,8 +45,6 @@ class FormBuilder
 
     private $normalizationTransformer;
 
-    private $theme;
-
     private $validators = array();
 
     private $attributes = array();
@@ -64,10 +61,8 @@ class FormBuilder
 
     private $dataMapper;
 
-    public function __construct(ThemeInterface $theme,
-            EventDispatcherInterface $dispatcher)
+    public function __construct(EventDispatcherInterface $dispatcher)
     {
-        $this->theme = $theme;
         $this->dispatcher = $dispatcher;
     }
 
@@ -256,11 +251,11 @@ class FormBuilder
 
     protected function buildRenderer()
     {
-        if (!$this->renderer) {
-            $this->renderer = new DefaultRenderer($this->theme, 'text');
-        }
-
         foreach ($this->rendererVars as $name => $value) {
+            if (!$this->renderer) {
+                throw new FormException('A renderer must be set in order to add renderer variables or plugins');
+            }
+
             if ($value instanceof RendererPluginInterface) {
                 $this->renderer->addPlugin($value);
                 continue;

+ 1 - 1
src/Symfony/Component/Form/Type/FieldType.php

@@ -85,7 +85,7 @@ class FieldType extends AbstractType
 
     public function createBuilder(array $options)
     {
-        return new FormBuilder($this->theme, new EventDispatcher());
+        return new FormBuilder(new EventDispatcher());
     }
 
     public function getParent(array $options)

+ 11 - 4
tests/Symfony/Tests/Component/Form/FormBuilderTest.php

@@ -12,6 +12,7 @@
 namespace Symfony\Tests\Component\Form;
 
 use Symfony\Component\Form\FormFactory;
+use Symfony\Component\Form\FormBuilder;
 use Symfony\Component\Form\Type\Guesser\Guess;
 use Symfony\Component\Form\Type\Guesser\ValueGuess;
 use Symfony\Component\Form\Type\Guesser\TypeGuess;
@@ -22,9 +23,8 @@ class FormBuilderTest extends \PHPUnit_Framework_TestCase
 
     public function setUp()
     {
-        $theme = $this->getMock('Symfony\Component\Form\Renderer\Theme\ThemeInterface');
         $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
-        $this->builder = new \Symfony\Component\Form\FormBuilder($theme, $dispatcher);
+        $this->builder = new FormBuilder($dispatcher);
     }
 
     public function testAddNameNoString()
@@ -94,7 +94,7 @@ class FormBuilderTest extends \PHPUnit_Framework_TestCase
         $factory->expects($this->once())
                 ->method('createBuilder')
                 ->with($this->equalTo($expectedType), $this->equalTo($expectedName), $this->equalTo($expectedOptions))
-                ->will($this->returnValue($this->getMock('Symfony\Component\Form\FormBuilder', array(), array(), '', false)));
+                ->will($this->returnValue($this->getFormBuilder()));
         $this->builder->setFormFactory($factory);
 
         $this->builder->add($expectedName, $expectedType, $expectedOptions);
@@ -112,7 +112,7 @@ class FormBuilderTest extends \PHPUnit_Framework_TestCase
         $factory->expects($this->once())
                 ->method('createBuilderForProperty')
                 ->with($this->equalTo('stdClass'), $this->equalTo($expectedName), $this->equalTo($expectedOptions))
-                ->will($this->returnValue($this->getMock('Symfony\Component\Form\FormBuilder', array(), array(), '', false)));
+                ->will($this->returnValue($this->getFormBuilder()));
         $this->builder->setFormFactory($factory);
 
         $this->builder->setDataClass('stdClass');
@@ -121,4 +121,11 @@ class FormBuilderTest extends \PHPUnit_Framework_TestCase
 
         $this->assertNotSame($builder, $this->builder);
     }
+
+    private function getFormBuilder()
+    {
+        return $this->getMockBuilder('Symfony\Component\Form\FormBuilder')
+            ->disableOriginalConstructor()
+            ->getMock();
+    }
 }

+ 1 - 6
tests/Symfony/Tests/Component/Form/Type/EntityTypeTest.php

@@ -21,17 +21,12 @@ use Symfony\Tests\Component\Form\Fixtures\CompositeIdentEntity;
 use Doctrine\ORM\Tools\SchemaTool;
 use Doctrine\Common\Collections\ArrayCollection;
 
-class EntityChoiceTypeTest extends DoctrineOrmTestCase
+class EntityTypeTest extends DoctrineOrmTestCase
 {
     const SINGLE_IDENT_CLASS = 'Symfony\Tests\Component\Form\Fixtures\SingleIdentEntity';
 
     const COMPOSITE_IDENT_CLASS = 'Symfony\Tests\Component\Form\Fixtures\CompositeIdentEntity';
 
-    /**
-     * @var EntityManager
-     */
-    private $em;
-
     protected function setUp()
     {
         parent::setUp();

+ 5 - 4
tests/Symfony/Tests/Component/Form/Type/FieldTypeTest.php

@@ -239,11 +239,12 @@ class FieldTypeTest extends TestCase
             'app[filter2[norm[filter1[0]]]]' => 'filter2[norm[filter1[0]]]',
         ));
 
-        $this->builder->addEventSubscriber($filter);
-        $this->builder->setClientTransformer($clientTransformer);
-        $this->builder->setNormTransformer($normTransformer);
+        $builder = $this->factory->createBuilder('field', 'title');
+        $builder->addEventSubscriber($filter);
+        $builder->setClientTransformer($clientTransformer);
+        $builder->setNormTransformer($normTransformer);
 
-        $field = $this->builder->getForm();
+        $field = $builder->getForm();
         $field->bind(0);
 
         $this->assertEquals('app[filter2[norm[filter1[0]]]]', $field->getData());

+ 5 - 2
tests/Symfony/Tests/Component/Form/Type/TestCase.php

@@ -26,17 +26,20 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
 
     protected $storage;
 
-    private $em;
+    protected $em;
 
     protected $factory;
 
     protected $builder;
 
+    protected $dispatcher;
+
     protected function setUp()
     {
         $this->theme = $this->getMock('Symfony\Component\Form\Renderer\Theme\ThemeInterface');
         $this->csrfProvider = $this->getMock('Symfony\Component\Form\CsrfProvider\CsrfProviderInterface');
         $this->validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
+        $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
 
         $this->storage = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\TemporaryStorage')
             ->disableOriginalConstructor()
@@ -49,6 +52,6 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
         $loader->initialize($this->factory, $this->theme, $this->csrfProvider,
                 $this->validator, $this->storage, $this->em);
 
-        $this->builder = new FormBuilder($this->theme, new EventDispatcher(), $this->csrfProvider);
+        $this->builder = new FormBuilder($this->dispatcher);
     }
 }