Skip to content

Commit c9b7afc

Browse files
author
Evan Kaufman
committed
OptionsResolver#validateOptionTypes should check if option value exists before checking its type; added corresponding test
OptionsResolver#validateOptionsCompleteness would already have thrown exception if the option were required, so this should only affect something explicitly marked as optional
1 parent 72f2e3c commit c9b7afc

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

OptionsResolver.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,10 @@ private function validateOptionValues(array $options)
312312
private function validateOptionTypes(array $options)
313313
{
314314
foreach ($this->allowedTypes as $option => $allowedTypes) {
315+
if (!isset($options[$option])) {
316+
continue;
317+
}
318+
315319
$value = $options[$option];
316320
$allowedTypes = (array) $allowedTypes;
317321

Tests/OptionsResolverTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,27 @@ public function testResolveSucceedsIfOptionTypeAllowedAddTypes()
381381
), $this->resolver->resolve($options));
382382
}
383383

384+
public function testResolveSucceedsIfOptionalWithTypeAndWithoutValue()
385+
{
386+
$this->resolver->setOptional(array(
387+
'one',
388+
'two',
389+
));
390+
391+
$this->resolver->setAllowedTypes(array(
392+
'one' => 'string',
393+
'two' => 'int',
394+
));
395+
396+
$options = array(
397+
'two' => 1,
398+
);
399+
400+
$this->assertEquals(array(
401+
'two' => 1,
402+
), $this->resolver->resolve($options));
403+
}
404+
384405
/**
385406
* @expectedException Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
386407
*/

0 commit comments

Comments
 (0)