minor #15798 [2.8][Form] Fix php warning on invalid FormFactory::createBuilder() argument (xelaris)

This PR was merged into the 2.8 branch.

Discussion
----------

[2.8][Form] Fix php warning on invalid FormFactory::createBuilder() argument

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

Without this check it comes to a `strpos() expects parameter 1 to be string, object given` warning, when passing an invalid argument to `FormFactory::createBuilder()` (e.g. when calling `$this->createForm(new AnEntity());` instead of `$this->createForm(new AnEntityType());` in a controller).

Commits
-------

b5599a5 [Form] Fix php warning on invalid FormFactory::createBuilder() argument
This commit is contained in:
Tobias Schultze 2015-09-22 18:06:19 +02:00
commit 70fcc9c5fb
2 changed files with 12 additions and 1 deletions

View File

@ -76,9 +76,11 @@ class FormFactory implements FormFactoryInterface
} elseif ($type instanceof FormTypeInterface) {
// BC
$typeName = $type->getName();
} else {
} elseif (is_string($type)) {
// BC
$typeName = $type;
} else {
throw new UnexpectedTypeException($type, 'string, Symfony\Component\Form\ResolvedFormTypeInterface or Symfony\Component\Form\FormTypeInterface');
}
if (null === $name) {

View File

@ -293,6 +293,15 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
$this->factory->createNamedBuilder('name', new \stdClass());
}
/**
* @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
* @expectedExceptionMessage Expected argument of type "string, Symfony\Component\Form\ResolvedFormTypeInterface or Symfony\Component\Form\FormTypeInterface", "stdClass" given
*/
public function testCreateThrowsUnderstandableException()
{
$this->factory->create(new \stdClass());
}
public function testCreateUsesTypeNameIfTypeGivenAsString()
{
$options = array('a' => '1', 'b' => '2');