Thomas Gelf b996a684be tests: refactor bootstrapping, initial Gitlab-CI..
...support. Currently expects three test-runners offering Debian Jessie,
Ubuntu 16.04 LTS (Xenial) and CentOS 7. Removed all dependencies on the
Icinga Web 2 test module, installing phpunit should suffice to run tests
in a normal environment.

This is a first attempt to make things easier, more to come.
2016-11-03 02:50:39 +01:00

93 lines
2.4 KiB
PHP

<?php
namespace Icinga\Module\Director\Test;
use Icinga\Application\Icinga;
use Icinga\Application\Config;
use Icinga\Data\ResourceFactory;
use Icinga\Exception\ConfigurationError;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Db\Migrations;
use Icinga\Module\Director\Objects\IcingaObject;
use PHPUnit_Framework_TestCase;
class BaseTestCase extends PHPUnit_Framework_TestCase
{
private static $app;
private $db;
public function setUp()
{
$this->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 function getDbResourceName()
{
if (array_key_exists('DIRECTOR_TESTDB_RES', $_SERVER)) {
return $_SERVER['DIRECTOR_TESTDB_RES'];
} else {
return Config::module('director')->get('testing', 'db_resource');
}
}
protected function getDb()
{
if ($this->db === null) {
$resourceName = $this->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_USER', $_SERVER)) {
$dbConfig->username = $_SERVER['DIRECTOR_TESTDB_USER'];
}
$this->db = new Db($dbConfig);
$migrations = new Migrations($this->db);
$migrations->applyPendingMigrations();
}
return $this->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);
}
protected function app()
{
if (self::$app === null) {
self::$app = Icinga::app();
}
return self::$app;
}
}