2013-07-16 16:18:02 +02:00
|
|
|
<?php
|
2013-07-18 17:15:32 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-07-16 16:18:02 +02:00
|
|
|
|
|
|
|
namespace Icinga\Web
|
|
|
|
{
|
2013-07-18 17:15:32 +02:00
|
|
|
/**
|
|
|
|
* Mocked controller base class to avoid the complete
|
|
|
|
* Bootstrap dependency of the normally used ModuleActionController
|
|
|
|
*/
|
2013-07-16 16:18:02 +02:00
|
|
|
class ModuleActionController
|
|
|
|
{
|
2013-07-18 17:15:32 +02:00
|
|
|
/**
|
|
|
|
* The view this controller would create
|
|
|
|
* @var stdClass
|
|
|
|
*/
|
2013-07-16 16:18:02 +02:00
|
|
|
public $view;
|
2013-07-18 17:15:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parameters provided on call
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-07-16 16:18:02 +02:00
|
|
|
public $params = array();
|
|
|
|
|
2013-07-18 17:15:32 +02:00
|
|
|
/**
|
|
|
|
* _getParam method that normally retrieves GET/POST parameters
|
|
|
|
*
|
|
|
|
* @param string $param The parameter name to retrieve
|
|
|
|
* @return mixed|bool The parameter $param or false if it doesn't exist
|
|
|
|
*/
|
2013-07-16 16:18:02 +02:00
|
|
|
public function _getParam($param)
|
|
|
|
{
|
|
|
|
if (!isset($this->params[$param])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $this->params[$param];
|
|
|
|
}
|
2013-07-18 17:15:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the backend for this controller which will be used in the action
|
|
|
|
*
|
|
|
|
* @param $backend
|
|
|
|
*/
|
2013-07-16 16:18:02 +02:00
|
|
|
public function setBackend($backend)
|
|
|
|
{
|
|
|
|
$this->backend = $backend;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace Test\Monitoring\Testlib
|
|
|
|
{
|
2013-07-17 13:07:39 +02:00
|
|
|
use Icinga\Protocol\Statusdat\Reader;
|
2013-07-16 16:18:02 +02:00
|
|
|
use Test\Monitoring\Testlib\DataSource\TestFixture;
|
|
|
|
use Test\Monitoring\Testlib\DataSource\DataSourceTestSetup;
|
|
|
|
use Monitoring\Backend\Ido;
|
2013-07-18 14:53:47 +02:00
|
|
|
use Monitoring\Backend\Statusdat;
|
2013-07-16 16:18:02 +02:00
|
|
|
|
2013-07-18 17:15:32 +02:00
|
|
|
/**
|
|
|
|
* Base class for monitoring controllers that loads required dependencies
|
|
|
|
* and allows easier setup of tests
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* <code>
|
|
|
|
*
|
|
|
|
* class MyControllerTest extends MonitoringControllerTest
|
|
|
|
* {
|
|
|
|
* public function testSomething()
|
|
|
|
* {
|
|
|
|
* // Create a test fixture
|
|
|
|
* $fixture = new TestFixture()
|
|
|
|
* $fixture->addHost('host', 0)->addService(...)->..->;
|
|
|
|
*
|
|
|
|
* $this->setupFixture($fixture, "mysql"); // setup the fixture
|
|
|
|
* $controller = $this->requireController('MyController', 'mysql');
|
|
|
|
* // controller is now the Zend controller instance, perform an action
|
|
|
|
* $controller->myAction();
|
|
|
|
* $result = $controller->view->hosts->fetchAll();
|
|
|
|
* // assert stuff
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*/
|
2013-07-19 11:29:51 +02:00
|
|
|
abstract class MonitoringControllerTest extends \PHPUnit_Framework_TestCase
|
2013-07-16 16:18:02 +02:00
|
|
|
{
|
2013-07-18 17:15:32 +02:00
|
|
|
/**
|
|
|
|
* The module directory for requiring modules (is relative to the source file)
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-07-16 16:18:02 +02:00
|
|
|
private $moduleDir = "";
|
2013-07-18 17:15:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The application directory for requirying library files (is relative to the source file)
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-07-16 16:18:02 +02:00
|
|
|
private $appDir = "";
|
2013-07-18 17:15:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Require necessary libraries on test creation
|
|
|
|
*
|
|
|
|
* This is called for every test and assures that all required libraries for the controllers
|
|
|
|
* are loaded. If you need additional dependencies you should overwrite this method, call the parent
|
|
|
|
* and then require your classes
|
|
|
|
*/
|
2013-07-16 16:18:02 +02:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->moduleDir = dirname(__FILE__) . '/../../../';
|
|
|
|
$this->appDir = $this->moduleDir.'../../library/Icinga/';
|
|
|
|
$module = $this->moduleDir;
|
|
|
|
$app = $this->appDir;
|
|
|
|
set_include_path(get_include_path().':'.$module);
|
|
|
|
set_include_path(get_include_path().':'.$app);
|
|
|
|
|
|
|
|
require_once('Zend/Config.php');
|
|
|
|
require_once('Zend/Db.php');
|
|
|
|
require_once(dirname(__FILE__) . '/datasource/DataSourceTestSetup.php');
|
|
|
|
|
|
|
|
$this->requireBase();
|
|
|
|
$this->requireViews();
|
|
|
|
}
|
|
|
|
|
2013-07-18 17:15:32 +02:00
|
|
|
/**
|
|
|
|
* Require base application and data retrieval classes from the Icinga Library
|
|
|
|
*
|
|
|
|
*/
|
2013-07-16 16:18:02 +02:00
|
|
|
private function requireBase()
|
|
|
|
{
|
|
|
|
require_once('Application/Benchmark.php');
|
|
|
|
require_once('Data/AbstractQuery.php');
|
|
|
|
require_once('Data/DatasourceInterface.php');
|
|
|
|
require_once('Data/Db/Connection.php');
|
|
|
|
require_once('Data/Db/Query.php');
|
|
|
|
require_once('Exception/ProgrammingError.php');
|
|
|
|
|
|
|
|
require_once('library/Monitoring/Backend/AbstractBackend.php');
|
2013-07-18 14:53:47 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-18 17:15:32 +02:00
|
|
|
/**
|
|
|
|
* Require all defined IDO queries in this module
|
|
|
|
*
|
|
|
|
*/
|
2013-07-18 14:53:47 +02:00
|
|
|
private function requireIDOQueries()
|
|
|
|
{
|
2013-07-16 16:18:02 +02:00
|
|
|
require_once('library/Monitoring/Backend/Ido.php');
|
2013-07-18 14:53:47 +02:00
|
|
|
$this->requireFolder('library/Monitoring/Backend/Ido/Query');
|
2013-07-16 16:18:02 +02:00
|
|
|
}
|
|
|
|
|
2013-07-18 17:15:32 +02:00
|
|
|
/**
|
|
|
|
* Require all php files in the folder $folder
|
|
|
|
*
|
|
|
|
* @param $folder The path to the folder containing PHP files
|
|
|
|
*/
|
2013-07-18 14:53:47 +02:00
|
|
|
private function requireFolder($folder)
|
2013-07-16 16:18:02 +02:00
|
|
|
{
|
|
|
|
$module = $this->moduleDir;
|
2013-07-18 14:53:47 +02:00
|
|
|
$views = scandir($module.$folder);
|
2013-07-16 16:18:02 +02:00
|
|
|
foreach ($views as $view) {
|
|
|
|
if (!preg_match('/php$/', $view)) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-07-19 11:29:51 +02:00
|
|
|
require_once(realpath($module.$folder."/".$view));
|
2013-07-16 16:18:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-18 17:15:32 +02:00
|
|
|
/**
|
|
|
|
* Require all views and queries from the statusdat backen
|
|
|
|
*
|
|
|
|
*/
|
2013-07-18 14:53:47 +02:00
|
|
|
private function requireStatusDatQueries()
|
|
|
|
{
|
2013-07-19 11:29:51 +02:00
|
|
|
require_once(realpath($this->moduleDir.'/library/Monitoring/Backend/Statusdat.php'));
|
|
|
|
require_once(realpath($this->moduleDir.'/library/Monitoring/Backend/Statusdat/Query/Query.php'));
|
2013-07-18 14:53:47 +02:00
|
|
|
$this->requireFolder('library/Monitoring/Backend/Statusdat');
|
|
|
|
$this->requireFolder('library/Monitoring/Backend/Statusdat/Criteria');
|
|
|
|
$this->requireFolder('library/Monitoring/Backend/Statusdat/Query');
|
|
|
|
$this->requireFolder('library/Monitoring/Backend/Statusdat/DataView');
|
|
|
|
$this->requireFolder('library/Monitoring/Backend/Statusdat/DataView');
|
|
|
|
}
|
|
|
|
|
2013-07-18 17:15:32 +02:00
|
|
|
/**
|
|
|
|
* Require all (generic) view classes from the monitoring module
|
|
|
|
*/
|
2013-07-16 16:18:02 +02:00
|
|
|
private function requireViews()
|
|
|
|
{
|
|
|
|
$module = $this->moduleDir;
|
|
|
|
require_once($module.'library/Monitoring/View/MonitoringView.php');
|
2013-07-18 14:53:47 +02:00
|
|
|
$this->requireFolder('library/Monitoring/View/');
|
2013-07-16 16:18:02 +02:00
|
|
|
}
|
|
|
|
|
2013-07-18 17:15:32 +02:00
|
|
|
/**
|
|
|
|
* Require and set up a controller $controller using the backend type specified at $backend
|
|
|
|
*
|
|
|
|
* @param string $controller The name of the controller tu use (must be under monitoring/application/controllers)
|
|
|
|
* @param string $backend The backend to use ('mysql', 'pgsql' or 'statusdat')
|
|
|
|
* @return ModuleActionController The newly created controller
|
|
|
|
*/
|
2013-07-16 16:18:02 +02:00
|
|
|
public function requireController($controller, $backend)
|
|
|
|
{
|
|
|
|
require_once($this->moduleDir.'/application/controllers/'.$controller.'.php');
|
|
|
|
$controllerName = '\Monitoring_'.ucfirst($controller);
|
|
|
|
$controller = new $controllerName;
|
|
|
|
$controller->setBackend($this->getBackendFor($backend));
|
|
|
|
return $controller;
|
|
|
|
}
|
|
|
|
|
2013-07-18 17:15:32 +02:00
|
|
|
/**
|
|
|
|
* Create a new backend and insert the given fixture into it
|
|
|
|
*
|
|
|
|
* @param TestFixture $fixture The TestFixture to create
|
|
|
|
* @param string $type The type of the backend ('mysql', 'pgsql' or 'statusdat')
|
|
|
|
*/
|
2013-07-16 16:18:02 +02:00
|
|
|
public function setupFixture(TestFixture $fixture, $type)
|
|
|
|
{
|
|
|
|
$dbInstance = new DataSourceTestSetup($type);
|
|
|
|
$dbInstance->setup();
|
|
|
|
$dbInstance->insert($fixture);
|
|
|
|
}
|
|
|
|
|
2013-07-18 17:15:32 +02:00
|
|
|
/**
|
|
|
|
* Set up and configure a new testbackend for the given type
|
|
|
|
*
|
|
|
|
* @param string $type The type of the backend 'mysql', 'pgsql' or 'statusdat'
|
|
|
|
* @return Ido|Statusdat The newly created backend
|
|
|
|
*/
|
2013-07-16 16:18:02 +02:00
|
|
|
public function getBackendFor($type) {
|
|
|
|
if ($type == "mysql" || $type == "pgsql") {
|
2013-07-18 14:53:47 +02:00
|
|
|
$this->requireIDOQueries();
|
2013-07-16 16:18:02 +02:00
|
|
|
return new Ido(new \Zend_Config(array(
|
|
|
|
"dbtype"=> $type,
|
|
|
|
'host' => "localhost",
|
|
|
|
'user' => "icinga_unittest",
|
|
|
|
'pass' => "icinga_unittest",
|
|
|
|
'db' => "icinga_unittest"
|
|
|
|
)));
|
2013-07-17 13:07:39 +02:00
|
|
|
} else if ($type == "statusdat") {
|
2013-07-18 14:53:47 +02:00
|
|
|
$this->requireStatusDatQueries();
|
|
|
|
return new Statusdat(new \Zend_Config(array(
|
2013-07-17 13:07:39 +02:00
|
|
|
'status_file' => '/tmp/teststatus.dat',
|
2013-07-18 14:53:47 +02:00
|
|
|
'objects_file' => '/tmp/testobjects.cache',
|
|
|
|
'no_cache' => true
|
|
|
|
)));
|
2013-07-16 16:18:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|