Преглед изворни кода

[Form] Add tests for untested classes.

Benjamin Eberlei пре 14 година
родитељ
комит
b43318ee3e

+ 3 - 3
src/Symfony/Component/Form/DefaultFormFactory.php

@@ -48,7 +48,7 @@ class DefaultFormFactory extends FormFactory
         $csrfProvider = new DefaultCsrfProvider($csrfSecret);
         $tempStorage = new TemporaryStorage($storageSecret);
         $defaultTheme = new PhpTheme($charset);
-        return self::create($defaultTheme, $validator, $csrfProvider, $tempStorage, $entityManager);
+        return self::createInstance($defaultTheme, $validator, $csrfProvider, $tempStorage, $entityManager);
     }
 
     /**
@@ -61,7 +61,7 @@ class DefaultFormFactory extends FormFactory
      * @param \Doctrine\ORM\EntityManager $entityManager
      * @return DefaultFormFactory
      */
-    public static function create(ThemeInterface $theme,
+    public static function createInstance(ThemeInterface $theme,
             ValidatorInterface $validator,
             CsrfProviderInterface $crsfProvider,
             TemporaryStorage $tempStorage,
@@ -71,7 +71,7 @@ class DefaultFormFactory extends FormFactory
         $factory = new self($typeLoader);
         $typeLoader->initialize($factory, $theme, $crsfProvider, $validator, $tempStorage, $entityManager);
 
-        return new self($typeLoader);
+        return $factory;
     }
 
     /**

+ 8 - 0
src/Symfony/Component/Form/EventListener/MergeCollectionListener.php

@@ -15,6 +15,14 @@ use Symfony\Component\Form\Events;
 use Symfony\Component\Form\Event\FilterDataEvent;
 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 
+/**
+ * Merge changes from the request to a Doctrine\Common\Collections\Collection instance.
+ *
+ * This works with ORM, MongoDB and CouchDB instances of the collection interface.
+ *
+ * @see Doctrine\Common\Collections\Collection
+ * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
+ */
 class MergeCollectionListener implements EventSubscriberInterface
 {
     public static function getSubscribedEvents()

+ 20 - 5
src/Symfony/Component/Form/EventListener/ResizeFormListener.php

@@ -19,12 +19,26 @@ use Symfony\Component\Form\FieldInterface;
 use Symfony\Component\Form\Exception\UnexpectedTypeException;
 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 
+/**
+ * Resize a collection form element based on the data sent from the client.
+ *
+ * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
+ */
 class ResizeFormListener implements EventSubscriberInterface
 {
+    /**
+     * @var FormFactoryInterface
+     */
     private $factory;
 
+    /**
+     * @var string
+     */
     private $type;
 
+    /**
+     * @var bool
+     */
     private $resizeOnBind;
 
     public function __construct(FormFactoryInterface $factory, $type, $resizeOnBind = false)
@@ -70,24 +84,25 @@ class ResizeFormListener implements EventSubscriberInterface
 
     public function preBind(DataEvent $event)
     {
+        if (!$this->resizeOnBind) {
+            return;
+        }
+
         $form = $event->getField();
         $data = $event->getData();
 
-        $this->removedFields = array();
-
         if (null === $data) {
             $data = array();
         }
 
         foreach ($form as $name => $field) {
-            if (!isset($data[$name]) && $this->resizeOnBind && '$$name$$' != $name) {
+            if (!isset($data[$name]) && '$$name$$' != $name) {
                 $form->remove($name);
-                $this->removedFields[] = $name;
             }
         }
 
         foreach ($data as $name => $value) {
-            if (!$form->has($name) && $this->resizeOnBind) {
+            if (!$form->has($name)) {
                 $form->add($this->factory->create($this->type, $name, array(
                     'property_path' => '['.$name.']',
                 )));

+ 1 - 1
src/Symfony/Component/Form/EventListener/StripTagsFilter.php

@@ -22,7 +22,7 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  */
 class StripTagsFilter implements EventSubscriberInterface
 {
-    public function filterBoundDataFromClient(FilterDataEvent $event)
+    public function filterBoundClientData(FilterDataEvent $event)
     {
         $event->setData(strip_tags($event->getData()));
     }

+ 7 - 2
src/Symfony/Component/Form/FormBuilder.php

@@ -142,9 +142,14 @@ class FormBuilder extends FieldBuilder
      */
     public function remove($name)
     {
-        $this->fields[$name]->setParent(null);
+        if (isset($this->fields[$name])) {
+            // field might still be lazy
+            if ($this->fields[$name] instanceof FieldInterface) {
+                $this->fields[$name]->setParent(null);
+            }
 
-        unset($this->fields[$name]);
+            unset($this->fields[$name]);
+        }
     }
 
     /**

+ 54 - 0
tests/Symfony/Tests/Component/Form/EventListener/FixUrlProtocolListenerTest.php

@@ -0,0 +1,54 @@
+<?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.
+ */
+
+namespace Symfony\Tests\Component\Form\EventListener;
+
+use Symfony\Component\Form\Event\FilterDataEvent;
+use Symfony\Component\Form\EventListener\FixUrlProtocolListener;
+
+class FixUrlProtocolListenerTest extends \PHPUnit_Framework_TestCase
+{
+    public function testFixHttpUrl()
+    {
+        $data = "www.symfony.com";
+        $field = $this->getMock('Symfony\Component\Form\FieldInterface');
+        $event = new FilterDataEvent($field, $data);
+
+        $filter = new FixUrlProtocolListener('http');
+        $filter->filterBoundNormData($event);
+
+        $this->assertEquals('http://www.symfony.com', $event->getData());
+    }
+
+    public function testSkipKnownUrl()
+    {
+        $data = "http://www.symfony.com";
+        $field = $this->getMock('Symfony\Component\Form\FieldInterface');
+        $event = new FilterDataEvent($field, $data);
+
+        $filter = new FixUrlProtocolListener('http');
+        $filter->filterBoundNormData($event);
+
+        $this->assertEquals('http://www.symfony.com', $event->getData());
+    }
+
+    public function testSkipOtherProtocol()
+    {
+        $data = "ftp://www.symfony.com";
+        $field = $this->getMock('Symfony\Component\Form\FieldInterface');
+        $event = new FilterDataEvent($field, $data);
+
+        $filter = new FixUrlProtocolListener('http');
+        $filter->filterBoundNormData($event);
+
+        $this->assertEquals('ftp://www.symfony.com', $event->getData());
+    }
+}

+ 84 - 0
tests/Symfony/Tests/Component/Form/EventListener/ResizeFormListenerTest.php

@@ -0,0 +1,84 @@
+<?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.
+ */
+
+namespace Symfony\Tests\Component\Form\EventListener;
+
+use Symfony\Component\Form\Event\FilterDataEvent;
+use Symfony\Component\Form\EventListener\ResizeFormListener;
+use Symfony\Component\Form\Event\DataEvent;
+
+class ResizeFormListenerTest extends \PHPUnit_Framework_TestCase
+{
+    private $factory;
+    private $form;
+
+    public function setUp()
+    {
+        $this->factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
+        $this->form = $this->getMock('Symfony\Component\Form\Form', array('add', 'has'), array(), '', false);
+    }
+
+    public function testResizePreSetData()
+    {
+        $expectedType = "text";
+
+        $this->factory->expects($this->at(0))
+                      ->method('create')
+                      ->with($this->equalTo($expectedType), $this->equalTo( 0 ), array('property_path' => '[0]'))
+                      ->will($this->returnValue($this->getMock('Symfony\Component\Form\FieldInterface')));
+        $this->factory->expects($this->at(1))
+                      ->method('create')
+                      ->with($this->equalTo($expectedType), $this->equalTo( 1 ), array('property_path' => '[1]'))
+                      ->will($this->returnValue($this->getMock('Symfony\Component\Form\FieldInterface')));
+
+        $data = array("string", "string");
+        $event = new DataEvent($this->form, $data);
+        $listener = new ResizeFormListener($this->factory, $expectedType, false);
+        $listener->preSetData($event);
+    }
+
+    public function testResizePreSetDataNoArrayThrowsException()
+    {
+        $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
+        
+        $data = "no array or traversable";
+        $event = new DataEvent($this->form, $data);
+        $listener = new ResizeFormListener($this->factory, "text", false);
+        $listener->preSetData($event);
+    }
+
+    public function testResizePreSetDataNull()
+    {
+        $this->factory->expects($this->never())->method('create');
+
+        $data = null;
+        $event = new DataEvent($this->form, $data);
+        $listener = new ResizeFormListener($this->factory, "text", false);
+        $listener->preSetData($event);
+    }
+
+    public function testPreBind()
+    {
+        $expectedType = "text";
+
+        $this->form->expects($this->once())->method('has')->with($this->equalTo('foo'))->will($this->returnValue( false ));
+        $this->form->expects($this->once())->method('add')->with($this->isInstanceOf('Symfony\Component\Form\FieldInterface'));
+        $this->factory->expects($this->at(0))
+                      ->method('create')
+                      ->with($this->equalTo($expectedType), $this->equalTo('foo'), $this->equalTo(array('property_path' => '[foo]')))
+                      ->will($this->returnValue( $this->getMock('Symfony\Component\Form\FieldInterface') ));
+
+        $data = array("foo" => "bar");
+        $event = new DataEvent($this->form, $data);
+        $listener = new ResizeFormListener($this->factory, "text", true);
+        $listener->preBind($event);
+    }
+}

+ 1 - 1
tests/Symfony/Tests/Component/Form/EventListener/StripTagsFilterTest.php

@@ -23,7 +23,7 @@ class StripTagsFilterTest extends \PHPUnit_Framework_TestCase
         $event = new FilterDataEvent($field, $data);
 
         $filter = new StripTagsFilter();
-        $filter->filterBoundDataFromClient($event);
+        $filter->filterBoundClientData($event);
 
         $this->assertEquals('Foo!Bar!Baz!', $event->getData());
     }

+ 42 - 0
tests/Symfony/Tests/Component/Form/EventListener/TrimListenerTest.php

@@ -0,0 +1,42 @@
+<?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.
+ */
+
+namespace Symfony\Tests\Component\Form\EventListener;
+
+use Symfony\Component\Form\Event\FilterDataEvent;
+use Symfony\Component\Form\EventListener\TrimListener;
+
+class TrimListenerTest extends \PHPUnit_Framework_TestCase
+{
+    public function testTrim()
+    {
+        $data = " Foo! ";
+        $field = $this->getMock('Symfony\Component\Form\FieldInterface');
+        $event = new FilterDataEvent($field, $data);
+
+        $filter = new TrimListener();
+        $filter->filterBoundClientData($event);
+
+        $this->assertEquals('Foo!', $event->getData());
+    }
+
+    public function testTrimSkipNonStrings()
+    {
+        $data = 1234;
+        $field = $this->getMock('Symfony\Component\Form\FieldInterface');
+        $event = new FilterDataEvent($field, $data);
+
+        $filter = new TrimListener();
+        $filter->filterBoundClientData($event);
+
+        $this->assertSame(1234, $event->getData());
+    }
+}

+ 124 - 0
tests/Symfony/Tests/Component/Form/FormBuilderTest.php

@@ -0,0 +1,124 @@
+<?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.
+ */
+
+namespace Symfony\Tests\Component\Form;
+
+use Symfony\Component\Form\FormFactory;
+use Symfony\Component\Form\Type\Guesser\Guess;
+use Symfony\Component\Form\Type\Guesser\ValueGuess;
+use Symfony\Component\Form\Type\Guesser\TypeGuess;
+
+class FormBuilderTest extends \PHPUnit_Framework_TestCase
+{
+    private $builder;
+
+    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);
+    }
+
+    public function testAddNameNoString()
+    {
+        $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
+        $this->builder->add(1234);
+    }
+
+    public function testAddTypeNoString()
+    {
+        $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
+        $this->builder->add("foo", 1234);
+    }
+
+    public function testAddWithGuessFluent()
+    {
+        $this->builder->setDataClass('stdClass');
+        $builder = $this->builder->add('foo');
+        $this->assertSame($builder, $this->builder);
+    }
+
+    public function testAddIsFluent()
+    {
+        $builder = $this->builder->add("foo", "text", array("bar" => "baz"));
+        $this->assertSame($builder, $this->builder);
+    }
+
+    public function testAdd()
+    {
+        $this->assertFalse($this->builder->has('foo'));
+        $this->builder->add('foo', 'text');
+        $this->assertTrue($this->builder->has('foo'));
+    }
+
+    public function testRemove()
+    {
+        $this->builder->add('foo', 'text');
+        $this->builder->remove('foo');
+        $this->assertFalse($this->builder->has('foo'));
+    }
+
+    public function testRemoveUnknown()
+    {
+        $this->builder->remove('foo');
+        $this->assertFalse($this->builder->has('foo'));
+    }
+
+    public function testAddNoTypeNoDataClass()
+    {
+        $this->setExpectedException('Symfony\Component\Form\Exception\FormException', 'The data class must be set to automatically create fields');
+        $this->builder->add("foo");
+    }
+
+    public function testGetUnknown()
+    {
+        $this->setExpectedException('Symfony\Component\Form\Exception\FormException', 'The field "foo" does not exist');
+        $this->builder->get('foo');
+    }
+
+    public function testGetTyped()
+    {
+        $expectedType = 'text';
+        $expectedName = 'foo';
+        $expectedOptions = array('bar' => 'baz');
+
+        $factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
+        $factory->expects($this->once())
+                ->method('createBuilder')
+                ->with($this->equalTo($expectedType), $this->equalTo($expectedName), $this->equalTo($expectedOptions))
+                ->will($this->returnValue($this->getMock('Symfony\Component\Form\FieldBuilder', array(), array(), '', false)));
+        $this->builder->setFormFactory($factory);
+
+        $this->builder->add($expectedName, $expectedType, $expectedOptions);
+        $builder = $this->builder->get($expectedName);
+
+        $this->assertNotSame($builder, $this->builder);
+    }
+
+    public function testGetGuessed()
+    {
+        $expectedName = 'foo';
+        $expectedOptions = array('bar' => 'baz');
+
+        $factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
+        $factory->expects($this->once())
+                ->method('createBuilderForProperty')
+                ->with($this->equalTo('stdClass'), $this->equalTo($expectedName), $this->equalTo($expectedOptions))
+                ->will($this->returnValue($this->getMock('Symfony\Component\Form\FieldBuilder', array(), array(), '', false)));
+        $this->builder->setFormFactory($factory);
+
+        $this->builder->setDataClass('stdClass');
+        $this->builder->add($expectedName, null, $expectedOptions);
+        $builder = $this->builder->get($expectedName);
+
+        $this->assertNotSame($builder, $this->builder);
+    }
+}

+ 1 - 1
tests/Symfony/Tests/Component/Form/FormFactoryTest.php

@@ -9,7 +9,7 @@
  * file that was distributed with this source code.
  */
 
-namespace Symfony\Tests\Component\Form\FormFactory;
+namespace Symfony\Tests\Component\Form;
 
 use Symfony\Component\Form\FormFactory;
 use Symfony\Component\Form\Type\Guesser\Guess;