[DependencyInjection] Added some check before guessing similar parameters

This commit is contained in:
Grégoire Pineau 2013-04-30 20:00:25 +02:00
parent 9300157a61
commit e989d8bd0b
2 changed files with 12 additions and 0 deletions

View File

@ -93,6 +93,10 @@ class ParameterBag implements ParameterBagInterface
$name = strtolower($name);
if (!array_key_exists($name, $this->parameters)) {
if (!$name) {
throw new ParameterNotFoundException($name);
}
$alternatives = array();
foreach (array_keys($this->parameters) as $key) {
$lev = levenshtein($name, $key);

View File

@ -107,6 +107,14 @@ class ParameterBagTest extends \PHPUnit_Framework_TestCase
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException', $e, '->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
$this->assertEquals('You have requested a non-existent parameter "bag". Did you mean one of these: "bar", "baz" ?', $e->getMessage(), '->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException with some advices');
}
try {
$bag->get('');
$this->fail('->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
} catch (\Exception $e) {
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException', $e, '->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
$this->assertEquals('You have requested a non-existent parameter "".', $e->getMessage(), '->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException with some advices');
}
}
/**