From 02589ba46c0302328656c17f5c3424547468d3bc Mon Sep 17 00:00:00 2001 From: liyang <“liyang@easycorp.ltd”> Date: Mon, 27 Jun 2022 10:32:03 +0800 Subject: [PATCH 1/3] * Add switch DB. --- test/config/config.php | 5 ++ test/lib/db.class.php | 150 +++++++++++++++++++++++++++++++++++++++++ test/lib/init.php | 6 ++ test/lib/utils.php | 33 ++++++++- test/tmp/dblog.php | 0 test/ztest | 3 + 6 files changed, 195 insertions(+), 2 deletions(-) create mode 100644 test/config/config.php create mode 100644 test/lib/db.class.php create mode 100644 test/tmp/dblog.php 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 112f4153e3..d08ab930d1 100644 --- a/test/lib/init.php +++ b/test/lib/init.php @@ -12,6 +12,7 @@ * @version $Id: $ * @link http://www.zentao.net */ +$testPath = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'test' . DIRECTORY_SEPARATOR; /* Set the error reporting. */ error_reporting(E_ALL & E_STRICT); @@ -43,6 +44,7 @@ include $frameworkRoot . 'control.class.php'; include $frameworkRoot . 'model.class.php'; include $frameworkRoot . 'helper.class.php'; + $app = router::createApp('pms', dirname(dirname(__FILE__)), 'router'); $tester = $app->loadCommon(); @@ -51,6 +53,10 @@ $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 fbe3b538cc..d3aa784371 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'; @@ -71,7 +72,7 @@ function ztfExtract($dir) * @access public * @return void */ -function zdRun() +function zdRun($model = '') { global $config, $dao; @@ -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,35 @@ function zdRun() } } +function copyDB() +{ + global $config; + global $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 7eb3023bb7..c169e18479 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'); From 804fe254d0e8af9953a0d51b059f71563a3fda9c Mon Sep 17 00:00:00 2001 From: liyang <“liyang@easycorp.ltd”> Date: Mon, 27 Jun 2022 10:40:57 +0800 Subject: [PATCH 2/3] * Fix format. --- test/lib/init.php | 4 ++-- test/lib/utils.php | 17 +++++++++++------ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/test/lib/init.php b/test/lib/init.php index d08ab930d1..144b7790be 100644 --- a/test/lib/init.php +++ b/test/lib/init.php @@ -12,10 +12,10 @@ * @version $Id: $ * @link http://www.zentao.net */ -$testPath = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'test' . DIRECTORY_SEPARATOR; /* 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; /** @@ -44,7 +44,6 @@ include $frameworkRoot . 'control.class.php'; include $frameworkRoot . 'model.class.php'; include $frameworkRoot . 'helper.class.php'; - $app = router::createApp('pms', dirname(dirname(__FILE__)), 'router'); $tester = $app->loadCommon(); @@ -57,6 +56,7 @@ $config->zdPath = dirname(dirname(__FILE__)) . '/tools/zd'; 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 d3aa784371..8892627b00 100644 --- a/test/lib/utils.php +++ b/test/lib/utils.php @@ -208,22 +208,27 @@ function zdRun($model = '') } } +/** + * copy init DB. + * + * @access public + * @return void + */ function copyDB() { - global $config; - global $dao; + global $config, $dao; - $dumpCommand = "mysqldump -u%s -p%s %s > %s"; - $sqlFile = TEST_BASEHPATH . DS . 'tmp/raw.sql'; + $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); + $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++) + for($i = 1; $i <= $dbNum; $i++) { $dbUsed[] = $config->test->dbPrefix . $i; } From 1ab22702b4e0e3d9dff19d0aa1dcbb1b1d12ad7b Mon Sep 17 00:00:00 2001 From: liyang <“liyang@easycorp.ltd”> Date: Mon, 27 Jun 2022 10:45:02 +0800 Subject: [PATCH 3/3] * Fix bug for utils.php --- test/lib/utils.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/lib/utils.php b/test/lib/utils.php index 8892627b00..83d98eb51b 100644 --- a/test/lib/utils.php +++ b/test/lib/utils.php @@ -72,7 +72,7 @@ function ztfExtract($dir) * @access public * @return void */ -function zdRun($model = '') +function zdRun() { global $config, $dao;