Added support for MySQL in test runner

This commit is contained in:
Marcin Kurczewski 2014-05-15 11:01:47 +02:00
parent e84f8096bd
commit 3f93973a12

View file

@ -2,14 +2,32 @@
require_once __DIR__ . '/../src/core.php'; require_once __DIR__ . '/../src/core.php';
\Chibi\Autoloader::registerFileSystem(__DIR__); \Chibi\Autoloader::registerFileSystem(__DIR__);
$dbPath = __DIR__ . '/db.sqlite'; function getSqliteDatabasePath()
{
return __DIR__ . '/db.sqlite';
}
function getMysqlDatabaseName()
{
return 'booru_test';
}
function cleanDatabase() function cleanDatabase()
{ {
global $dbPath; if (Core::getConfig()->main->dbDriver == 'sqlite')
{
$dbPath = getSqliteDatabasePath();
if (file_exists($dbPath)) if (file_exists($dbPath))
unlink($dbPath); unlink($dbPath);
} }
elseif (Core::getConfig()->main->dbDriver == 'mysql')
{
$stmt = new \Chibi\Sql\RawStatement('DROP DATABASE IF EXISTS ' . getMysqlDatabaseName());
\Chibi\Database::exec($stmt);
$stmt = new \Chibi\Sql\RawStatement('CREATE DATABASE ' . getMysqlDatabaseName());
\Chibi\Database::exec($stmt);
}
}
function removeTestFolders() function removeTestFolders()
{ {
@ -41,20 +59,34 @@ function removeTestFolders()
} }
} }
function resetEnvironment() function resetEnvironment($dbDriver)
{ {
global $dbPath;
$_SESSION = []; $_SESSION = [];
Core::prepareConfig(true); Core::prepareConfig(true);
Core::getConfig()->main->dbDriver = 'sqlite'; Core::getConfig()->main->dbDriver = $dbDriver;
Core::getConfig()->main->dbLocation = $dbPath; if ($dbDriver == 'sqlite')
{
Core::getConfig()->main->dbLocation = getSqliteDatabasePath();
}
elseif ($dbDriver == 'mysql')
{
Core::getConfig()->main->dbLocation = getMysqlDatabaseName();
Core::getConfig()->main->dbUser = 'test';
Core::getConfig()->main->dbPass = 'test';
}
removeTestFolders(); removeTestFolders();
Core::prepareEnvironment(true); Core::prepareEnvironment(true);
if ($dbDriver == 'mysql')
{
$stmt = new \Chibi\Sql\RawStatement('USE ' . getMysqlDatabaseName());
\Chibi\Database::execUnprepared($stmt);
}
} }
$options = getopt('cf:', ['clean', 'filter:']); $options = getopt('cf:', ['clean', 'filter:', 'driver:']);
$cleanDatabase = (isset($options['c']) or isset($options['clean'])); $cleanDatabase = (isset($options['c']) or isset($options['clean']));
$dbDriver = isset($options['driver']) ? $options['driver'] : 'sqlite';
if (isset($options['f'])) if (isset($options['f']))
$filter = $options['f']; $filter = $options['f'];
@ -63,16 +95,16 @@ elseif (isset($options['filter']))
else else
$filter = null; $filter = null;
resetEnvironment(); resetEnvironment($dbDriver);
if ($cleanDatabase) if ($cleanDatabase)
cleanDatabase(); cleanDatabase();
resetEnvironment(); resetEnvironment($dbDriver);
Core::upgradeDatabase(); Core::upgradeDatabase();
$testRunner = new TestRunner; $testRunner = new TestRunner;
$testRunner->setFilter($filter); $testRunner->setFilter($filter);
$testRunner->setEnvironmentPrepareAction(function() { resetEnvironment(); }); $testRunner->setEnvironmentPrepareAction(function() use ($dbDriver) { resetEnvironment($dbDriver); });
$testRunner->setEnvironmentCleanAction(function() { removeTestFolders(); }); $testRunner->setEnvironmentCleanAction(function() { removeTestFolders(); });
$testRunner->setTestWrapperAction(function($callback) $testRunner->setTestWrapperAction(function($callback)
{ {