[OptionsResolver] Fix force prepend normalizer

This commit is contained in:
Martin Hasoň 2020-07-10 11:08:50 +02:00
parent ecb5f7c875
commit 6eb836b6f9
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');