Skip to content

Commit 97fdf49

Browse files
committed
[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 ```
1 parent 5360f60 commit 97fdf49

File tree

5 files changed

+93
-0
lines changed

5 files changed

+93
-0
lines changed

AbstractType.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public function finishView(FormView $view, FormInterface $form, array $options)
4545
*/
4646
public function setDefaultOptions(OptionsResolverInterface $resolver)
4747
{
48+
if (!$resolver instanceof OptionsResolver) {
49+
throw new \InvalidArgumentException(sprintf('Custom resolver %s must extend Symfony\Component\OptionsResolver\OptionsResolver', get_class($resolver)));
50+
}
4851
$this->configureOptions($resolver);
4952
}
5053

AbstractTypeExtension.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public function finishView(FormView $view, FormInterface $form, array $options)
4545
*/
4646
public function setDefaultOptions(OptionsResolverInterface $resolver)
4747
{
48+
if (!$resolver instanceof OptionsResolver) {
49+
throw new \InvalidArgumentException(sprintf('Custom resolver %s must extend Symfony\Component\OptionsResolver\OptionsResolver', get_class($resolver)));
50+
}
4851
$this->configureOptions($resolver);
4952
}
5053

Tests/AbstractExtensionTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ public function testGetType()
2828
$loader = new ConcreteExtension();
2929
$this->assertInstanceOf('Symfony\Component\Form\Tests\Fixtures\FooType', $loader->getType('foo'));
3030
}
31+
32+
/**
33+
* @expectedException \InvalidArgumentException
34+
* @expectedExceptionMessage Custom resolver Symfony\Component\Form\Tests\Fixtures\CustomOptionsResolver must extend Symfony\Component\OptionsResolver\OptionsResolver
35+
*/
36+
public function testCustomOptionsResolver()
37+
{
38+
$extension = new Fixtures\FooTypeBarExtension();
39+
$resolver = new Fixtures\CustomOptionsResolver();
40+
$extension->setDefaultOptions($resolver);
41+
}
3142
}
3243

3344
class ConcreteExtension extends AbstractExtension
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Tests\Fixtures;
13+
14+
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
15+
16+
class CustomOptionsResolver implements OptionsResolverInterface
17+
{
18+
public function setDefaults(array $defaultValues)
19+
{
20+
}
21+
22+
public function replaceDefaults(array $defaultValues)
23+
{
24+
}
25+
26+
public function setOptional(array $optionNames)
27+
{
28+
}
29+
30+
public function setRequired($optionNames)
31+
{
32+
}
33+
34+
public function setAllowedValues($allowedValues)
35+
{
36+
}
37+
38+
public function addAllowedValues($allowedValues)
39+
{
40+
}
41+
42+
public function setAllowedTypes($allowedTypes)
43+
{
44+
}
45+
46+
public function addAllowedTypes($allowedTypes)
47+
{
48+
}
49+
50+
public function setNormalizers(array $normalizers)
51+
{
52+
}
53+
54+
public function isKnown($option)
55+
{
56+
}
57+
58+
public function isRequired($option)
59+
{
60+
}
61+
62+
public function resolve(array $options = array())
63+
{
64+
}
65+
}

Tests/SimpleFormTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,17 @@ public function testInitializeFailsIfParent()
10571057
$child->initialize();
10581058
}
10591059

1060+
/**
1061+
* @expectedException \InvalidArgumentException
1062+
* @expectedExceptionMessage Custom resolver Symfony\Component\Form\Tests\Fixtures\CustomOptionsResolver must extend Symfony\Component\OptionsResolver\OptionsResolver
1063+
*/
1064+
public function testCustomOptionsResolver()
1065+
{
1066+
$fooType = new Fixtures\FooType();
1067+
$resolver = new Fixtures\CustomOptionsResolver();
1068+
$fooType->setDefaultOptions($resolver);
1069+
}
1070+
10601071
protected function createForm()
10611072
{
10621073
return $this->getBuilder()->getForm();

0 commit comments

Comments
 (0)