From 420f2306ac60c1331691e9ca3ddc748eed90d0d1 Mon Sep 17 00:00:00 2001 From: diosmosis <benaka@piwik.pro> Date: Wed, 1 Oct 2014 00:46:17 -0700 Subject: [PATCH] Refactor DbHelper::truncateAllTables() to truncate non-core tables, and tweaks to DatabaseTestCase tests. --- core/Db/Schema/Mysql.php | 28 ++++++++++++++----- tests/PHPUnit/DatabaseTestCase.php | 7 ++++- tests/PHPUnit/Integration/Core/AccessTest.php | 1 - tests/PHPUnit/IntegrationTestCase.php | 13 ++------- 4 files changed, 29 insertions(+), 20 deletions(-) diff --git a/core/Db/Schema/Mysql.php b/core/Db/Schema/Mysql.php index 43a42f167d..45ff75b685 100644 --- a/core/Db/Schema/Mysql.php +++ b/core/Db/Schema/Mysql.php @@ -341,12 +341,9 @@ class Mysql implements SchemaInterface || $forceReload === true ) { $db = Db::get(); - $prefixTables = $this->getTablePrefix(); + $prefixTables = $this->getTablePrefixEscaped(); - // '_' matches any character; force it to be literal - $prefixTables = str_replace('_', '\_', $prefixTables); - - $allTables = $db->fetchCol("SHOW TABLES LIKE '" . $prefixTables . "%'"); + $allTables = $this->getAllExistingTables($prefixTables); // all the tables to be installed $allMyTables = $this->getTablesNames(); @@ -461,8 +458,8 @@ class Mysql implements SchemaInterface */ public function truncateAllTables() { - $tablesAlreadyInstalled = $this->getTablesInstalled($forceReload = true); - foreach ($tablesAlreadyInstalled as $table) { + $tables = $this->getAllExistingTables(); + foreach ($tables as $table) { Db::query("TRUNCATE `$table`"); } } @@ -489,4 +486,21 @@ class Mysql implements SchemaInterface return $dbName; } + + private function getAllExistingTables($prefixTables = false) + { + if (empty($prefixTables)) { + $prefixTables = $this->getTablePrefixEscaped(); + } + + return Db::get()->fetchCol("SHOW TABLES LIKE '" . $prefixTables . "%'"); + } + + private function getTablePrefixEscaped() + { + $prefixTables = $this->getTablePrefix(); + // '_' matches any character; force it to be literal + $prefixTables = str_replace('_', '\_', $prefixTables); + return $prefixTables; + } } diff --git a/tests/PHPUnit/DatabaseTestCase.php b/tests/PHPUnit/DatabaseTestCase.php index a0f171d7a3..278ef0b1b4 100644 --- a/tests/PHPUnit/DatabaseTestCase.php +++ b/tests/PHPUnit/DatabaseTestCase.php @@ -28,12 +28,17 @@ class DatabaseTestCase extends IntegrationTestCase public static function setUpBeforeClass() { - static::configureFixture(self::$fixture); + static::configureFixture(static::$fixture); parent::setUpBeforeClass(); self::$tableData = self::getDbTablesWithData(); } + public static function tearDownAfterClass() + { + self::$tableData = array(); + } + /** * Setup the database and create the base tables for all tests */ diff --git a/tests/PHPUnit/Integration/Core/AccessTest.php b/tests/PHPUnit/Integration/Core/AccessTest.php index cba36a9f93..ec1eb9bd29 100644 --- a/tests/PHPUnit/Integration/Core/AccessTest.php +++ b/tests/PHPUnit/Integration/Core/AccessTest.php @@ -11,7 +11,6 @@ use Piwik\AuthResult; /** * Class Core_AccessTest * - * @group Core_AccessTest * @group Core */ class Core_AccessTest extends DatabaseTestCase diff --git a/tests/PHPUnit/IntegrationTestCase.php b/tests/PHPUnit/IntegrationTestCase.php index 4687cc534c..d5d78128a4 100755 --- a/tests/PHPUnit/IntegrationTestCase.php +++ b/tests/PHPUnit/IntegrationTestCase.php @@ -518,19 +518,10 @@ abstract class IntegrationTestCase extends PHPUnit_Framework_TestCase */ protected static function restoreDbTables($tables) { - $tablesPrefix = Config::getInstance()->database_tests['tables_prefix']; - - $existingTables = array(); - foreach (Db::fetchAll("SHOW TABLES LIKE '$tablesPrefix%'") as $row) { - $existingTables[] = reset($row); - } - - // truncate existing tables - foreach ($existingTables as $existingTable) { - Db::exec("TRUNCATE `$existingTable`"); // NOTE: DbHelper::truncateAllTables() will not truncate non-core tables - } + DbHelper::truncateAllTables(); // insert data + $existingTables = DbHelper::getTablesInstalled(); foreach ($tables as $table => $rows) { // create table if it's an archive table if (strpos($table, 'archive_') !== false && !in_array($table, $existingTables)) { -- GitLab