IcingaObject::replaceWith: implement preserve...

...and add related tests, as this is a tricky one
This commit is contained in:
Thomas Gelf 2016-07-14 17:06:48 +02:00
parent 56257c1c0f
commit 15acbb5290
3 changed files with 92 additions and 3 deletions

View File

@ -48,7 +48,8 @@ class IcingaCommandArgument extends IcingaObject
// No log right now, we have to handle "sub-objects"
}
public function replaceWith(IcingaObject $object)
// Preserve is not supported
public function replaceWith(IcingaObject $object, $preserve = null)
{
$this->setProperties((array) $object->toPlainObject(
false,

View File

@ -339,6 +339,15 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
}
}
if (substr($key, 0, 5) === 'vars.') {
$var = $this->vars()->get(substr($key, 5));
if ($var === null) {
return $var;
} else {
return $var->getValue();
}
}
if ($this->hasRelation($key)) {
if ($id = $this->get($key . '_id')) {
$class = $this->getRelationClass($key);
@ -1495,9 +1504,21 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
return static::create((array) $plain, $connection);
}
public function replaceWith(IcingaObject $object)
public function replaceWith(IcingaObject $object, $preserve = null)
{
$this->setProperties((array) $object->toPlainObject());
if ($preserve === null) {
$this->setProperties((array) $object->toPlainObject());
} else {
$plain = (array) $object->toPlainObject();
foreach ($preserve as $k) {
$v = $this->$k;
if ($v !== null) {
$plain[$k] = $v;
}
}
$this->setProperties($plain);
}
return $this;
}

View File

@ -84,6 +84,73 @@ class IcingaHostTest extends BaseTestCase
);
}
public function testPropertiesCanBePreservedWhenBeingReplaced()
{
if ($this->skipForMissingDb()) {
return;
}
$db = $this->getDb();
$this->host()->store($db);
$host = IcingaHost::load($this->testHostName, $db);
$newHost = IcingaHost::create(
array(
'display_name' => 'Replaced display',
'address' => '1.2.2.3',
'vars' => array(
'test1' => 'newstring',
'test2' => 18,
'initially' => 'set and then preserved',
)
)
);
$preserve = array('address', 'vars.test1', 'vars.initially');
$host->replaceWith($newHost, $preserve);
$this->assertEquals(
$host->address,
'127.0.0.127'
);
$this->assertEquals(
$host->{'vars.test2'},
18
);
$this->assertEquals(
$host->vars()->test2->getValue(),
18
);
$this->assertEquals(
$host->{'vars.initially'},
'set and then preserved'
);
$this->assertFalse(
array_key_exists('address', $host->getModifiedProperties()),
'Preserved property stays unmodified'
);
$newHost->set('vars.initially', 'changed later on');
$newHost->set('vars.test2', 19);
$host->replaceWith($newHost, $preserve);
$this->assertEquals(
$host->{'vars.initially'},
'set and then preserved'
);
$this->assertEquals(
$host->get('vars.test2'),
19
);
$host->delete();
}
public function testDistinctCustomVarsCanBeSetWithoutSideEffects()
{
$host = $this->host();