parent
659e4506c0
commit
c5bea04909
|
@ -47,12 +47,12 @@ password is queried when connecting from the local machine:
|
|||
|
||||
## Writing tests for icinga
|
||||
|
||||
Icinga has it's own base test which let you simple require libraries, testing database and form. The class resides in
|
||||
library/Icinga/Test. If you write test, just subclass BaseTestCase.
|
||||
Icinga has it's own base test which lets you easily require libraries, testing database and form functionality. The class resides in
|
||||
library/Icinga/Test. If you write a test, just subclass BaseTestCase.
|
||||
|
||||
### Default test header
|
||||
|
||||
Before write a test you should inclide the base test first
|
||||
Before writing a test you should include the base test first
|
||||
|
||||
// @codingStandardsIgnoreStart
|
||||
require_once realpath(__DIR__ . '/../../../../../library/Icinga/Test/BaseTestCase.php');
|
||||
|
@ -68,7 +68,7 @@ BaseTestCase provides static variables for every directory in the project.
|
|||
### Writing database tests
|
||||
|
||||
The base test uses the PHPUnit dataProvider annotation system to create Zend Database Adapters. Typically a
|
||||
database test look like this:
|
||||
database test looks like this:
|
||||
|
||||
/**
|
||||
* @dataProvider mysqlDb
|
||||
|
@ -79,7 +79,7 @@ database test look like this:
|
|||
$this->setupDbProvider($mysqlDb); // Drops everything from existing database
|
||||
|
||||
// Load a dump file into database
|
||||
$this->loadSql($mysqlDb, BaseTestCae::$etcDir . '/etc/schema/mydump.mysql.sql');
|
||||
$this->loadSql($mysqlDb, BaseTestCase::$etcDir . '/etc/schema/mydump.mysql.sql');
|
||||
|
||||
// Test your code
|
||||
}
|
||||
|
@ -104,8 +104,8 @@ BaseTestCase holds method to require form libraries and create form classes base
|
|||
// Testing your code
|
||||
}
|
||||
|
||||
The seconds parameter of createForm() can be omitted. You can set initial post request data as
|
||||
an array if need it.
|
||||
The second parameter of createForm() can be omitted. You can set initial post request data as
|
||||
an array if needed.
|
||||
|
||||
## Writing tests for controllers
|
||||
|
||||
|
|
|
@ -149,7 +149,7 @@ class BaseTestCase extends Zend_Test_PHPUnit_ControllerTestCase implements DbTes
|
|||
return;
|
||||
}
|
||||
|
||||
$baseDir = realpath(__DIR__. '/../../../');
|
||||
$baseDir = realpath(__DIR__ . '/../../../');
|
||||
|
||||
if ($baseDir === false) {
|
||||
throw new RuntimeException('Application base dir not found');
|
||||
|
@ -160,7 +160,7 @@ class BaseTestCase extends Zend_Test_PHPUnit_ControllerTestCase implements DbTes
|
|||
self::$etcDir = $baseDir . '/etc';
|
||||
self::$testDir = $baseDir . '/test/php';
|
||||
self::$shareDir = $baseDir . '/share/icinga2-web';
|
||||
self::$moduleDir = $baseDir. '/modules';
|
||||
self::$moduleDir = $baseDir . '/modules';
|
||||
|
||||
$initialized = true;
|
||||
}
|
||||
|
@ -247,11 +247,19 @@ class BaseTestCase extends Zend_Test_PHPUnit_ControllerTestCase implements DbTes
|
|||
public function loadSql(Zend_Db_Adapter_Pdo_Abstract $resource, $filename)
|
||||
{
|
||||
if (!is_file($filename)) {
|
||||
throw new RuntimeException('Sql file not found: ' . $filename);
|
||||
throw new RuntimeException(
|
||||
'Sql file not found: ' . $filename . ' (test=' . $this->getName() . ')'
|
||||
);
|
||||
}
|
||||
|
||||
$sqlData = file_get_contents($filename);
|
||||
|
||||
if (!$sqlData) {
|
||||
throw new RuntimeException(
|
||||
'Sql file is empty: ' . $filename . ' (test=' . $this->getName() . ')'
|
||||
);
|
||||
}
|
||||
|
||||
$resource->exec($sqlData);
|
||||
}
|
||||
|
||||
|
@ -279,7 +287,7 @@ class BaseTestCase extends Zend_Test_PHPUnit_ControllerTestCase implements DbTes
|
|||
|
||||
$tables = $resource->listTables();
|
||||
foreach ($tables as $table) {
|
||||
$resource->exec('DROP TABLE ' . $table. ';');
|
||||
$resource->exec('DROP TABLE ' . $table . ';');
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -302,7 +310,7 @@ class BaseTestCase extends Zend_Test_PHPUnit_ControllerTestCase implements DbTes
|
|||
$fixedPathComponent = '/forms';
|
||||
|
||||
if (strtolower($identifier) == 'icinga') {
|
||||
$startPathComponent = self::$appDir. $fixedPathComponent;
|
||||
$startPathComponent = self::$appDir . $fixedPathComponent;
|
||||
} else {
|
||||
$startPathComponent = self::$moduleDir
|
||||
. '/'
|
||||
|
|
|
@ -33,10 +33,21 @@ require_once 'Zend/Db/Adapter/Pdo/Pgsql.php';
|
|||
require_once realpath(__DIR__ . '/../../../../../library/Icinga/Test/BaseTestCase.php');
|
||||
|
||||
use \PDO;
|
||||
use \RuntimeException;
|
||||
use Icinga\Test\BaseTestCase;
|
||||
|
||||
class BaseTestCaseDbTest extends BaseTestCase
|
||||
{
|
||||
private $emptySqlDumpFile;
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
if ($this->emptySqlDumpFile) {
|
||||
unlink($this->emptySqlDumpFile);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function testExistingTestDirectories()
|
||||
{
|
||||
$this->assertFileExists(self::$appDir);
|
||||
|
@ -148,4 +159,41 @@ class BaseTestCaseDbTest extends BaseTestCase
|
|||
{
|
||||
$this->dbAdapterSqlLoadTable($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider mysqlDb
|
||||
*/
|
||||
public function testNotExistSqlDumpFile($resource)
|
||||
{
|
||||
$this->setupDbProvider($resource);
|
||||
|
||||
$this->setExpectedException(
|
||||
'RuntimeException',
|
||||
'Sql file not found: /does/not/exist1238837 (test=testNotExistSqlDumpFile with data set #0)'
|
||||
);
|
||||
|
||||
$this->loadSql($resource, '/does/not/exist1238837');
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider mysqlDb
|
||||
*/
|
||||
public function testDumpFileIsEmpty($resource)
|
||||
{
|
||||
$this->setupDbProvider($resource);
|
||||
$this->emptySqlDumpFile = tempnam(sys_get_temp_dir(), 'icinga2-web-db-test-empty');
|
||||
$this->assertFileExists($this->emptySqlDumpFile);
|
||||
|
||||
$expectedMessage = 'Sql file is empty: '
|
||||
. $this->emptySqlDumpFile
|
||||
. ' (test=testDumpFileIsEmpty with data set #0)';
|
||||
|
||||
$this->setExpectedException(
|
||||
'RuntimeException',
|
||||
$expectedMessage
|
||||
);
|
||||
|
||||
$this->loadSql($resource, $this->emptySqlDumpFile);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue