From f66ab8dae2f3d0418759f81a3a64dc9517a62449 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 14 Apr 2024 15:17:31 +0200 Subject: Adding upstream version 1.10.2. Signed-off-by: Daniel Baumann --- library/Director/Test/BaseTestCase.php | 127 +++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 library/Director/Test/BaseTestCase.php (limited to 'library/Director/Test/BaseTestCase.php') diff --git a/library/Director/Test/BaseTestCase.php b/library/Director/Test/BaseTestCase.php new file mode 100644 index 0000000..611805b --- /dev/null +++ b/library/Director/Test/BaseTestCase.php @@ -0,0 +1,127 @@ +app(); + } + + protected function skipForMissingDb() + { + if ($this->hasDb()) { + return false; + } + + $this->markTestSkipped('Test db resource has not been configured'); + + return true; + } + + protected function hasDb() + { + return $this->getDbResourceName() !== null; + } + + protected static function getDbResourceName() + { + if (array_key_exists('DIRECTOR_TESTDB_RES', $_SERVER)) { + return $_SERVER['DIRECTOR_TESTDB_RES']; + } else { + return Config::module('director')->get('testing', 'db_resource'); + } + } + + /** + * @return Db + * @throws ConfigurationError + */ + protected static function getDb() + { + if (self::$db === null) { + $resourceName = self::getDbResourceName(); + if (! $resourceName) { + throw new ConfigurationError( + 'Could not run DB-based tests, please configure a testing db resource' + ); + } + $dbConfig = ResourceFactory::getResourceConfig($resourceName); + if (array_key_exists('DIRECTOR_TESTDB', $_SERVER)) { + $dbConfig->dbname = $_SERVER['DIRECTOR_TESTDB']; + } + if (array_key_exists('DIRECTOR_TESTDB_HOST', $_SERVER)) { + $dbConfig->host = $_SERVER['DIRECTOR_TESTDB_HOST']; + } + if (array_key_exists('DIRECTOR_TESTDB_USER', $_SERVER)) { + $dbConfig->username = $_SERVER['DIRECTOR_TESTDB_USER']; + } + if (array_key_exists('DIRECTOR_TESTDB_PASSWORD', $_SERVER)) { + $dbConfig->password = $_SERVER['DIRECTOR_TESTDB_PASSWORD']; + } + self::$db = new Db($dbConfig); + $migrations = new Migrations(self::$db); + $migrations->applyPendingMigrations(); + IcingaZone::create([ + 'object_name' => 'director-global', + 'object_type' => 'external_object', + 'is_global' => 'y' + ])->store(self::$db); + } + + return self::$db; + } + + protected function newObject($type, $name, $properties = array()) + { + if (! array_key_exists('object_type', $properties)) { + $properties['object_type'] = 'object'; + } + $properties['object_name'] = $name; + + return IcingaObject::createByType($type, $properties, $this->getDb()); + } + + protected function app() + { + if (self::$app === null) { + self::$app = Icinga::app(); + } + + return self::$app; + } + + /** + * Call a protected function for a class during testing + * + * @param $obj + * @param $name + * @param array $args + * + * @return mixed + * @throws \ReflectionException + */ + public static function callMethod($obj, $name, array $args) + { + $class = new \ReflectionClass($obj); + $method = $class->getMethod($name); + $method->setAccessible(true); + return $method->invokeArgs($obj, $args); + } +} -- cgit v1.2.3