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

84 lines
2.1 KiB
PHP
Raw Normal View History

<?php
namespace Icinga\Module\Director\Test;
use Icinga\Application\Cli;
2016-02-25 19:01:48 +01:00
use Icinga\Application\Config;
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 PHPUnit_Framework_TestCase;
class BaseTestCase extends PHPUnit_Framework_TestCase
{
2016-02-25 18:31:00 +01:00
private static $app;
2016-02-25 19:01:48 +01:00
private $db;
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 function getDbResourceName()
{
return Config::module('director')->get('testing', 'db_resource');
}
2016-02-25 19:01:48 +01:00
protected function getDb()
{
if ($this->db === null) {
2016-02-27 02:03:24 +01:00
$resourceName = $this->getDbResourceName();
if (! $resourceName) {
throw new ConfigurationError(
'Could not run DB-based tests, please configure a testing db resource'
);
}
2016-02-25 19:01:48 +01:00
$this->db = Db::fromResourceName($resourceName);
$migrations = new Migrations($this->db);
$migrations->applyPendingMigrations();
2016-02-25 19:01:48 +01:00
}
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()
{
2016-02-25 18:31:00 +01:00
if (self::$app === null) {
$testModuleDir = $_SERVER['PWD'];
$libDir = dirname(dirname($testModuleDir)) . '/library';
require_once $libDir . '/Icinga/Application/Cli.php';
2016-02-25 18:31:00 +01:00
self::$app = Cli::start();
}
2016-02-25 18:31:00 +01:00
return self::$app;
}
}