icingaweb2-module-director/library/Director/Test/BaseTestCase.php

128 lines
3.6 KiB
PHP
Raw Normal View History

<?php
namespace Icinga\Module\Director\Test;
use Icinga\Application\Icinga;
2016-02-25 19:01:48 +01:00
use Icinga\Application\Config;
use Icinga\Data\ResourceFactory;
2016-02-27 02:03:24 +01:00
use Icinga\Exception\ConfigurationError;
2016-02-25 19:01:48 +01:00
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Db\Migrations;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Objects\IcingaZone;
use PHPUnit_Framework_TestCase;
abstract class BaseTestCase extends PHPUnit_Framework_TestCase
{
2016-02-25 18:31:00 +01:00
private static $app;
/** @var Db */
private static $db;
2016-02-25 19:01:48 +01:00
public function setUp()
{
$this->app();
}
2016-02-27 02:03:24 +01:00
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()
2016-02-27 02:03:24 +01:00
{
if (array_key_exists('DIRECTOR_TESTDB_RES', $_SERVER)) {
return $_SERVER['DIRECTOR_TESTDB_RES'];
} else {
return Config::module('director')->get('testing', 'db_resource');
}
2016-02-27 02:03:24 +01:00
}
2016-11-03 09:02:44 +01:00
/**
2017-05-24 15:16:11 +02:00
* @return Db
2016-11-03 09:02:44 +01:00
* @throws ConfigurationError
*/
protected static function getDb()
2016-02-25 19:01:48 +01:00
{
if (self::$db === null) {
$resourceName = self::getDbResourceName();
2016-02-27 02:03:24 +01:00
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);
2016-02-25 19:01:48 +01:00
}
return self::$db;
2016-02-25 19:01:48 +01:00
}
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()
{
2016-02-25 18:31:00 +01:00
if (self::$app === null) {
self::$app = Icinga::app();
}
2016-02-25 18:31:00 +01:00
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);
}
}