bug #37550 [OptionsResolver] Fix force prepend normalizer (hason)

This PR was merged into the 4.4 branch.

Discussion
----------

[OptionsResolver] Fix force prepend normalizer

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        |

Commits
-------

6eb836b6f9 [OptionsResolver] Fix force prepend normalizer
This commit is contained in:
Nicolas Grekas 2020-07-12 11:44:05 +02:00
commit a10ff4cb00
2 changed files with 12 additions and 0 deletions

View File

@ -532,6 +532,7 @@ class OptionsResolver implements Options
}
if ($forcePrepend) {
$this->normalizers[$option] = $this->normalizers[$option] ?? [];
array_unshift($this->normalizers[$option], $normalizer);
} else {
$this->normalizers[$option][] = $normalizer;

View File

@ -1506,6 +1506,17 @@ class OptionsResolverTest extends TestCase
$this->assertEquals(['foo' => '2nd-normalized-1st-normalized-bar'], $this->resolver->resolve());
}
public function testForcePrependNormalizerForResolverWithoutPreviousNormalizers()
{
// defined by superclass
$this->resolver->setDefault('foo', 'bar');
$this->resolver->addNormalizer('foo', function (Options $options, $value) {
return '1st-normalized-'.$value;
}, true);
$this->assertEquals(['foo' => '1st-normalized-bar'], $this->resolver->resolve());
}
public function testAddNormalizerFailsIfUnknownOption()
{
$this->expectException('Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException');