Added data cleanup after each test run

This commit is contained in:
Marcin Kurczewski 2014-05-06 15:09:06 +02:00
parent a8be3a8bce
commit 431d881962
5 changed files with 65 additions and 16 deletions

View file

@ -44,6 +44,9 @@ class TransferHelper
public static function moveUpload($srcPath, $dstPath)
{
if ($srcPath == $dstPath)
throw new SimpleException('Trying to move file to the same location');
if (is_uploaded_file($srcPath))
{
move_uploaded_file($srcPath, $dstPath);
@ -57,6 +60,27 @@ class TransferHelper
}
}
public static function copy($srcPath, $dstPath)
{
if ($srcPath == $dstPath)
throw new SimpleException('Trying to copy file to the same location');
copy($srcPath, $dstPath);
}
public static function createDirectory($dirPath)
{
if (file_exists($dirPath))
{
if (!is_dir($dirPath))
throw new SimpleException($dirPath . ' exists, but it\'s not a directory');
return;
}
mkdir($dirPath, 0777, true);
}
public static function handleUploadErrors($file)
{
switch ($file['error'])

View file

@ -12,9 +12,7 @@ class Logger
self::$autoFlush = true;
self::$buffer = [];
self::$path = self::getLogPath();
$dir = dirname(self::$path);
if (!is_dir($dir))
mkdir($dir, 0777, true);
TransferHelper::createDirectory(dirname(self::$path));
#if (!is_writable(self::$path))
# throw new SimpleException('Cannot write logs to "' . self::$path . '". Check access rights.');
}

View file

@ -4,13 +4,6 @@ use \Chibi\Database as Database;
class PostModel extends AbstractCrudModel
{
protected static $config;
public static function init()
{
self::$config = getConfig();
}
public static function getTableName()
{
return 'post';
@ -293,8 +286,8 @@ class PostModel extends AbstractCrudModel
public static function validateThumbSize($width, $height)
{
$width = $width === null ? self::$config->browsing->thumbWidth : $width;
$height = $height === null ? self::$config->browsing->thumbHeight : $height;
$width = $width === null ? getConfig()->browsing->thumbWidth : $width;
$height = $height === null ? getConfig()->browsing->thumbHeight : $height;
$width = min(1000, max(1, $width));
$height = min(1000, max(1, $height));
return [$width, $height];
@ -305,7 +298,7 @@ class PostModel extends AbstractCrudModel
list ($width, $height) = self::validateThumbSize($width, $height);
return TextHelper::absolutePath(TextHelper::replaceTokens($text, [
'fullpath' => self::$config->main->thumbsPath . DS . $name,
'fullpath' => getConfig()->main->thumbsPath . DS . $name,
'width' => $width,
'height' => $height]));
}
@ -322,8 +315,6 @@ class PostModel extends AbstractCrudModel
public static function getFullPath($name)
{
return TextHelper::absolutePath(self::$config->main->filesPath . DS . $name);
return TextHelper::absolutePath(getConfig()->main->filesPath . DS . $name);
}
}
PostModel::init();

View file

@ -7,6 +7,7 @@ define('SZURU_LINK', 'http://github.com/rr-/szurubooru');
//basic settings and preparation
define('DS', DIRECTORY_SEPARATOR);
$rootDir = __DIR__ . DS . '..' . DS;
chdir($rootDir);
date_default_timezone_set('UTC');
setlocale(LC_CTYPE, 'en_US.UTF-8');
ini_set('memory_limit', '128M');
@ -65,6 +66,9 @@ function prepareEnvironment($testEnvironment)
$config = getConfig();
TransferHelper::createDirectory($config->main->filesPath);
TransferHelper::createDirectory($config->main->thumbsPath);
//extension sanity checks
$requiredExtensions = ['pdo', 'pdo_' . $config->main->dbDriver, 'gd', 'openssl', 'fileinfo'];
foreach ($requiredExtensions as $ext)

View file

@ -28,6 +28,7 @@ try
}
finally
{
removeTestFolders();
}
function resetEnvironment()
@ -36,9 +37,40 @@ function resetEnvironment()
prepareConfig(true);
getConfig()->main->dbDriver = 'sqlite';
getConfig()->main->dbLocation = $dbPath;
removeTestFolders();
prepareEnvironment(true);
}
function removeTestFolders()
{
$folders =
[
realpath(getConfig()->main->filesPath),
realpath(getConfig()->main->thumbsPath),
realpath(dirname(getConfig()->main->logsPath)),
];
foreach ($folders as $folder)
{
if (!file_exists($folder))
continue;
$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);
}
}
function getTestMethods($filter)
{
$testFiles = [];