Refactor test for Icinga\Test\BaseTestCase

refs #6011
This commit is contained in:
Johannes Meyer 2014-04-22 14:00:52 +02:00
parent c93159d287
commit 45d7864198
2 changed files with 93 additions and 64 deletions

View File

@ -21,11 +21,11 @@ namespace {
namespace Icinga\Test {
use \Exception;
use \RuntimeException;
use \Mockery;
use \Zend_Config;
use \Zend_Test_PHPUnit_ControllerTestCase;
use Exception;
use RuntimeException;
use Mockery;
use Zend_Config;
use Zend_Test_PHPUnit_ControllerTestCase;
use Icinga\Application\Icinga;
use Icinga\Util\DateTimeFactory;
use Icinga\Data\ResourceFactory;
@ -85,7 +85,7 @@ namespace Icinga\Test {
*
* @var array
*/
private static $dbConfiguration = array(
protected static $dbConfiguration = array(
'mysql' => array(
'type' => 'db',
'db' => 'mysql',

View File

@ -4,15 +4,17 @@
namespace Tests\Icinga\Test;
use Mockery;
use Icinga\Test\BaseTestCase;
class BaseTestCaseDbTest extends BaseTestCase
class BaseTestCaseTest extends BaseTestCase
{
private $emptySqlDumpFile;
protected $emptySqlDumpFile;
public function tearDown()
{
parent::tearDown();
if ($this->emptySqlDumpFile) {
unlink($this->emptySqlDumpFile);
}
@ -21,7 +23,7 @@ class BaseTestCaseDbTest extends BaseTestCase
/**
* @dataProvider mysqlDb
*/
public function testMySqlProviderAnnotation($resource)
public function testWhetherMySqlProviderAnnotationSetsUpZendDbAdapter($resource)
{
$this->setupDbProvider($resource);
$this->assertInstanceOf('Zend_Db_Adapter_Pdo_Mysql', $resource->getConnection());
@ -30,7 +32,16 @@ class BaseTestCaseDbTest extends BaseTestCase
/**
* @dataProvider mysqlDb
*/
public function testMySqlCreateTablePart1($resource)
public function testWhetherMySqlAdapterWorks($resource)
{
$this->setupDbProvider($resource);
$this->dbAdapterSqlLoadTable($resource);
}
/**
* @dataProvider mysqlDb
*/
public function testWhetherCreatingTablesWithMySqlAdapterWorks($resource)
{
$this->setupDbProvider($resource);
$adapter = $resource->getConnection();
@ -42,47 +53,20 @@ class BaseTestCaseDbTest extends BaseTestCase
/**
* @dataProvider mysqlDb
* @depends testWhetherCreatingTablesWithMySqlAdapterWorks
*/
public function testMySqlCreateTablePart2($resource)
public function testWhetherSetupDbProviderCleansUpMySqlAdapter($resource)
{
$this->setupDbProvider($resource);
$tables = $resource->getConnection()->listTables();
$this->assertCount(0, $tables);
}
private function dbAdapterSqlLoadTable($resource)
{
$this->setupDbProvider($resource);
$sqlContent = array();
$sqlContent[] = 'CREATE TABLE dummyData(value VARCHAR(50) NOT NULL PRIMARY KEY);';
for ($i=0; $i<20; $i++) {
$sqlContent[] = 'INSERT INTO dummyData VALUES(\'' . uniqid(). '\');';
}
$tempFile = tempnam(sys_get_temp_dir(), 'icinga2-web-test-load-sql');
file_put_contents($tempFile, implode(chr(10), $sqlContent));
$this->loadSql($resource, $tempFile);
$count = (int) $resource->getConnection()->fetchOne('SELECT COUNT(*) as cntX from dummyData;');
$this->assertSame(20, $count);
$this->assertTrue(unlink($tempFile));
}
/**
* @dataProvider mysqlDb
*/
public function testMySqlLoadTable($resource)
{
$this->dbAdapterSqlLoadTable($resource);
}
/**
* @dataProvider pgsqlDb
*/
public function testPgSqlProviderAnnotation($resource)
public function testWhetherPgSqlProviderAnnotationSetsUpZendDbAdapter($resource)
{
$this->setupDbProvider($resource);
$this->assertInstanceOf('Zend_Db_Adapter_Pdo_Pgsql', $resource->getConnection());
@ -91,7 +75,16 @@ class BaseTestCaseDbTest extends BaseTestCase
/**
* @dataProvider pgsqlDb
*/
public function testPgSqlCreateTablePart1($resource)
public function testWhetherPgSqlAdapterWorks($resource)
{
$this->setupDbProvider($resource);
$this->dbAdapterSqlLoadTable($resource);
}
/**
* @dataProvider pgsqlDb
*/
public function testWhetherCreatingTablesWithPgSqlAdapterWorks($resource)
{
$this->setupDbProvider($resource);
$adapter = $resource->getConnection();
@ -103,56 +96,92 @@ class BaseTestCaseDbTest extends BaseTestCase
/**
* @dataProvider pgsqlDb
* @depends testWhetherCreatingTablesWithPgSqlAdapterWorks
*/
public function testPgSqlCreateTablePart2($resource)
public function testWhetherSetupDbProviderCleansUpPgSqlAdapter($resource)
{
$this->setupDbProvider($resource);
$tables = $resource->getConnection()->listTables();
$this->assertCount(0, $tables);
}
/**
* @dataProvider pgsqlDb
* @dataProvider oracleDb
*/
public function testPgSqlLoadTable($resource)
public function testWhetherOciProviderAnnotationSetsUpZendDbAdapter($resource)
{
$this->setupDbProvider($resource);
$this->assertInstanceOf('Zend_Db_Adapter_Pdo_Oci', $resource->getConnection());
}
/**
* @dataProvider oracleDb
*/
public function testWhetherOciAdapterWorks($resource)
{
$this->setupDbProvider($resource);
$this->dbAdapterSqlLoadTable($resource);
}
/**
* @dataProvider mysqlDb
* @dataProvider oracleDb
*/
public function testNotExistSqlDumpFile($resource)
public function testWhetherCreatingTablesWithOciAdapterWorks($resource)
{
$this->setupDbProvider($resource);
$adapter = $resource->getConnection();
$adapter->exec('CREATE TABLE test(uid INT NOT NULL PRIMARY KEY);');
$this->setExpectedException(
'RuntimeException',
'Sql file not found: /does/not/exist1238837 (test=testNotExistSqlDumpFile with data set #0)'
);
$this->loadSql($resource, '/does/not/exist1238837');
$tables = $adapter->listTables();
$this->assertCount(1, $tables);
}
/**
* @dataProvider mysqlDb
* @dataProvider oracleDb
* @depends testWhetherCreatingTablesWithOciAdapterWorks
*/
public function testDumpFileIsEmpty($resource)
public function testWhetherSetupDbProviderCleansUpOciAdapter($resource)
{
$this->setupDbProvider($resource);
$tables = $resource->getConnection()->listTables();
$this->assertCount(0, $tables);
}
/**
* @expectedException RuntimeException
*/
public function testWhetherLoadSqlThrowsErrorWhenFileMissing()
{
$this->loadSql(Mockery::mock('Icinga\Data\Db\Connection'), 'not_existing');
}
/**
* @expectedException RuntimeException
*/
public function testWhetherLoadSqlThrowsErrorWhenFileEmpty()
{
$this->emptySqlDumpFile = tempnam(sys_get_temp_dir(), 'icinga2-web-db-test-empty');
$this->assertFileExists($this->emptySqlDumpFile);
$this->loadSql(Mockery::mock('Icinga\Data\Db\Connection'), $this->emptySqlDumpFile);
}
$expectedMessage = 'Sql file is empty: '
. $this->emptySqlDumpFile
. ' (test=testDumpFileIsEmpty with data set #0)';
protected function dbAdapterSqlLoadTable($resource)
{
$sqlContent = array();
$sqlContent[] = 'CREATE TABLE dummyData(value VARCHAR(50) NOT NULL PRIMARY KEY);';
for ($i=0; $i<20; $i++) {
$sqlContent[] = 'INSERT INTO dummyData VALUES(\'' . uniqid(). '\');';
}
$this->setExpectedException(
'RuntimeException',
$expectedMessage
);
$tempFile = tempnam(sys_get_temp_dir(), 'icinga2-web-test-load-sql');
file_put_contents($tempFile, implode(PHP_EOL, $sqlContent));
$this->loadSql($resource, $this->emptySqlDumpFile);
$this->loadSql($resource, $tempFile);
$count = (int) $resource->getConnection()->fetchOne('SELECT COUNT(*) as cntX from dummyData;');
$this->assertSame(20, $count);
$this->assertTrue(unlink($tempFile));
}
}