ExtensibleSet: add and test renderer

This commit is contained in:
Thomas Gelf 2016-02-29 12:38:31 +01:00
parent da3b9246f1
commit cc70193a56
2 changed files with 49 additions and 0 deletions

View File

@ -3,6 +3,7 @@
namespace Icinga\Module\Director\IcingaConfig; namespace Icinga\Module\Director\IcingaConfig;
use Icinga\Exception\InvalidPropertyException; use Icinga\Exception\InvalidPropertyException;
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
class ExtensibleSet class ExtensibleSet
{ {
@ -89,6 +90,38 @@ class ExtensibleSet
return $this; return $this;
} }
public function renderAs($key, $prefix = ' ')
{
$parts = array();
if ($this->ownValues !== null) {
$parts[] = c::renderKeyValue(
$key,
c::renderArray($this->ownValues),
$prefix
);
}
if (! empty($this->plusValues)) {
$parts[] = c::renderKeyOperatorValue(
$key,
'+=',
c::renderArray($this->plusValues),
$prefix
);
}
if (! empty($this->minusValues)) {
$parts[] = c::renderKeyOperatorValue(
$key,
'-=',
c::renderArray($this->plusValues),
$prefix
);
}
return implode('', $parts);
}
protected function hasBeenResolved() protected function hasBeenResolved()
{ {
return $this->resolvedValues !== null; return $this->resolvedValues !== null;

View File

@ -132,4 +132,20 @@ class ExtensibleSetTest extends BaseTestCase
$child->getResolvedValues() $child->getResolvedValues()
); );
} }
public function testCombinedDefinitionRendersCorrectly()
{
$set = new ExtensibleSet(array('pre', 'def', 'ined'));
$set->blacklist('and', 'not', 'those');
$set->extend('plus this');
$out = ' key_name = [ "pre", "def", "ined" ]' . "\n"
. ' key_name += [ "plus this" ]' . "\n"
. ' key_name -= [ "plus this" ]' . "\n";
$this->assertEquals(
$out,
$set->renderAs('key_name')
);
}
} }