1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
<?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 Icinga\Module\Director\Objects\IcingaZone;
use Icinga\Test\BaseTestCase as IcingaBaseTestCase;
abstract class BaseTestCase extends IcingaBaseTestCase
{
private static $app;
/** @var Db */
private static $db;
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_PORT', $_SERVER)) {
$dbConfig->port = $_SERVER['DIRECTOR_TESTDB_PORT'];
}
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();
$zone = IcingaZone::create([
'object_name' => 'director-global',
'object_type' => 'external_object',
'is_global' => 'y'
]);
if (! IcingaZone::exists($zone->getId(), self::$db)) {
$zone->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);
}
}
|