2016-02-25 18:23:05 +01:00
|
|
|
<?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;
|
2016-02-25 18:23:05 +01:00
|
|
|
use PHPUnit_Framework_TestCase;
|
|
|
|
|
|
|
|
class BaseTestCase extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
2016-02-25 18:31:00 +01:00
|
|
|
private static $app;
|
2016-02-25 18:23:05 +01:00
|
|
|
|
2016-02-25 19:01:48 +01:00
|
|
|
private $db;
|
|
|
|
|
2016-02-25 18:23:05 +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 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->db;
|
|
|
|
}
|
|
|
|
|
2016-02-25 18:23:05 +01:00
|
|
|
protected function app()
|
|
|
|
{
|
2016-02-25 18:31:00 +01:00
|
|
|
if (self::$app === null) {
|
2016-02-25 18:23:05 +01:00
|
|
|
$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:23:05 +01:00
|
|
|
}
|
|
|
|
|
2016-02-25 18:31:00 +01:00
|
|
|
return self::$app;
|
2016-02-25 18:23:05 +01:00
|
|
|
}
|
|
|
|
}
|