[2.7] Improved exception message if custom implementation of ``OptionsResolverInterface
`` is used
| Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Improved exception message if custom implementation of ```OptionsResolverInterface``` is used within ```AbstractType::setDefaultOptions()``` or ```AbstractTypeExtension::setDefaultOptions()```. Before: ``` Argument 1 passed to Symfony\Component\Form\AbstractType::configureOptions() must be an instance of Symfony\Component\OptionsResolver\OptionsResolver, instance of Symfony\Component\Form\Tests\Fixtures\CustomOptionsResolver given ``` After: ``` Argument 1 passed to Symfony\Component\Form\AbstractType::setDefaultOptions() must be an instance of Symfony\Component\OptionsResolver\OptionsResolver, instance of Symfony\Component\Form\Tests\Fixtures\CustomOptionsResolver given ```
This commit is contained in:
parent
72b2a79972
commit
aa945d355f
@ -45,6 +45,9 @@ abstract class AbstractType implements FormTypeInterface
|
|||||||
*/
|
*/
|
||||||
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
||||||
{
|
{
|
||||||
|
if (!$resolver instanceof OptionsResolver) {
|
||||||
|
throw new \InvalidArgumentException(sprintf('Custom resolver %s must extend Symfony\Component\OptionsResolver\OptionsResolver', get_class($resolver)));
|
||||||
|
}
|
||||||
$this->configureOptions($resolver);
|
$this->configureOptions($resolver);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,6 +45,9 @@ abstract class AbstractTypeExtension implements FormTypeExtensionInterface
|
|||||||
*/
|
*/
|
||||||
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
||||||
{
|
{
|
||||||
|
if (!$resolver instanceof OptionsResolver) {
|
||||||
|
throw new \InvalidArgumentException(sprintf('Custom resolver %s must extend Symfony\Component\OptionsResolver\OptionsResolver', get_class($resolver)));
|
||||||
|
}
|
||||||
$this->configureOptions($resolver);
|
$this->configureOptions($resolver);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,6 +28,17 @@ class AbstractExtensionTest extends \PHPUnit_Framework_TestCase
|
|||||||
$loader = new ConcreteExtension();
|
$loader = new ConcreteExtension();
|
||||||
$this->assertInstanceOf('Symfony\Component\Form\Tests\Fixtures\FooType', $loader->getType('foo'));
|
$this->assertInstanceOf('Symfony\Component\Form\Tests\Fixtures\FooType', $loader->getType('foo'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \InvalidArgumentException
|
||||||
|
* @expectedExceptionMessage Custom resolver Symfony\Component\Form\Tests\Fixtures\CustomOptionsResolver must extend Symfony\Component\OptionsResolver\OptionsResolver
|
||||||
|
*/
|
||||||
|
public function testCustomOptionsResolver()
|
||||||
|
{
|
||||||
|
$extension = new Fixtures\FooTypeBarExtension();
|
||||||
|
$resolver = new Fixtures\CustomOptionsResolver();
|
||||||
|
$extension->setDefaultOptions($resolver);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ConcreteExtension extends AbstractExtension
|
class ConcreteExtension extends AbstractExtension
|
||||||
|
@ -0,0 +1,65 @@
|
|||||||
|
<?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\Component\Form\Tests\Fixtures;
|
||||||
|
|
||||||
|
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
|
||||||
|
|
||||||
|
class CustomOptionsResolver implements OptionsResolverInterface
|
||||||
|
{
|
||||||
|
public function setDefaults(array $defaultValues)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function replaceDefaults(array $defaultValues)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setOptional(array $optionNames)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setRequired($optionNames)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setAllowedValues($allowedValues)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addAllowedValues($allowedValues)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setAllowedTypes($allowedTypes)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addAllowedTypes($allowedTypes)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setNormalizers(array $normalizers)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isKnown($option)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isRequired($option)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function resolve(array $options = array())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
@ -1057,6 +1057,17 @@ class SimpleFormTest extends AbstractFormTest
|
|||||||
$child->initialize();
|
$child->initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \InvalidArgumentException
|
||||||
|
* @expectedExceptionMessage Custom resolver Symfony\Component\Form\Tests\Fixtures\CustomOptionsResolver must extend Symfony\Component\OptionsResolver\OptionsResolver
|
||||||
|
*/
|
||||||
|
public function testCustomOptionsResolver()
|
||||||
|
{
|
||||||
|
$fooType = new Fixtures\FooType();
|
||||||
|
$resolver = new Fixtures\CustomOptionsResolver();
|
||||||
|
$fooType->setDefaultOptions($resolver);
|
||||||
|
}
|
||||||
|
|
||||||
protected function createForm()
|
protected function createForm()
|
||||||
{
|
{
|
||||||
return $this->getBuilder()->getForm();
|
return $this->getBuilder()->getForm();
|
||||||
|
Reference in New Issue
Block a user