From: Tim Otten Date: Fri, 29 Jan 2016 07:28:50 +0000 (-0800) Subject: CRM-17860 - CiviUnitTestCase - Extract body of populateDB to CiviTestDB X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=cdc44255b375f19c1fbb3a360b6939e2b5429951;p=civicrm-core.git CRM-17860 - CiviUnitTestCase - Extract body of populateDB to CiviTestDB --- diff --git a/CRM/Core/ClassLoader.php b/CRM/Core/ClassLoader.php index fb91ca0791..d78c070421 100644 --- a/CRM/Core/ClassLoader.php +++ b/CRM/Core/ClassLoader.php @@ -77,6 +77,7 @@ class CRM_Core_ClassLoader { 'CiviMailUtils', 'CiviReportTestCase', 'CiviSeleniumTestCase', + 'CiviTestDB', 'CiviTestPdoUtils', 'CiviTestSuite', 'CiviUnitTestCase', diff --git a/tests/phpunit/CiviTest/CiviTestDB.php b/tests/phpunit/CiviTest/CiviTestDB.php new file mode 100644 index 0000000000..2bcf83b76f --- /dev/null +++ b/tests/phpunit/CiviTest/CiviTestDB.php @@ -0,0 +1,103 @@ +pdo; + // only consider real tables and not views + $tables = $pdo->query("SELECT table_name FROM INFORMATION_SCHEMA.TABLES + WHERE TABLE_SCHEMA = '{$dbName}' AND TABLE_TYPE = 'BASE TABLE'"); + + $truncates = array(); + $drops = array(); + foreach ($tables as $table) { + // skip log tables + if (substr($table['table_name'], 0, 4) == 'log_') { + continue; + } + + // don't change list of installed extensions + if ($table['table_name'] == 'civicrm_extension') { + continue; + } + + if (substr($table['table_name'], 0, 14) == 'civicrm_value_') { + $drops[] = 'DROP TABLE ' . $table['table_name'] . ';'; + } + else { + $truncates[] = 'TRUNCATE ' . $table['table_name'] . ';'; + } + } + + $queries = array( + "USE {$dbName};", + "SET foreign_key_checks = 0", + // SQL mode needs to be strict, that's our standard + "SET SQL_MODE='STRICT_ALL_TABLES';", + "SET global innodb_flush_log_at_trx_commit = 2;", + ); + $queries = array_merge($queries, $truncates); + $queries = array_merge($queries, $drops); + foreach ($queries as $query) { + if ($pdoUtils->do_query($query) === FALSE) { + // failed to create test database + echo "failed to create test db."; + exit; + } + } + + // initialize test database + $sql_file2 = dirname(dirname(dirname(dirname(__FILE__)))) . "/sql/civicrm_data.mysql"; + $sql_file3 = dirname(dirname(dirname(dirname(__FILE__)))) . "/sql/test_data.mysql"; + $sql_file4 = dirname(dirname(dirname(dirname(__FILE__)))) . "/sql/test_data_second_domain.mysql"; + + $query2 = file_get_contents($sql_file2); + $query3 = file_get_contents($sql_file3); + $query4 = file_get_contents($sql_file4); + if ($pdoUtils->do_query($query2) === FALSE) { + echo "Cannot load civicrm_data.mysql. Aborting."; + exit; + } + if ($pdoUtils->do_query($query3) === FALSE) { + echo "Cannot load test_data.mysql. Aborting."; + exit; + } + if ($pdoUtils->do_query($query4) === FALSE) { + echo "Cannot load test_data.mysql. Aborting."; + exit; + } + + // done with all the loading, get transactions back + if ($pdoUtils->do_query("set global innodb_flush_log_at_trx_commit = 1;") === FALSE) { + echo "Cannot set global? Huh?"; + exit; + } + + if ($pdoUtils->do_query("SET foreign_key_checks = 1") === FALSE) { + echo "Cannot get foreign keys back? Huh?"; + exit; + } + + unset($query, $query2, $query3); + + // Rebuild triggers + civicrm_api('system', 'flush', array('version' => 3, 'triggers' => 1)); + + CRM_Core_BAO_ConfigSetting::setEnabledComponents(array( + 'CiviEvent', + 'CiviContribute', + 'CiviMember', + 'CiviMail', + 'CiviReport', + 'CiviPledge', + )); + + return TRUE; + } + +} \ No newline at end of file diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index b6e06ff0da..745756927c 100755 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -245,7 +245,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { */ protected static function _populateDB($perClass = FALSE, &$object = NULL) { if (CIVICRM_UF !== 'UnitTests') { - throw new \RuntimeException("_populateDB requirs CIVICRM_UF=UnitTests"); + throw new \RuntimeException("_populateDB requires CIVICRM_UF=UnitTests"); } if ($perClass || $object == NULL) { @@ -260,97 +260,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } self::$populateOnce = NULL; - $dbName = self::getDBName(); - $pdo = self::$utils->pdo; - // only consider real tables and not views - $tables = $pdo->query("SELECT table_name FROM INFORMATION_SCHEMA.TABLES - WHERE TABLE_SCHEMA = '{$dbName}' AND TABLE_TYPE = 'BASE TABLE'"); - - $truncates = array(); - $drops = array(); - foreach ($tables as $table) { - // skip log tables - if (substr($table['table_name'], 0, 4) == 'log_') { - continue; - } - - // don't change list of installed extensions - if ($table['table_name'] == 'civicrm_extension') { - continue; - } - - if (substr($table['table_name'], 0, 14) == 'civicrm_value_') { - $drops[] = 'DROP TABLE ' . $table['table_name'] . ';'; - } - else { - $truncates[] = 'TRUNCATE ' . $table['table_name'] . ';'; - } - } - - $queries = array( - "USE {$dbName};", - "SET foreign_key_checks = 0", - // SQL mode needs to be strict, that's our standard - "SET SQL_MODE='STRICT_ALL_TABLES';", - "SET global innodb_flush_log_at_trx_commit = 2;", - ); - $queries = array_merge($queries, $truncates); - $queries = array_merge($queries, $drops); - foreach ($queries as $query) { - if (self::$utils->do_query($query) === FALSE) { - // failed to create test database - echo "failed to create test db."; - exit; - } - } - - // initialize test database - $sql_file2 = dirname(dirname(dirname(dirname(__FILE__)))) . "/sql/civicrm_data.mysql"; - $sql_file3 = dirname(dirname(dirname(dirname(__FILE__)))) . "/sql/test_data.mysql"; - $sql_file4 = dirname(dirname(dirname(dirname(__FILE__)))) . "/sql/test_data_second_domain.mysql"; - - $query2 = file_get_contents($sql_file2); - $query3 = file_get_contents($sql_file3); - $query4 = file_get_contents($sql_file4); - if (self::$utils->do_query($query2) === FALSE) { - echo "Cannot load civicrm_data.mysql. Aborting."; - exit; - } - if (self::$utils->do_query($query3) === FALSE) { - echo "Cannot load test_data.mysql. Aborting."; - exit; - } - if (self::$utils->do_query($query4) === FALSE) { - echo "Cannot load test_data.mysql. Aborting."; - exit; - } - - // done with all the loading, get transactions back - if (self::$utils->do_query("set global innodb_flush_log_at_trx_commit = 1;") === FALSE) { - echo "Cannot set global? Huh?"; - exit; - } - - if (self::$utils->do_query("SET foreign_key_checks = 1") === FALSE) { - echo "Cannot get foreign keys back? Huh?"; - exit; - } - - unset($query, $query2, $query3); - - // Rebuild triggers - civicrm_api('system', 'flush', array('version' => 3, 'triggers' => 1)); - - CRM_Core_BAO_ConfigSetting::setEnabledComponents(array( - 'CiviEvent', - 'CiviContribute', - 'CiviMember', - 'CiviMail', - 'CiviReport', - 'CiviPledge', - )); - - return TRUE; + return CiviTestDB::realPopulateDB(self::getDBName(), self::$utils); } public static function setUpBeforeClass() { @@ -469,15 +379,6 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { return $contactID; } - public function cleanDB() { - self::$populateOnce = NULL; - $this->DBResetRequired = TRUE; - - $this->_dbconn = $this->getConnection(); - static::_populateDB(); - $this->tempDirs = array(); - } - /** * Create default domain contacts for the two domains added during test class. * database population.