Fix database tests

refs #4639
This commit is contained in:
Johannes Meyer 2014-04-10 11:33:24 +02:00
parent d78aec2d48
commit 1e69169bab
4 changed files with 64 additions and 87 deletions

View File

@ -23,13 +23,10 @@ namespace Icinga\Test {
use \Exception;
use \RuntimeException;
use \Zend_Test_PHPUnit_ControllerTestCase;
use \Zend_Config;
use \Zend_Db_Adapter_Pdo_Abstract;
use \Zend_Db_Adapter_Pdo_Mysql;
use \Zend_Db_Adapter_Pdo_Pgsql;
use \Zend_Db_Adapter_Pdo_Oci;
use \Zend_Test_PHPUnit_ControllerTestCase;
use Icinga\Data\ResourceFactory;
use Icinga\Data\Db\Connection;
use Icinga\User\Preferences;
use Icinga\Web\Form;
@ -167,65 +164,64 @@ namespace Icinga\Test {
}
/**
* Creates an array of Zend Database Adapter
* Creates an array of Icinga\Data\Db\Connection
*
* @param string $name
*
* @return array
*/
private function createDbAdapterFor($name)
private function createDbConnectionFor($name)
{
try {
$adapter = ResourceFactory::createResource($this->createDbConfigFor($name))->getConnection();
$conn = ResourceFactory::createResource($this->createDbConfigFor($name));
} catch (Exception $e) {
$adapter = $e->getMessage();
$conn = $e->getMessage();
}
return array(
array($adapter)
array($conn)
);
}
/**
* PHPUnit provider for mysql
*
* @return Zend_Db_Adapter_Pdo_Mysql
* @return Connection
*/
public function mysqlDb()
{
return $this->createDbAdapterFor('mysql');
return $this->createDbConnectionFor('mysql');
}
/**
* PHPUnit provider for pgsql
*
* @return Zend_Db_Adapter_Pdo_Pgsql
* @return Connection
*/
public function pgsqlDb()
{
return $this->createDbAdapterFor('pgsql');
return $this->createDbConnectionFor('pgsql');
}
/**
* PHPUnit provider for oracle
*
* @return Zend_Db_Adapter_Pdo_Oci
* @return Connection
*/
public function oracleDb()
{
return $this->createDbAdapterFor('oracle');
return $this->createDbConnectionFor('oracle');
}
/**
* Executes sql file on PDO object
* Executes sql file by using the database connection
*
* @param Zend_Db_Adapter_Pdo_Abstract $resource
* @param string $filename
* @param Connection $resource
* @param string $filename
*
* @return boolean Operational success flag
* @throws RuntimeException
*/
public function loadSql(Zend_Db_Adapter_Pdo_Abstract $resource, $filename)
public function loadSql(Connection $resource, $filename)
{
if (!is_file($filename)) {
throw new RuntimeException(
@ -241,17 +237,17 @@ namespace Icinga\Test {
);
}
$resource->exec($sqlData);
$resource->getConnection()->exec($sqlData);
}
/**
* Setup provider for testcase
*
* @param string|Zend_Db_Adapter_PDO_Abstract|null $resource
* @param string|Connection|null $resource
*/
public function setupDbProvider($resource)
{
if (!$resource instanceof Zend_Db_Adapter_Pdo_Abstract) {
if (!$resource instanceof Connection) {
if (is_string($resource)) {
$this->markTestSkipped('Could not initialize provider: ' . $resource);
} else {
@ -260,15 +256,17 @@ namespace Icinga\Test {
return;
}
$adapter = $resource->getConnection();
try {
$resource->getConnection();
$adapter->getConnection();
} catch (Exception $e) {
$this->markTestSkipped('Could not connect to provider: '. $e->getMessage());
}
$tables = $resource->listTables();
$tables = $adapter->listTables();
foreach ($tables as $table) {
$resource->exec('DROP TABLE ' . $table . ';');
$adapter->exec('DROP TABLE ' . $table . ';');
}
}

View File

@ -29,48 +29,45 @@
namespace Icinga\Test;
use \Zend_Db_Adapter_Pdo_Abstract;
use \Zend_Db_Adapter_Pdo_Mysql;
use \Zend_Db_Adapter_Pdo_Pgsql;
use \Zend_Db_Adapter_Pdo_Oci;
use Icinga\Data\Db\Connection;
interface DbTest
{
/**
* PHPUnit provider for mysql
*
* @return Zend_Db_Adapter_Pdo_Mysql
* @return Connection
*/
public function mysqlDb();
/**
* PHPUnit provider for pgsql
*
* @return Zend_Db_Adapter_Pdo_Pgsql
* @return Connection
*/
public function pgsqlDb();
/**
* PHPUnit provider for oracle
*
* @return Zend_Db_Adapter_Pdo_Oci
* @return Connection
*/
public function oracleDb();
/**
* Executes sql file on PDO object
*
* @param Zend_Db_Adapter_PDO_Abstract $resource
* @param string $filename
* @param Connection $resource
* @param string $filename
*
* @return boolean Operational success flag
*/
public function loadSql(Zend_Db_Adapter_PDO_Abstract $resource, $filename);
public function loadSql(Connection $resource, $filename);
/**
* Setup provider for testcase
*
* @param string|Zend_Db_Adapter_PDO_Abstract|null $resource
* @param string|Connection|null $resource
*/
public function setupDbProvider($resource);
}

View File

@ -5,9 +5,9 @@
namespace Tests\Icinga\Authentication;
use \PDO;
use \Zend_Db_Adapter_Pdo_Abstract;
use \Zend_Config;
use Icinga\Test\BaseTestCase;
use Icinga\Data\Db\Connection;
use Icinga\Authentication\Backend\DbUserBackend;
/**
@ -56,7 +56,7 @@ class DbUserBackendTest extends BaseTestCase
)
);
private function createDbBackendConfig($resource, $name = null)
private function createDbConnection($resource, $name = null)
{
if ($name === null) {
$name = 'TestDbUserBackend-' . uniqid();
@ -77,11 +77,10 @@ class DbUserBackendTest extends BaseTestCase
*
* @dataProvider pgsqlDb
*/
public function testCorrectUserLoginForPgsql($db)
public function testCorrectUserLoginForPgsql($resource)
{
$this->setupDbProvider($db);
$backend = new DbUserBackend($this->createDbBackendConfig($db));
$backend->connect();
$this->setupDbProvider($resource);
$backend = new DbUserBackend($resource);
$this->runBackendAuthentication($backend);
$this->runBackendUsername($backend);
}
@ -91,23 +90,23 @@ class DbUserBackendTest extends BaseTestCase
*
* @dataProvider mysqlDb
*/
public function testCorrectUserLoginForMySQL($db)
public function testCorrectUserLoginForMySQL($resource)
{
$this->setupDbProvider($db);
$backend = new DbUserBackend($this->createDbBackendConfig($db));
$backend->connect();
$this->setupDbProvider($resource);
$backend = new DbUserBackend($resource);
$this->runBackendAuthentication($backend);
$this->runBackendUsername($backend);
}
/**
* @param Zend_Db_Adapter_Pdo_Abstract $resource
* @param Connection $resource
*/
public function setupDbProvider($resource)
{
parent::setupDbProvider($resource);
$type = $resource->getConnection()->getAttribute(PDO::ATTR_DRIVER_NAME);
$adapter = $resource->getConnection();
$type = $adapter->getConnection()->getAttribute(PDO::ATTR_DRIVER_NAME);
$dumpFile = BaseTestCase::$etcDir . '/schema/accounts.' . $type . '.sql';
@ -127,7 +126,7 @@ class DbUserBackendTest extends BaseTestCase
self::ACTIVE_COLUMN => $usr[self::ACTIVE_COLUMN],
self::SALT_COLUMN => $usr[self::SALT_COLUMN]
);
$resource->insert($this->testTable, $data);
$adapter->insert($this->testTable, $data);
}
}
@ -229,39 +228,22 @@ class DbUserBackendTest extends BaseTestCase
/**
* @dataProvider mysqlDb
*/
public function testBackendNameAssignment($db)
public function testCountUsersMySql($resource)
{
$this->setupDbProvider($db);
$this->setupDbProvider($resource);
$backend = new DbUserBackend($resource);
$testName = 'test-name-123123';
$backend = new DbUserBackend($this->createDbBackendConfig($db, $testName));
$backend->connect();
$this->assertSame($testName, $backend->getName());
}
/**
* @dataProvider mysqlDb
*/
public function testCountUsersMySql($db)
{
$this->setupDbProvider($db);
$testName = 'test-name-123123';
$backend = new DbUserBackend($this->createDbBackendConfig($db, $testName));
$backend->connect();
$this->assertGreaterThan(0, $backend->getUserCount());
$this->assertGreaterThan(0, $backend->count());
}
/**
* @dataProvider pgsqlDb
*/
public function testCountUsersPgSql($db)
public function testCountUsersPgSql($resource)
{
$this->setupDbProvider($db);
$testName = 'test-name-123123';
$backend = new DbUserBackend($this->createDbBackendConfig($db, $testName));
$backend->connect();
$this->setupDbProvider($resource);
$backend = new DbUserBackend($resource);
$this->assertGreaterThan(0, $backend->getUserCount());
$this->assertGreaterThan(0, $backend->count());
}
}

View File

@ -23,7 +23,7 @@ class BaseTestCaseDbTest extends BaseTestCase
public function testMySqlProviderAnnotation($resource)
{
$this->setupDbProvider($resource);
$this->assertInstanceOf('Zend_Db_Adapter_Pdo_Mysql', $resource);
$this->assertInstanceOf('Zend_Db_Adapter_Pdo_Mysql', $resource->getConnection());
}
/**
@ -32,9 +32,10 @@ class BaseTestCaseDbTest extends BaseTestCase
public function testMySqlCreateTablePart1($resource)
{
$this->setupDbProvider($resource);
$resource->exec('CREATE TABLE test(uid INT NOT NULL PRIMARY KEY);');
$adapter = $resource->getConnection();
$adapter->exec('CREATE TABLE test(uid INT NOT NULL PRIMARY KEY);');
$tables = $resource->listTables();
$tables = $adapter->listTables();
$this->assertCount(1, $tables);
}
@ -44,13 +45,12 @@ class BaseTestCaseDbTest extends BaseTestCase
public function testMySqlCreateTablePart2($resource)
{
$this->setupDbProvider($resource);
$tables = $resource->listTables();
$tables = $resource->getConnection()->listTables();
$this->assertCount(0, $tables);
}
private function dbAdapterSqlLoadTable($resource)
{
/** @var $resource \Zend_Db_Adapter_Pdo_Abstract **/
$this->setupDbProvider($resource);
$sqlContent = array();
@ -64,7 +64,7 @@ class BaseTestCaseDbTest extends BaseTestCase
$this->loadSql($resource, $tempFile);
$count = (int)$resource->fetchOne('SELECT COUNT(*) as cntX from dummyData;');
$count = (int) $resource->getConnection()->fetchOne('SELECT COUNT(*) as cntX from dummyData;');
$this->assertSame(20, $count);
$this->assertTrue(unlink($tempFile));
@ -84,7 +84,7 @@ class BaseTestCaseDbTest extends BaseTestCase
public function testPgSqlProviderAnnotation($resource)
{
$this->setupDbProvider($resource);
$this->assertInstanceOf('Zend_Db_Adapter_Pdo_Pgsql', $resource);
$this->assertInstanceOf('Zend_Db_Adapter_Pdo_Pgsql', $resource->getConnection());
}
/**
@ -93,10 +93,10 @@ class BaseTestCaseDbTest extends BaseTestCase
public function testPgSqlCreateTablePart1($resource)
{
$this->setupDbProvider($resource);
/** @var \Zend_Db_Adapter_Pdo_Abstract $resource **/
$resource->exec('CREATE TABLE test(uid INT NOT NULL PRIMARY KEY);');
$adapter = $resource->getConnection();
$adapter->exec('CREATE TABLE test(uid INT NOT NULL PRIMARY KEY);');
$tables = $resource->listTables();
$tables = $adapter->listTables();
$this->assertCount(1, $tables);
}
@ -106,7 +106,7 @@ class BaseTestCaseDbTest extends BaseTestCase
public function testPgSqlCreateTablePart2($resource)
{
$this->setupDbProvider($resource);
$tables = $resource->listTables();
$tables = $resource->getConnection()->listTables();
$this->assertCount(0, $tables);
}