CustomVariables: fix and test numeric key rendering

fixes #996
This commit is contained in:
Thomas Gelf 2017-07-12 12:35:26 +02:00
parent 3173b885e8
commit 15e87a15bb
2 changed files with 15 additions and 1 deletions

View File

@ -368,7 +368,7 @@ class CustomVariables implements Iterator, Countable, IcingaConfigRenderer
protected function renderKeyName($key)
{
if (preg_match('/^[a-z0-9_]+\d*$/i', $key)) {
if (preg_match('/^[a-z][a-z0-9_]*$/i', $key)) {
return 'vars.' . c::escapeIfReserved($key);
} else {
return 'vars[' . c::renderString($key) . ']';

View File

@ -44,6 +44,20 @@ class CustomVariablesTest extends BaseTestCase
);
}
public function testNumericKeysAreRenderedWithArraySyntax()
{
$vars = $this->newVars();
$vars->{'1'} = 1;
$expected = $this->indentVarsList(array(
'vars["1"] = 1'
));
$this->assertEquals(
$expected,
$vars->toConfigString(true)
);
}
public function testVariablesToExpression()
{
$vars = $this->newVars();