szurubooru/tests/SzurubooruTestRunner.php

184 lines
3.9 KiB
PHP
Raw Normal View History

2014-05-16 16:52:32 +02:00
<?php
class SzurubooruTestRunner implements ITestRunner
{
public function run()
{
$options = $this->getOptions();
2014-05-16 18:55:52 +02:00
if ($options->help)
{
$this->printHelp();
exit(0);
}
$this->connectToDatabase($options);
2014-05-16 16:52:32 +02:00
if ($options->cleanDatabase)
$this->cleanDatabase();
$this->connectToDatabase($options);
2014-05-16 16:52:32 +02:00
Core::upgradeDatabase();
$testRunner = new ReflectionBasedTestRunner;
$testRunner->setFilter($options->filter);
$testRunner->setEnvironmentPrepareAction(function() use ($options)
{
$this->resetEnvironment($options);
});
$testRunner->setEnvironmentCleanAction(function()
{
$this->removeTestFolders();
});
$testRunner->setTestWrapperAction(function($callback)
{
Core::getDatabase()->rollback(function() use ($callback)
2014-05-16 16:52:32 +02:00
{
$callback();
});
});
$testRunner->run();
}
2014-05-16 18:55:52 +02:00
private function printHelp()
{
readfile(__DIR__ . DIRECTORY_SEPARATOR . 'help.txt');
}
2014-05-16 16:52:32 +02:00
private function getOptions()
{
2014-05-16 18:55:52 +02:00
$options = getopt('cf:h', ['clean', 'filter:', 'driver:', 'help']);
2014-05-16 16:52:32 +02:00
$ret = new SzurubooruTestOptions;
2014-05-16 18:55:52 +02:00
$ret->help = (isset($options['h']) or isset($options['h']));
2014-05-16 16:52:32 +02:00
$ret->cleanDatabase = (isset($options['c']) or isset($options['clean']));
$ret->dbDriver = 'sqlite';
if (isset($options['driver']))
$ret->dbDriver = $options['driver'];
$ret->filter = null;
if (isset($options['f']))
$ret->filter = $options['f'];
if (isset($options['filter']))
$ret->filter = $options['filter'];
return $ret;
}
private function getSqliteDatabasePath()
{
return __DIR__ . '/db.sqlite';
}
private function getMysqlDatabaseName()
{
return 'booru_test';
}
private function cleanDatabase()
{
if (Core::getConfig()->main->dbDriver == 'sqlite')
{
$this->cleanSqliteDatabase();
}
elseif (Core::getConfig()->main->dbDriver == 'mysql')
{
$this->cleanMysqlDatabase();
}
}
private function cleanSqliteDatabase()
{
$dbPath = $this->getSqliteDatabasePath();
if (file_exists($dbPath))
unlink($dbPath);
}
private function cleanMysqlDatabase()
{
$stmt = \Chibi\Sql\Statements::raw('DROP DATABASE IF EXISTS ' . $this->getMysqlDatabaseName());
Core::getDatabase()->exec($stmt);
$stmt = \Chibi\Sql\Statements::raw('CREATE DATABASE ' . $this->getMysqlDatabaseName());
Core::getDatabase()->exec($stmt);
2014-05-16 16:52:32 +02:00
}
private function removeTestFolders()
{
$folders =
[
realpath(Core::getConfig()->main->filesPath),
realpath(Core::getConfig()->main->thumbnailsPath),
2014-05-20 19:46:05 +02:00
realpath(Core::getConfig()->main->avatarsPath),
2014-05-16 16:52:32 +02:00
realpath(dirname(Core::getConfig()->main->logsPath)),
];
foreach ($folders as $folder)
$this->removeTestFolder($folder);
}
private function removeTestFolder($folder)
{
if (!file_exists($folder))
return;
$it = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$folder,
FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST);
foreach ($it as $path)
{
$path->isFile()
? unlink($path->getPathname())
: rmdir($path->getPathname());
}
rmdir($folder);
}
private function connectToDatabase($options)
2014-05-16 16:52:32 +02:00
{
$this->prepareTestConfig($options);
Core::prepareDatabase();
2014-05-16 16:52:32 +02:00
if ($options->dbDriver == 'mysql')
{
$stmt = \Chibi\Sql\Statements::raw('USE ' . $this->getMysqlDatabaseName());
Core::getDatabase()->executeUnprepared($stmt);
}
}
private function prepareTestConfig($options)
{
2014-05-16 16:52:32 +02:00
Core::prepareConfig(true);
Core::getConfig()->main->dbDriver = $options->dbDriver;
if ($options->dbDriver == 'sqlite')
{
Core::getConfig()->main->dbLocation = $this->getSqliteDatabasePath();
}
elseif ($options->dbDriver == 'mysql')
{
Core::getConfig()->main->dbLocation = $this->getMysqlDatabaseName();
Core::getConfig()->main->dbUser = 'test';
Core::getConfig()->main->dbPass = 'test';
}
}
2014-05-16 16:52:32 +02:00
private function resetEnvironment($options)
{
$_SESSION = [];
Auth::setCurrentUser(null);
2014-05-16 16:52:32 +02:00
$this->removeTestFolders();
$this->prepareTestConfig($options);
Core::prepareEnvironment();
2014-05-16 16:52:32 +02:00
}
}