Skip to content

Commit

Permalink
[OptionsResolver] Improve invalid type message on nested option
Browse files Browse the repository at this point in the history
  • Loading branch information
flohw authored and nicolas-grekas committed Aug 8, 2023
1 parent b8c939b commit 22301f0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.4
---

* Improve message with full path on invalid type in nested option

6.3
---

Expand Down
2 changes: 1 addition & 1 deletion OptionsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@ public function offsetGet(mixed $option, bool $triggerDeprecation = true): mixed
if (!$success) {
$message = sprintf(
'The option "%s" with value %s is invalid.',
$option,
$this->formatOptions([$option]),
$this->formatValue($value)
);

Expand Down
26 changes: 26 additions & 0 deletions Tests/OptionsResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,32 @@ public function testFailIfSetAllowedValuesFromLazyOption()
$this->resolver->resolve();
}

public function testResolveFailsIfInvalidValueFromNestedOption()
{
$this->expectException(InvalidOptionsException::class);
$this->expectExceptionMessage('The option "foo[bar]" with value "invalid value" is invalid. Accepted values are: "valid value".');
$this->resolver->setDefault('foo', function (OptionsResolver $resolver) {
$resolver
->setDefined('bar')
->setAllowedValues('bar', 'valid value');
});

$this->resolver->resolve(['foo' => ['bar' => 'invalid value']]);
}

public function testResolveFailsIfInvalidTypeFromNestedOption()
{
$this->expectException(InvalidOptionsException::class);
$this->expectExceptionMessage('The option "foo[bar]" with value 1 is expected to be of type "string", but is of type "int".');
$this->resolver->setDefault('foo', function (OptionsResolver $resolver) {
$resolver
->setDefined('bar')
->setAllowedTypes('bar', 'string');
});

$this->resolver->resolve(['foo' => ['bar' => 1]]);
}

public function testResolveFailsIfInvalidValue()
{
$this->expectException(InvalidOptionsException::class);
Expand Down

0 comments on commit 22301f0

Please sign in to comment.