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

View File

@ -4,15 +4,17 @@
namespace Tests\Icinga\Test; namespace Tests\Icinga\Test;
use Mockery;
use Icinga\Test\BaseTestCase; use Icinga\Test\BaseTestCase;
class BaseTestCaseDbTest extends BaseTestCase class BaseTestCaseTest extends BaseTestCase
{ {
private $emptySqlDumpFile; protected $emptySqlDumpFile;
public function tearDown() public function tearDown()
{ {
parent::tearDown(); parent::tearDown();
if ($this->emptySqlDumpFile) { if ($this->emptySqlDumpFile) {
unlink($this->emptySqlDumpFile); unlink($this->emptySqlDumpFile);
} }
@ -21,7 +23,7 @@ class BaseTestCaseDbTest extends BaseTestCase
/** /**
* @dataProvider mysqlDb * @dataProvider mysqlDb
*/ */
public function testMySqlProviderAnnotation($resource) public function testWhetherMySqlProviderAnnotationSetsUpZendDbAdapter($resource)
{ {
$this->setupDbProvider($resource); $this->setupDbProvider($resource);
$this->assertInstanceOf('Zend_Db_Adapter_Pdo_Mysql', $resource->getConnection()); $this->assertInstanceOf('Zend_Db_Adapter_Pdo_Mysql', $resource->getConnection());
@ -30,7 +32,16 @@ class BaseTestCaseDbTest extends BaseTestCase
/** /**
* @dataProvider mysqlDb * @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); $this->setupDbProvider($resource);
$adapter = $resource->getConnection(); $adapter = $resource->getConnection();
@ -42,47 +53,20 @@ class BaseTestCaseDbTest extends BaseTestCase
/** /**
* @dataProvider mysqlDb * @dataProvider mysqlDb
* @depends testWhetherCreatingTablesWithMySqlAdapterWorks
*/ */
public function testMySqlCreateTablePart2($resource) public function testWhetherSetupDbProviderCleansUpMySqlAdapter($resource)
{ {
$this->setupDbProvider($resource); $this->setupDbProvider($resource);
$tables = $resource->getConnection()->listTables(); $tables = $resource->getConnection()->listTables();
$this->assertCount(0, $tables); $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 * @dataProvider pgsqlDb
*/ */
public function testPgSqlProviderAnnotation($resource) public function testWhetherPgSqlProviderAnnotationSetsUpZendDbAdapter($resource)
{ {
$this->setupDbProvider($resource); $this->setupDbProvider($resource);
$this->assertInstanceOf('Zend_Db_Adapter_Pdo_Pgsql', $resource->getConnection()); $this->assertInstanceOf('Zend_Db_Adapter_Pdo_Pgsql', $resource->getConnection());
@ -91,7 +75,16 @@ class BaseTestCaseDbTest extends BaseTestCase
/** /**
* @dataProvider pgsqlDb * @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); $this->setupDbProvider($resource);
$adapter = $resource->getConnection(); $adapter = $resource->getConnection();
@ -103,56 +96,92 @@ class BaseTestCaseDbTest extends BaseTestCase
/** /**
* @dataProvider pgsqlDb * @dataProvider pgsqlDb
* @depends testWhetherCreatingTablesWithPgSqlAdapterWorks
*/ */
public function testPgSqlCreateTablePart2($resource) public function testWhetherSetupDbProviderCleansUpPgSqlAdapter($resource)
{ {
$this->setupDbProvider($resource); $this->setupDbProvider($resource);
$tables = $resource->getConnection()->listTables(); $tables = $resource->getConnection()->listTables();
$this->assertCount(0, $tables); $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); $this->dbAdapterSqlLoadTable($resource);
} }
/** /**
* @dataProvider mysqlDb * @dataProvider oracleDb
*/ */
public function testNotExistSqlDumpFile($resource) public function testWhetherCreatingTablesWithOciAdapterWorks($resource)
{ {
$this->setupDbProvider($resource); $this->setupDbProvider($resource);
$adapter = $resource->getConnection();
$adapter->exec('CREATE TABLE test(uid INT NOT NULL PRIMARY KEY);');
$this->setExpectedException( $tables = $adapter->listTables();
'RuntimeException', $this->assertCount(1, $tables);
'Sql file not found: /does/not/exist1238837 (test=testNotExistSqlDumpFile with data set #0)'
);
$this->loadSql($resource, '/does/not/exist1238837');
} }
/** /**
* @dataProvider mysqlDb * @dataProvider oracleDb
* @depends testWhetherCreatingTablesWithOciAdapterWorks
*/ */
public function testDumpFileIsEmpty($resource) public function testWhetherSetupDbProviderCleansUpOciAdapter($resource)
{ {
$this->setupDbProvider($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->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: ' protected function dbAdapterSqlLoadTable($resource)
. $this->emptySqlDumpFile {
. ' (test=testDumpFileIsEmpty with data set #0)'; $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( $tempFile = tempnam(sys_get_temp_dir(), 'icinga2-web-test-load-sql');
'RuntimeException', file_put_contents($tempFile, implode(PHP_EOL, $sqlContent));
$expectedMessage
);
$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));
} }
} }