From 9682ec1923b753d9a89e64ffcefba7d1013a2432 Mon Sep 17 00:00:00 2001 From: liyang Date: Thu, 10 Nov 2022 11:16:34 +0800 Subject: [PATCH] * Add custom test data. --- test/lib/db.class.php | 33 ++++- test/lib/init.php | 2 + test/lib/utils.php | 16 ++- test/lib/yaml.class.php | 303 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 348 insertions(+), 6 deletions(-) create mode 100644 test/lib/yaml.class.php diff --git a/test/lib/db.class.php b/test/lib/db.class.php index 71a3b48d09..5d51e0de54 100644 --- a/test/lib/db.class.php +++ b/test/lib/db.class.php @@ -53,6 +53,14 @@ class db */ public $dao; + /** + * DB Config root. + * + * @var string + * @access public + */ + public $dbConfigRoot; + /** * __construct function load app config dao. * @@ -67,6 +75,8 @@ class db $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'; + + $this->dbConfigRoot = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'test/config/database/'; } /** @@ -145,7 +155,7 @@ class db { $this->dao->query('DROP DATABASE IF EXISTS ' . $this->config->test->dbPrefix . $i); $this->dao->query('CREATE DATABASE ' . $this->config->test->dbPrefix . $i); - if($this->config->db->host = 'localhost' and $this->config->db->port = '3306') + if($this->config->db->host == 'localhost' and $this->config->db->port == '3306') { shell_exec("mysql -u {$this->config->db->user} -p{$this->config->db->password} {$this->config->test->dbPrefix}{$i} < {$this->sqlFile}"); } @@ -155,4 +165,23 @@ class db } } } -} + + /** + * replace DB Config file. + * + * @param string $newDBfile(禅道项目config目录下的安装配置文件) + * @access public + * @return mixed + */ + function replaceDBConfig($newDBfile) + { + $replacedDBFile = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'config/my.php'; + shell_exec("sed -i '/db->name/d ; /db->host/d ; /db->port/d ; /db->user/d ; /db->password/d' {$replacedDBFile}"); + + $newDBCF = file($this->dbConfigRoot . $newDBfile); + foreach($newDBCF as $newDB) + { + file_put_contents($replacedDBFile, $newDB, FILE_APPEND); + } + } + } diff --git a/test/lib/init.php b/test/lib/init.php index ea50a9dc84..53aa259cb8 100644 --- a/test/lib/init.php +++ b/test/lib/init.php @@ -60,8 +60,10 @@ $config->zdPath = dirname(dirname(__FILE__)) . '/tools/zd'; /* init testDB. */ include $testPath . 'config/config.php'; include $testPath. 'lib/db.class.php'; +include $testPath. 'lib/yaml.class.php'; include $testPath. 'lib/rest.php'; $db = new db(); +$yaml = new yaml(); if(!empty($config->test->account) and !empty($config->test->password) and !empty($config->test->base)) { diff --git a/test/lib/utils.php b/test/lib/utils.php index baa2b68448..445168cb54 100755 --- a/test/lib/utils.php +++ b/test/lib/utils.php @@ -72,14 +72,22 @@ function ztfExtract($dir) * @access public * @return void */ -function zdRun() +function zdRun($dataVersion = '', $dbFile = '') { - global $config, $dao; + global $config, $dao, $db; $frameworkRoot = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'framework' . DIRECTORY_SEPARATOR; include LIB_ROOT . 'spyc.php'; - $zdRoot = dirname(dirname(__FILE__)) . '/data/'; + if($dataVersion && $dbFile) + { + $db->replaceDBConfig($dbFile); + $zdRoot = dirname(dirname(__FILE__)) . "/data/{$dataVersion}/"; + } + else + { + $zdRoot = dirname(dirname(__FILE__)) . '/data/'; + } $zdPath = RUNTIME_ROOT . 'zd'; set_time_limit(0); @@ -100,7 +108,7 @@ function zdRun() $tmpCommonPath = RUNTIME_ROOT . 'tmp/common'; if(file_exists($tmpCommonPath)) system("rm -rf $tmpCommonPath"); system("cp -r {$zdRoot}common $tmpCommonPath"); - system("find {$zdRoot}sql/ -type f -delete"); + if(file_exists("{$zdRoot}sql/")) system("find {$zdRoot}sql/ -type f -delete"); /* Generate SQL files. */ $tables = array(); diff --git a/test/lib/yaml.class.php b/test/lib/yaml.class.php new file mode 100644 index 0000000000..8e96353772 --- /dev/null +++ b/test/lib/yaml.class.php @@ -0,0 +1,303 @@ + + * @package ZenTaoPMS + * @version $Id: $ + * @link http://www.zentao.net/ + */ +class yaml +{ + /** + * Global config. + * + * @var object + * @access public + */ + public $config; + + /** + * Filed Arr. + * + * @var array + * @access public + */ + public $fieldArr = array(); + + /** + * Field. + * + * @var int + * @access private + */ + private $field; + + /** + * Yaml Dir root. + * + * @var int + * @access public + */ + public $yamlDir; + + /** + * __construct function load config. + * + * @access public + * @return void + */ + public function __construct() + { + global $config; + $this->config = $config; + $this->yamlDir = dirname(__FILE__, 2) . '/model'; + } + + /** + * Magic method, return fild. + * + * @param string $property_name + * @access protected + * @return object + */ + public function __get($property_name) + { + $this->setField($property_name); + return $this; + } + + /** + * Set yaml filed. + * + * @param string $value + * @access public + * @return object + */ + public function setField($value) + { + $this->fieldArr[$value] = array(); + $this->field = $value; + return $this; + } + + /** + * Set filed rang. + * + * @param string $range + * @access public + * @return object + */ + public function range($range) + { + $this->fieldArr[$this->field]['range'] = $range; + return $this; + } + + /** + * Set field prefix. + * + * @param string $prefix + * @access public + * @return object + */ + public function prefix($prefix) + { + $this->fieldArr[$this->field]['prefix'] = $prefix; + return $this; + } + + /** + * Set field postfix. + * + * @param string $postfix + * @access public + * @return object + */ + function postfix($postfix) + { + $this->fieldArr[$this->field]['postfix'] = $postfix; + return $this; + } + + /** + * Set field type. + * + * @param string $type + * @access public + * @return object + */ + function type($type) + { + $this->fieldArr[$this->field]['type'] = $type; + return $this; + } + + /** + * Set field format. + * + * @param string $format + * @access public + * @return object + */ + function format($format) + { + $this->fieldArr[$this->field]['format'] = $format; + return $this; + } + + /** + * set field fields. + * + * @param array $fields + * @access public + * @return object + */ + function fields($fields) + { + if(!is_array($fields)) + { + echo "fileds must be an array"; + return; + } + + $this->fieldArr[$this->field]['fields'] = $fields; + return $this; + } + + /** + * get field array value. + * + * @access public + * @return array + */ + function getField() + { + return $this->fieldArr; + } + + /** + * Assembly field generation rules. + * + * @param array $filedArr + * @access public + * @return array + */ + function setFieldRule($filedArr) + { + $ruleArr = array(); + $index = 0; + + foreach($filedArr as $filed => $rule) + { + $ruleArr[$index]['field'] = $filed; + + if(array_key_exists('fields', $rule)) + { + $ruleArr[$index]['fields'] = $this->setFieldRule($rule['fields']); + } + else + { + if(!empty($rule['range'])) $ruleArr[$index]['range'] = $rule['range']; + } + + if(!empty($rule['prefix'])) $ruleArr[$index]['prefix'] = $rule['prefix']; + if(!empty($rule['postfix'])) $ruleArr[$index]['postfix'] = $rule['postfix']; + if(!empty($rule['type'])) $ruleArr[$index]['type'] = $rule['type']; + if(!empty($rule['format'])) $ruleArr[$index]['format'] = $rule['format']; + $index++; + } + + return $ruleArr; + } + + /** + * Build yaml file. + * + * @param string $model + * @param string $name + * @param string $version + * @access public + * @return void + */ + function build($model, $name, $version = '') + { + if(!is_dir($this->yamlDir . "/{$model}/data")) mkdir($this->yamlDir . "/{$model}/data", 0700); + $yamlFile = $this->yamlDir . "/{$model}/data/{$name}.yaml"; + + $yamlDataArr = array(); + + $yamlDataArr['title'] = "zt_{$name}"; + $yamlDataArr['author'] = "auto_{$name}"; + $version ? $yamlDataArr['version'] = $version : $yamlDataArr['version'] = '1.0'; + + if(empty($this->fieldArr)) return; + $yamlDataArr['fields'] = $this->setFieldRule($this->fieldArr); + + yaml_emit_file($yamlFile, $yamlDataArr); + } + + /** + * Insert the data into database. + * + * @param string $model + * @param string $tableName + * @param int $rows + * @access public + * @return string + */ + function insertDB($model, $file, $tableName, $rows, $isClear = false) + { + $yamlFile = $this->yamlDir . "/{$model}/data/{$file}.yaml"; + $tableSqlDir = $this->yamlDir . "/{$model}/data/sql"; + + if(!is_dir($tableSqlDir)) mkdir($tableSqlDir, 0700); + $dumpCommand = "mysqldump -u%s -p%s -h%s -P%s %s %s > {$tableSqlDir}/{$tableName}.sql"; + + $runtimeRoot = dirname(dirname(__FILE__)) . '/runtime/'; + $zdPath = $runtimeRoot . 'zd'; + $configYaml = $runtimeRoot . 'tmp/config.yaml'; + + $tableName = $this->config->db->prefix . $tableName; + $dbName = $this->config->db->name; + $dbHost = $this->config->db->host; + $dbPort = $this->config->db->port; + $dbUser = $this->config->db->user; + $dbPWD = $this->config->db->password; + + $command = "$zdPath -c %s -d %s -n %d -t %s --trim -dns mysql://%s:%s@%s:%s/%s#utf8"; + if($isClear === true) $command .= ' --clear'; + $execYaml = sprintf($command, $configYaml, $yamlFile, $rows, $tableName, $dbUser, $dbPWD, $dbHost, $dbPort, $dbName); + $execDump = sprintf($dumpCommand, $dbUser, $dbPWD, $dbHost, $dbPort, $dbName, $tableName); + system($execDump); + system($execYaml); + } + + /** + * Restore table data. + * + * @param string $model + * @param string $tableName + * @access public + * @return mixed + */ + public function restoreTable($model, $tableName) + { + $tableSql = $this->yamlDir . "/{$model}/data/sql/$tableName.sql"; + if(!is_file($tableSql)) return false; + + $dbName = $this->config->db->name; + $dbHost = $this->config->db->host; + $dbPort = $this->config->db->port; + $dbUser = $this->config->db->user; + $dbPWD = $this->config->db->password; + + $command = "mysql -u%s -p%s -h%s -P%s %s < %s"; + $execRestore = sprintf($command, $dbUser, $dbPWD, $dbHost, $dbPort, $dbName, $tableSql); + system($execRestore); + } +}