diff --git a/test/config/config.php b/test/config/config.php new file mode 100644 index 0000000000..df921468b6 --- /dev/null +++ b/test/config/config.php @@ -0,0 +1,5 @@ +test = new stdclass; +$config->test->dbPrefix = "db_"; +$config->test->rawDB = 'zentaounittest'; +$config->test->dbNum = 10; diff --git a/test/lib/db.class.php b/test/lib/db.class.php new file mode 100644 index 0000000000..9769f186d6 --- /dev/null +++ b/test/lib/db.class.php @@ -0,0 +1,150 @@ + + * @package ZenTaoPMS + * @version $Id: $ + * @link http://www.zentao.net/ + */ +class db +{ + /** + * global config + * + * @var object + * @access public + */ + public $config; + + /** + * global app + * + * @var object + * @access public + */ + public $app; + + /** + * switch DB file + * + * @var string + * @access public + */ + public $dbLogFile; + + /** + * init DB file + * + * @var string + * @access public + */ + public $sqlFile; + + /** + * global dao + * + * @var object + * @access public + */ + public $dao; + + /** + * __construct function load app config dao. + * + * @access public + * @return void + */ + public function __construct() + { + global $config, $app, $dao; + $this->config = $config; + $this->app = $app; + $this->dao = $dao; + $this->dbLogFile = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'test/tmp/dblog.php'; + $this->sqlFile = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'test/tmp/raw.sql'; + } + + /** + * switch database. + * + * @access public + * @return void + */ + function switchDB() + { + global $tester, $config; + $dbUsed = $this->getUsedDbList(); + $lastDB = 0; + if(!empty($dbUsed)) $lastDB = max($dbUsed); + if($lastDB == $this->config->test->dbNum) + { + shell_exec("> $this->dbLogFile"); + $this->initDB(); + $lastDB = 0; + } + + $dbSN = $lastDB + 1; + $dbName = $config->test->dbPrefix . $dbSN; + + $this->dao->query("use $dbName"); + + $tester = $this->app->loadCommon(); + $logHandle = fopen($this->dbLogFile, 'a'); + fwrite($logHandle, $dbSN . PHP_EOL); + fclose($logHandle); + } + + /** + * restore database. + * + * @access public + * @return void + */ + function restoreDB() + { + $this->dao->query("use " . $this->config->test->rawDB); + } + + + /** + * get used DbList. + * + * @access public + * @return array + */ + function getUsedDbList() + { + $dbUsed = explode("\n", file_get_contents($this->dbLogFile)); + $dbUsed = array_unique($dbUsed); + + foreach($dbUsed as $key => $db) + { + if(!is_numeric($db)) unset($dbUsed[$key]); + } + + return $dbUsed; + } + + + /** + * Init all test databases. + * + * @access public + * @return void + */ + function initDB() + { + for($i = 1; $i < 11; $i++) + { + $this->dao->query('DROP DATABASE ' . $this->config->test->dbPrefix . $i); + $this->dao->query('CREATE DATABASE ' . $this->config->test->dbPrefix . $i); + + shell_exec("mysql -u" . $this->config->db->user . ' -p' . $this->config->db->password . ' ' . $this->config->test->dbPrefix . $i . ' < ' . $this->sqlFile); + } + } +} diff --git a/test/lib/init.php b/test/lib/init.php index 6a56270c9c..344f2625d9 100644 --- a/test/lib/init.php +++ b/test/lib/init.php @@ -15,6 +15,7 @@ /* Set the error reporting. */ error_reporting(E_ALL & E_STRICT); +$testPath = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'test' . DIRECTORY_SEPARATOR; $frameworkRoot = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'framework' . DIRECTORY_SEPARATOR; /** @@ -51,6 +52,11 @@ $config->zendataRoot = dirname(dirname(__FILE__)) . '/zendata'; $config->ztfPath = dirname(dirname(__FILE__)) . '/tools/ztf'; $config->zdPath = dirname(dirname(__FILE__)) . '/tools/zd'; +/* init testDB. */ +include $testPath . 'config/config.php'; +include $testPath. 'lib/db.class.php'; +$db = new db(); + /** * Save variable to $_result. * diff --git a/test/lib/utils.php b/test/lib/utils.php index 86430cd9ef..8f933a7608 100644 --- a/test/lib/utils.php +++ b/test/lib/utils.php @@ -12,6 +12,7 @@ define('RUNTIME_ROOT', dirname(dirname(__FILE__)) . '/runtime/'); define('LIB_ROOT', dirname(dirname(__FILE__)) . '/lib/'); +define('TEST_BASEHPATH', dirname(__FILE__, 2)); include LIB_ROOT . 'init.php'; @@ -84,7 +85,6 @@ function zdRun() set_time_limit(0); try { - $versionType = getVersionType($config->version); $configRoot = $zdRoot . $versionType . '/'; include $configRoot . 'config.php'; include $configRoot . 'processor.php'; @@ -208,6 +208,40 @@ function zdRun() } } +/** + * copy init DB. + * + * @access public + * @return void + */ +function copyDB() +{ + global $config, $dao; + + $dumpCommand = "mysqldump -u%s -p%s %s > %s"; + $sqlFile = TEST_BASEHPATH . DS . 'tmp/raw.sql'; + + $currentDBNum = $dao->query("select count(*) from information_schema.SCHEMATA where SCHEMA_NAME like '" . $config->test->dbPrefix . "%'"); + $dumpCommand = sprintf($dumpCommand, $config->db->user, $config->db->password, $config->test->rawDB, $sqlFile); + shell_exec($dumpCommand); + + $dbNum = $config->test->dbNum; + + $dbUsed = array(); + for($i = 1; $i <= $dbNum; $i++) + { + $dbUsed[] = $config->test->dbPrefix . $i; + } + + foreach($dbUsed as $db) + { + if (!empty($currentDBNum)) $dao->query('drop database ' . $db); + $dao->query('CREATE DATABASE ' . $db); + shell_exec("mysql -u" . $config->db->user . ' -p' . $config->db->password . ' ' . $db . ' < ' . $sqlFile); + echo '数据库<' . $db . '>复制成功!'; + } +} + /** * Print usage of zdtest. * diff --git a/test/tmp/dblog.php b/test/tmp/dblog.php new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/ztest b/test/ztest index ef7d8cb240..51db9c2c3a 100755 --- a/test/ztest +++ b/test/ztest @@ -9,6 +9,9 @@ switch($argv[1]) case 'init': zdRun(); break; + case 'copyDB': + copyDB(); + break; case 'extract': ztfExtract('api'); ztfExtract('model');