diff --git a/core/Db/Schema/Mysql.php b/core/Db/Schema/Mysql.php index 43a42f167d1adc256224d030ab5b244ca37567bf..45ff75b68517fbe78264487b3b9a0fa8fbeade90 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 a0f171d7a310326c5d4523842f8988427480d534..278ef0b1b49fc449449b2449e0ccabee8f733ef7 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 cba36a9f93a5abccb3feee5abe0b2e4d818aeed1..ec1eb9bd29800c9c4fb7ffbff567a4ac522a3bb9 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 4687cc534c55abefd128e3c75374f78859cd9e43..d5d78128a4552d64a401f45533c00e87f4bc11fd 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)) {