mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-06-29 18:14:26 +02:00
Create the DbAdapterFactory to instanciate db adapters, add resources.ini to configure resources, change the authentication Manager to fall back to backends with lower priority in case of errors, update the current UserBackends to the changed environment. Also adjust the documentation and existing unit tests. resolves #4503
160 lines
4.8 KiB
PHP
160 lines
4.8 KiB
PHP
<?php
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
/**
|
|
* This file is part of Icinga 2 Web.
|
|
*
|
|
* Icinga 2 Web - Head for multiple monitoring backends.
|
|
* Copyright (C) 2013 Icinga Development Team
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version 2
|
|
* of the License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
*
|
|
* @copyright 2013 Icinga Development Team <info@icinga.org>
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
|
|
* @author Icinga Development Team <info@icinga.org>
|
|
*/
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
namespace Tests\Icinga\Application;
|
|
|
|
require_once('Zend/Db.php');
|
|
require_once('Zend/Db/Adapter/Pdo/Mysql.php');
|
|
require_once('Zend/Config.php');
|
|
require_once('Zend/Log.php');
|
|
require_once('Zend/Config.php');
|
|
require_once('../../library/Icinga/Application/Logger.php');
|
|
require_once('library/Icinga/Application/ZendDbMock.php');
|
|
require_once('../../library/Icinga/Exception/ConfigurationError.php');
|
|
require_once('../../library/Icinga/Application/ConfigAwareFactory.php');
|
|
require_once('../../library/Icinga/Application/DbAdapterFactory.php');
|
|
|
|
use Tests\Icinga\Application\ZendDbMock;
|
|
use Icinga\Application\DbAdapterFactory;
|
|
use Icinga\Exception\ConfigurationError;
|
|
|
|
/*
|
|
* Unit test for the class DbAdapterFactory
|
|
*/
|
|
class DbAdapterFactoryTest extends \PHPUnit_Framework_TestCase {
|
|
|
|
/**
|
|
* The resources used for this test
|
|
*/
|
|
private $resources;
|
|
|
|
/**
|
|
* Set up the test fixture
|
|
*/
|
|
public function setUp()
|
|
{
|
|
$resources = array(
|
|
/*
|
|
* PostgreSQL databse
|
|
*/
|
|
'resource1' => array(
|
|
'type' => 'db',
|
|
'db' => 'pgsql',
|
|
'dbname' => 'resource1',
|
|
'host' => 'host1',
|
|
'username' => 'username1',
|
|
'password' => 'password1'
|
|
),
|
|
/*
|
|
* MySQL database
|
|
*/
|
|
'resource2' => array(
|
|
'type' => 'db',
|
|
'db' => 'mysql',
|
|
'dbname' => 'resource2',
|
|
'host' => 'host2',
|
|
'username' => 'username2',
|
|
'password' => 'password2'
|
|
),
|
|
/*
|
|
* Unsupported database type
|
|
*/
|
|
'resource3' => array(
|
|
'type' => 'db',
|
|
'db' => 'mssql',
|
|
'dbname' => 'resource3',
|
|
'host' => 'host3',
|
|
'username' => 'username3',
|
|
'password' => 'password3'
|
|
),
|
|
/*
|
|
* Unsupported resource type
|
|
*/
|
|
'resource4' => array(
|
|
'type' => 'ldap',
|
|
),
|
|
);
|
|
$this->resources = new \Zend_Config($resources);
|
|
DbAdapterFactory::setConfig(
|
|
$this->resources,
|
|
array(
|
|
'factory' => 'Tests\Icinga\Application\ZendDbMock'
|
|
)
|
|
);
|
|
}
|
|
|
|
public function testGetValidResource()
|
|
{
|
|
DbAdapterFactory::getDbAdapter('resource2');
|
|
$this->assertEquals(
|
|
'Pdo_Mysql',
|
|
ZendDbMock::getAdapter(),
|
|
'The db adapter name must be Pdo_Mysql.');
|
|
$this->assertEquals(
|
|
$this->getOptions($this->resources->{'resource2'}),
|
|
ZendDbMock::getConfig(),
|
|
'The options must match the original config file content'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Test if an exception is thrown, when an invalid database is used.
|
|
*
|
|
* @expectedException \Exception
|
|
*/
|
|
public function testGetInvalidDatabase()
|
|
{
|
|
DbAdapterFactory::getDbAdapter('resource3');
|
|
}
|
|
|
|
/**
|
|
* Test if an exception is thrown, when an invalid type is used.
|
|
*
|
|
* @expectedException \Exception
|
|
*/
|
|
public function testGetInvalidType()
|
|
{
|
|
DbAdapterFactory::getDbAdapter('resource4');
|
|
}
|
|
|
|
/**
|
|
* Prepare the options object for assertions
|
|
*
|
|
* @param Zend_Config $config The configuration to prepare
|
|
*
|
|
* @return array The prepared options object
|
|
*/
|
|
private function getOptions($config)
|
|
{
|
|
$options = array_merge(array(),$config->toArray());
|
|
unset($options['type']);
|
|
unset($options['db']);
|
|
return $options;
|
|
}
|
|
}
|