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

merged branch Burgov/add_error_on_wrong_type (PR #2859)

Commits
-------

d97d7e9 Added a check to see if the type is a string if it's not a FormTypeInterface

Discussion
----------

Add exception on wrong type

When you forget to extend AbstractType in your form type, and then try to create a named builder from it, the error message is quite confusing:

Expected argument of type "string", "Samson\InvoiceBundle\Form\Type\PaymentTermsType" given (from the getType() method)

This PR checks for the right type at the relevant place

---------------------------------------------------------------------------

by stloyd at 2011/12/13 03:00:29 -0800

IMO you should add an test for this.

---------------------------------------------------------------------------

by Burgov at 2011/12/13 03:11:50 -0800

@stloyd done

---------------------------------------------------------------------------

by fabpot at 2011/12/13 03:27:08 -0800

@Burgov: Looks good to me. Can you squash your commits before I merge this PR? Thanks.

---------------------------------------------------------------------------

by Burgov at 2011/12/13 03:29:00 -0800

@fabpot done!
Fabien Potencier пре 13 година
родитељ
комит
9e97e687b7

+ 3 - 1
src/Symfony/Component/Form/FormFactory.php

@@ -225,8 +225,10 @@ class FormFactory implements FormFactoryInterface
                 }
                 }
 
 
                 $this->addType($type);
                 $this->addType($type);
-            } else {
+            } elseif (is_string($type)) {
                 $type = $this->getType($type);
                 $type = $this->getType($type);
+            } else {
+                throw new UnexpectedTypeException($type, 'string or Symfony\Component\Form\FormTypeInterface');
             }
             }
 
 
             $defaultOptions = $type->getDefaultOptions($options);
             $defaultOptions = $type->getDefaultOptions($options);

+ 9 - 0
tests/Symfony/Tests/Component/Form/FormFactoryTest.php

@@ -320,6 +320,15 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
         $this->assertTrue($this->factory->hasType('foo'));
         $this->assertTrue($this->factory->hasType('foo'));
     }
     }
 
 
+    /**
+     * @expectedException        Symfony\Component\Form\Exception\UnexpectedTypeException
+     * @expectedExceptionMessage Expected argument of type "string or Symfony\Component\Form\FormTypeInterface", "stdClass" given
+     */
+    public function testCreateNamedBuilderThrowsUnderstandableException()
+    {
+        $this->factory->createNamedBuilder(new \StdClass, 'name');
+    }
+
     public function testCreateUsesTypeNameAsName()
     public function testCreateUsesTypeNameAsName()
     {
     {
         $type = new FooType();
         $type = new FooType();