diff --git a/OptionsResolver.php b/OptionsResolver.php index 63649dd..e7f59a1 100644 --- a/OptionsResolver.php +++ b/OptionsResolver.php @@ -532,6 +532,7 @@ public function addNormalizer(string $option, \Closure $normalizer, bool $forceP } if ($forcePrepend) { + $this->normalizers[$option] = $this->normalizers[$option] ?? []; array_unshift($this->normalizers[$option], $normalizer); } else { $this->normalizers[$option][] = $normalizer; diff --git a/Tests/OptionsResolverTest.php b/Tests/OptionsResolverTest.php index 8e3c1d3..1fa0e54 100644 --- a/Tests/OptionsResolverTest.php +++ b/Tests/OptionsResolverTest.php @@ -1506,6 +1506,17 @@ public function testForcePrependNormalizerClosure() $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');