Test: Fix provider for Preferences/DbStoreTest

refs #4608
This commit is contained in:
Marius Hein 2013-08-26 11:41:35 +02:00
parent 1130376ad6
commit 69aa6e3d22
1 changed files with 91 additions and 126 deletions

View File

@ -1,108 +1,59 @@
<?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\User\Preferences;
require_once __DIR__ . '/../../../../../../library/Icinga/Exception/ConfigurationError.php';
require_once __DIR__ . '/../../../../../../library/Icinga/Util/ConfigAwareFactory.php';
require_once __DIR__ . '/../../../../../../library/Icinga/Application/DbAdapterFactory.php';
require_once __DIR__ . '/../../../../../../library/Icinga/User.php';
require_once __DIR__ . '/../../../../../../library/Icinga/User/Preferences.php';
require_once __DIR__ . '/../../../../../../library/Icinga/User/Preferences/ChangeSet.php';
require_once __DIR__ . '/../../../../../../library/Icinga/User/Preferences/LoadInterface.php';
require_once __DIR__ . '/../../../../../../library/Icinga/User/Preferences/FlushObserverInterface.php';
require_once __DIR__ . '/../../../../../../library/Icinga/User/Preferences/DbStore.php';
// @codingStandardsIgnoreStart
require_once realpath(__DIR__. '/../../../../../../library/Icinga/Test/BaseTestCase.php');
// @codingStandardsIgnoreEnd
use Icinga\Test\BaseTestCase;
// @codingStandardsIgnoreStart
require_once 'Zend/Db.php';
require_once 'Zend/Config.php';
require_once 'Zend/Db/Adapter/Abstract.php';
require_once BaseTestCase::$libDir . '/Exception/ConfigurationError.php';
require_once BaseTestCase::$libDir . '/User.php';
require_once BaseTestCase::$libDir . '/User/Preferences.php';
require_once BaseTestCase::$libDir . '/User/Preferences/LoadInterface.php';
require_once BaseTestCase::$libDir . '/User/Preferences/FlushObserverInterface.php';
require_once BaseTestCase::$libDir . '/User/Preferences/DbStore.php';
// @codingStandardsIgnoreEnd
use Icinga\Application\DbAdapterFactory;
use \Zend_Db_Adapter_PDO_Abstract;
use Icinga\User;
use Icinga\User\Preferences\DbStore;
use Icinga\User\Preferences;
use \PHPUnit_Framework_TestCase;
use \Zend_Config;
use \Zend_Db;
use \Zend_Db_Adapter_Abstract;
use \PDOException;
use \Exception;
class DbStoreTest extends PHPUnit_Framework_TestCase
class DbStoreTest extends BaseTestCase
{
const TYPE_MYSQL = 'mysql';
const TYPE_PGSQL = 'pgsql';
private $table = 'preference';
private $databaseConfig = array(
'type' => 'db',
'host' => '127.0.0.1',
'username' => 'icinga_unittest',
'password' => 'icinga_unittest',
'dbname' => 'icinga_unittest'
);
/**
* @var Zend_Db_Adapter_Abstract
*/
private $dbMysql;
/**
* @var Zend_Db_Adapter_Abstract
*/
private $dbPgsql;
private function createDb($type)
{
$this->databaseConfig['db'] = $type;
$db = DbAdapterFactory::createDbAdapterFromConfig(
new Zend_Config($this->databaseConfig)
);
try {
$db->getConnection();
$dumpFile = realpath(__DIR__ . '/../../../../../../etc/schema/preferences.' . strtolower($type) . '.sql');
if (!$dumpFile) {
throw new Exception('Dumpfile for db type not found: ' . $type);
}
try {
$db->getConnection()->exec(file_get_contents($dumpFile));
} catch (PDOException $e) {
// PASS
}
} catch (\Zend_Db_Adapter_Exception $e) {
return null;
} catch (PDOException $e) {
return null;
}
return $db;
}
protected function setUp()
{
$this->dbMysql = $this->createDb(self::TYPE_MYSQL);
$this->dbPgsql = $this->createDb(self::TYPE_PGSQL);
}
protected function tearDown()
{
if ($this->dbMysql) {
$this->dbMysql->getConnection()->exec('DROP TABLE ' . $this->table);
}
if ($this->dbPgsql) {
$this->dbPgsql->getConnection()->exec('DROP TABLE ' . $this->table);
}
}
private function createDbStore(Zend_Db_Adapter_Abstract $db)
private function createDbStore(Zend_Db_Adapter_PDO_Abstract $db)
{
$user = new User('jdoe');
@ -113,53 +64,67 @@ class DbStoreTest extends PHPUnit_Framework_TestCase
return $store;
}
public function testCreateUpdateDeletePreferenceValuesMySQL()
/**
* @dataProvider mysqlDb
* @param Zend_Db_Adapter_PDO_Abstract $mysqlDb
*/
public function testCreateUpdateDeletePreferenceValuesMySQL($mysqlDb)
{
if ($this->dbMysql) {
$store = $this->createDbStore($this->dbMysql);
$this->setupDbProvider($mysqlDb);
$preferences = new Preferences(array());
$preferences->attach($store);
$this->loadSql(
$mysqlDb,
$sqlDumpFile = BaseTestCase::$etcDir . '/schema/preferences.mysql.sql'
);
$preferences->set('test.key1', 'OK1');
$preferences->set('test.key2', 'OK2');
$preferences->set('test.key3', 'OK2');
$store = $this->createDbStore($mysqlDb);
$preferences->remove('test.key2');
$preferences = new Preferences(array());
$preferences->attach($store);
$preferences->set('test.key3', 'OKOK333');
$preferences->set('test.key1', 'OK1');
$preferences->set('test.key2', 'OK2');
$preferences->set('test.key3', 'OK2');
$preferencesTest = new Preferences($store->load());
$this->assertEquals('OK1', $preferencesTest->get('test.key1'));
$this->assertNull($preferencesTest->get('test.key2'));
$this->assertEquals('OKOK333', $preferencesTest->get('test.key3'));
} else {
$this->markTestSkipped('MySQL test environment is not configured');
}
$preferences->remove('test.key2');
$preferences->set('test.key3', 'OKOK333');
$preferencesTest = new Preferences($store->load());
$this->assertEquals('OK1', $preferencesTest->get('test.key1'));
$this->assertNull($preferencesTest->get('test.key2'));
$this->assertEquals('OKOK333', $preferencesTest->get('test.key3'));
}
public function testCreateUpdateDeletePreferenceValuesPgSQL()
/**
* @dataProvider pgsqlDb
* @param Zend_Db_Adapter_PDO_Abstract $pgsqlDb
*/
public function testCreateUpdateDeletePreferenceValuesPgSQL($pgsqlDb)
{
if ($this->dbPgsql) {
$store = $this->createDbStore($this->dbPgsql);
$this->setupDbProvider($pgsqlDb);
$preferences = new Preferences(array());
$preferences->attach($store);
$this->loadSql(
$pgsqlDb,
$sqlDumpFile = BaseTestCase::$etcDir . '/schema/preferences.pgsql.sql'
);
$preferences->set('test.key1', 'OK1');
$preferences->set('test.key2', 'OK2');
$preferences->set('test.key3', 'OK2');
$store = $this->createDbStore($pgsqlDb);
$preferences->remove('test.key2');
$preferences = new Preferences(array());
$preferences->attach($store);
$preferences->set('test.key3', 'OKOK333');
$preferences->set('test.key1', 'OK1');
$preferences->set('test.key2', 'OK2');
$preferences->set('test.key3', 'OK2');
$preferencesTest = new Preferences($store->load());
$this->assertEquals('OK1', $preferencesTest->get('test.key1'));
$this->assertNull($preferencesTest->get('test.key2'));
$this->assertEquals('OKOK333', $preferencesTest->get('test.key3'));
} else {
$this->markTestSkipped('PgSQL test environment is not configured');
}
$preferences->remove('test.key2');
$preferences->set('test.key3', 'OKOK333');
$preferencesTest = new Preferences($store->load());
$this->assertEquals('OK1', $preferencesTest->get('test.key1'));
$this->assertNull($preferencesTest->get('test.key2'));
$this->assertEquals('OKOK333', $preferencesTest->get('test.key3'));
}
}
}