From 53f4d77ff30c1966925302a6636ef0061ad76596 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 16 May 2014 16:52:32 +0200 Subject: [PATCH] Increased readability --- tests/ITestRunner.php | 5 - ...nner.php => ReflectionBasedTestRunner.php} | 14 +- tests/SzurubooruTestOptions.php | 7 + tests/SzurubooruTestRunner.php | 159 ++++++++++++++++++ tests/run-all.php | 114 +------------ 5 files changed, 175 insertions(+), 124 deletions(-) rename tests/{TestRunner.php => ReflectionBasedTestRunner.php} (98%) create mode 100644 tests/SzurubooruTestOptions.php create mode 100644 tests/SzurubooruTestRunner.php diff --git a/tests/ITestRunner.php b/tests/ITestRunner.php index 9709ac31..84812aa7 100644 --- a/tests/ITestRunner.php +++ b/tests/ITestRunner.php @@ -1,10 +1,5 @@ getTestFixtures($this->filter); + $this->runAll($testFixtures); + } + public function setFilter($filter) { $this->filter = $filter; @@ -33,12 +39,6 @@ class TestRunner implements ITestRunner $this->testWrapperAction = $callback; } - public function run() - { - $testFixtures = $this->getTestFixtures($this->filter); - $this->runAll($testFixtures); - } - protected function getTestFixtures($filter) { $testFiles = []; diff --git a/tests/SzurubooruTestOptions.php b/tests/SzurubooruTestOptions.php new file mode 100644 index 00000000..889b313c --- /dev/null +++ b/tests/SzurubooruTestOptions.php @@ -0,0 +1,7 @@ +getOptions(); + + $this->resetEnvironment($options); + if ($options->cleanDatabase) + $this->cleanDatabase(); + $this->resetEnvironment($options); + + 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) + { + \Chibi\Database::rollback(function() use ($callback) + { + $callback(); + }); + }); + + $testRunner->run(); + } + + private function getOptions() + { + $options = getopt('cf:', ['clean', 'filter:', 'driver:']); + + $ret = new SzurubooruTestOptions; + + $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 = new \Chibi\Sql\RawStatement('DROP DATABASE IF EXISTS ' . $this->getMysqlDatabaseName()); + \Chibi\Database::exec($stmt); + $stmt = new \Chibi\Sql\RawStatement('CREATE DATABASE ' . $this->getMysqlDatabaseName()); + \Chibi\Database::exec($stmt); + } + + private function removeTestFolders() + { + $folders = + [ + realpath(Core::getConfig()->main->filesPath), + realpath(Core::getConfig()->main->thumbsPath), + 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 resetEnvironment($options) + { + $_SESSION = []; + + 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'; + } + + $this->removeTestFolders(); + + Core::prepareEnvironment(true); + + if ($options->dbDriver == 'mysql') + { + $stmt = new \Chibi\Sql\RawStatement('USE ' . $this->getMysqlDatabaseName()); + \Chibi\Database::execUnprepared($stmt); + } + } +} diff --git a/tests/run-all.php b/tests/run-all.php index a0545191..fdfdf9ff 100644 --- a/tests/run-all.php +++ b/tests/run-all.php @@ -2,115 +2,5 @@ require_once __DIR__ . '/../src/core.php'; \Chibi\Autoloader::registerFileSystem(__DIR__); -function getSqliteDatabasePath() -{ - return __DIR__ . '/db.sqlite'; -} - -function getMysqlDatabaseName() -{ - return 'booru_test'; -} - -function cleanDatabase() -{ - 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); - } -} - -function removeTestFolders() -{ - $folders = - [ - realpath(Core::getConfig()->main->filesPath), - realpath(Core::getConfig()->main->thumbsPath), - realpath(dirname(Core::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 resetEnvironment($dbDriver) -{ - $_SESSION = []; - Core::prepareConfig(true); - 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'; - } - removeTestFolders(); - Core::prepareEnvironment(true); - - if ($dbDriver == 'mysql') - { - $stmt = new \Chibi\Sql\RawStatement('USE ' . getMysqlDatabaseName()); - \Chibi\Database::execUnprepared($stmt); - } -} - -$options = getopt('cf:', ['clean', 'filter:', 'driver:']); -$cleanDatabase = (isset($options['c']) or isset($options['clean'])); -$dbDriver = isset($options['driver']) ? $options['driver'] : 'sqlite'; - -if (isset($options['f'])) - $filter = $options['f']; -elseif (isset($options['filter'])) - $filter = $options['filter']; -else - $filter = null; - -resetEnvironment($dbDriver); -if ($cleanDatabase) - cleanDatabase(); -resetEnvironment($dbDriver); - -Core::upgradeDatabase(); - -$testRunner = new TestRunner; -$testRunner->setFilter($filter); -$testRunner->setEnvironmentPrepareAction(function() use ($dbDriver) { resetEnvironment($dbDriver); }); -$testRunner->setEnvironmentCleanAction(function() { removeTestFolders(); }); -$testRunner->setTestWrapperAction(function($callback) - { - \Chibi\Database::rollback(function() use ($callback) - { - $callback(); - }); - }); -$testRunner->run($filter); +$runner = new SzurubooruTestRunner(); +$runner->run();