szurubooru/tests/run-all.php

117 lines
2.7 KiB
PHP
Raw Normal View History

2014-05-04 21:23:12 +02:00
<?php
2014-05-06 11:18:04 +02:00
require_once __DIR__ . '/../src/core.php';
\Chibi\Autoloader::registerFileSystem(__DIR__);
2014-05-15 11:01:47 +02:00
function getSqliteDatabasePath()
{
return __DIR__ . '/db.sqlite';
}
function getMysqlDatabaseName()
{
return 'booru_test';
}
2014-05-15 09:39:48 +02:00
function cleanDatabase()
{
2014-05-15 11:01:47 +02:00
if (Core::getConfig()->main->dbDriver == 'sqlite')
{
$dbPath = getSqliteDatabasePath();
if (file_exists($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);
}
2014-05-15 09:39:48 +02:00
}
2014-05-15 09:39:48 +02:00
function removeTestFolders()
{
$folders =
[
realpath(Core::getConfig()->main->filesPath),
realpath(Core::getConfig()->main->thumbsPath),
realpath(dirname(Core::getConfig()->main->logsPath)),
];
foreach ($folders as $folder)
{
2014-05-15 09:39:48 +02:00
if (!file_exists($folder))
continue;
2014-05-15 09:39:48 +02:00
$it = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$folder,
FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST);
2014-05-15 09:39:48 +02:00
foreach ($it as $path)
2014-05-04 21:23:12 +02:00
{
2014-05-15 09:39:48 +02:00
$path->isFile()
? unlink($path->getPathname())
: rmdir($path->getPathname());
2014-05-04 21:23:12 +02:00
}
2014-05-15 09:39:48 +02:00
rmdir($folder);
2014-05-04 21:23:12 +02:00
}
}
2014-05-15 11:01:47 +02:00
function resetEnvironment($dbDriver)
2014-05-04 21:23:12 +02:00
{
2014-05-15 09:39:48 +02:00
$_SESSION = [];
Core::prepareConfig(true);
2014-05-15 11:01:47 +02:00
Core::getConfig()->main->dbDriver = $dbDriver;
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';
}
2014-05-15 09:39:48 +02:00
removeTestFolders();
Core::prepareEnvironment(true);
2014-05-15 11:01:47 +02:00
if ($dbDriver == 'mysql')
{
$stmt = new \Chibi\Sql\RawStatement('USE ' . getMysqlDatabaseName());
\Chibi\Database::execUnprepared($stmt);
}
2014-05-04 21:23:12 +02:00
}
2014-05-15 11:01:47 +02:00
$options = getopt('cf:', ['clean', 'filter:', 'driver:']);
2014-05-15 09:39:48 +02:00
$cleanDatabase = (isset($options['c']) or isset($options['clean']));
2014-05-15 11:01:47 +02:00
$dbDriver = isset($options['driver']) ? $options['driver'] : 'sqlite';
2014-05-15 09:39:48 +02:00
if (isset($options['f']))
$filter = $options['f'];
elseif (isset($options['filter']))
$filter = $options['filter'];
else
$filter = null;
2014-05-15 11:01:47 +02:00
resetEnvironment($dbDriver);
2014-05-15 09:39:48 +02:00
if ($cleanDatabase)
cleanDatabase();
2014-05-15 11:01:47 +02:00
resetEnvironment($dbDriver);
2014-05-15 09:39:48 +02:00
Core::upgradeDatabase();
$testRunner = new TestRunner;
$testRunner->setFilter($filter);
2014-05-15 11:01:47 +02:00
$testRunner->setEnvironmentPrepareAction(function() use ($dbDriver) { resetEnvironment($dbDriver); });
2014-05-15 09:39:48 +02:00
$testRunner->setEnvironmentCleanAction(function() { removeTestFolders(); });
$testRunner->setTestWrapperAction(function($callback)
{
\Chibi\Database::rollback(function() use ($callback)
{
$callback();
});
});
$testRunner->run($filter);