From d7ddb3e63c8dde6a1adbb436e95b2ff27f502e59 Mon Sep 17 00:00:00 2001 From: Markus Frosch Date: Thu, 24 Nov 2016 17:32:34 +0100 Subject: [PATCH] Add IcingaObjectTestCase for testing IcingaObjects --- .../Director/Test/IcingaObjectTestCase.php | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 library/Director/Test/IcingaObjectTestCase.php diff --git a/library/Director/Test/IcingaObjectTestCase.php b/library/Director/Test/IcingaObjectTestCase.php new file mode 100644 index 00000000..3e307c55 --- /dev/null +++ b/library/Director/Test/IcingaObjectTestCase.php @@ -0,0 +1,92 @@ +table; + } + $properties['object_name'] = '___TEST___' . $type . '_' . $object_name; + $obj = IcingaObject::createByType($type, $properties, $this->getDb()); + + if ($storeIt === true) { + $obj->store(); + $this->prepareObjectTearDown($obj); + } + + return $obj; + } + + /** + * Helper method for loading an object + * + * @param string $name + * @param null $type + * @return IcingaObject + */ + protected function loadObject($name, $type = null) + { + if ($type === null) { + $type = $this->table; + } + $realName = '___TEST___' . $type . '_' . $name; + return IcingaObject::loadByType($type, $realName, $this->getDb()); + } + + /** + * Store the object in a list for deletion on tearDown() + * + * @param IcingaObject $object + * + * @return $this + */ + protected function prepareObjectTearDown(IcingaObject $object) + { + $this->assertTrue($object->hasBeenLoadedFromDb()); + $this->createdObjects[] = $object; + return $this; + } + + /** + * @inheritdoc + */ + public function tearDown() + { + if ($this->hasDb()) { + /** @var IcingaObject $object */ + foreach (array_reverse($this->createdObjects) as $object) { + $object->delete(); + } + + if ($this->subject !== null) { + $this->subject->delete(); + } + } + } +}