From 431d8819620b98d1dfbd0a4bc76f1a8a44298d6d Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Tue, 6 May 2014 15:09:06 +0200 Subject: [PATCH] Added data cleanup after each test run --- src/Helpers/TransferHelper.php | 24 ++++++++++++++++++++++++ src/Logger.php | 4 +--- src/Models/PostModel.php | 17 ++++------------- src/core.php | 4 ++++ tests/run-all.php | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 16 deletions(-) diff --git a/src/Helpers/TransferHelper.php b/src/Helpers/TransferHelper.php index f07f405c..55c6d8d0 100644 --- a/src/Helpers/TransferHelper.php +++ b/src/Helpers/TransferHelper.php @@ -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']) diff --git a/src/Logger.php b/src/Logger.php index 2e8f83bf..606c26e1 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -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.'); } diff --git a/src/Models/PostModel.php b/src/Models/PostModel.php index e812d6b0..42ddbfb9 100644 --- a/src/Models/PostModel.php +++ b/src/Models/PostModel.php @@ -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(); diff --git a/src/core.php b/src/core.php index 558a052a..a44eac7a 100644 --- a/src/core.php +++ b/src/core.php @@ -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) diff --git a/tests/run-all.php b/tests/run-all.php index fd50b1c4..f4451cb0 100644 --- a/tests/run-all.php +++ b/tests/run-all.php @@ -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 = [];