* Supply multiple config files.

This commit is contained in:
zhujinyong
2021-09-27 02:52:35 +08:00
parent eb04910243
commit e059381d7d
32 changed files with 1444 additions and 1747 deletions
+107 -29
View File
@@ -10,6 +10,27 @@
* @link http://www.zentao.net
*/
define('RUNTIME_ROOT', dirname(dirname(__FILE__)) . '/runtime/');
define('LIB_ROOT', dirname(dirname(__FILE__)) . '/lib/');
/**
* Get version type of ZenTaoPMS.
*
* @param $version
* @access public
* @return void
*/
function getVersionType($version)
{
global $config;
if(strpos($version, 'max') !== false) return 'max';
if(strpos($version, 'biz') !== false) return 'biz';
if(strpos($version, 'pro') !== false) return 'pro';
return 'zentao';
}
/**
* Run test cases of a directory using ztf.
*
@@ -21,70 +42,127 @@ function ztfRun($dir)
{
global $config;
$ztfPath = dirname(dirname(__FILE__)) . '/tools/ztf';
$ztfPath = RUNTIME_ROOT . 'ztf';
$command = "$ztfPath $dir";
system($command);
}
/**
* Run scripts of a directory using zendata.
* Extract comments to steps and expects.
*
* @param string $dir
* @access public
* @return void
*/
function zdRun($dir)
function ztfExtract($dir)
{
global $config;
$ztfPath = RUNTIME_ROOT . 'ztf';
$command = "$ztfPath extract $dir";
system($command);
}
/**
* Run yaml files of zendata.
*
* @access public
* @return void
*/
function zdRun()
{
$frameworkRoot = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'framework' . DIRECTORY_SEPARATOR;
include $frameworkRoot . 'helper.class.php';
include LIB_ROOT . 'spyc.php';
$zdRoot = dirname(dirname(__FILE__)) . '/' . $dir;
$zdPath = dirname(dirname(__FILE__)) . '/tools/zd';
$zdRoot = dirname(dirname(__FILE__)) . '/data/';
$zdPath = RUNTIME_ROOT . 'zd';
set_time_limit(0);
try
{
$config = new stdclass();
$config->db = new stdclass();
include dirname(dirname(dirname(__FILE__))) . '/config/config.php';
include $zdRoot . '/count.php';
include dirname(dirname(dirname(__FILE__))) . '/config/config.php';
$versionType = getVersionType($config->version);
$configRoot = $zdRoot . $versionType . '/';
include $configRoot . 'config.php';
/* Connect to MySQL. */
$dsn = "mysql:host={$config->db->host}:{$config->db->port};dbname={$config->db->name}";
$dbh = new PDO($dsn, $config->db->user, $config->db->password);
echo 'MySQL connect success' . PHP_EOL;
$command = "$zdPath -c $zdRoot/%s -n %s -o $zdRoot/sql/%s.sql -table %s";
$yamlFilePath = scandir($zdRoot);
foreach($yamlFilePath as $file)
{
if(!strpos($file, '.yaml')) continue;
$fileName = basename($file, '.yaml');
$count = isset($config->dataRows[$fileName]) ? $config->dataRows[$fileName] : 10;
$execYaml = sprintf($command, $file, $count, $fileName, $fileName);
exec($execYaml); // Create sql files.
echo 'Execute: ' . $file . PHP_EOL;
}
/* Export SQL files to mysql. */
$sqlDir = $zdRoot . '/sql/';
$sqlFilePath = scandir($sqlDir);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // 设置报错处理方式。
$dbh->exec("SET GLOBAL sql_mode = '';");
$dbh->exec("set names 'utf8'");
$dbh->beginTransaction();
$command = "$zdPath -c %s -d $zdRoot%s -n %s -o {$zdRoot}sql/%s.sql -table %s";
$tables = array();
foreach($builder as $key => $info)
{
/* Build zendata. */
$configData = array('fields' => array());
foreach($info['data'] as $fileKey => $fileName)
{
if($fileKey == 0) continue; // Skip default file.
$data = Spyc::YAMLLoad($configRoot . $fileName . '.yaml');
foreach($data['fields'] as $field)
{
$fieldName = $field['field'];
$fieldExist = false;
foreach($configData['fields'] as $key => $configField)
{
if($configField['field'] == $fieldName)
{
$configData['fields'][$key] = $field;
$fieldExist = true;
break;
}
}
if(!$fieldExist) $configData['fields'][] = $field;
}
}
/* Refresh runtime/tmp/config.yaml. */
$spyc = new Spyc();
$content = $spyc->dump($configData);
$yaml = fopen(RUNTIME_ROOT . 'tmp/config.yaml', 'w');
fwrite($yaml, $content);
fclose($yaml);
/* Create sql files. */
$defaultName = $info['data'][0];
$tableName = $config->db->prefix . $defaultName;
$execYaml = sprintf($command, RUNTIME_ROOT . 'tmp/config.yaml', $defaultName . '.yaml', $info['rows'], $fileName, $tableName);
system($execYaml);
echo 'Execute: ' . $fileName . PHP_EOL;
$tables[$tableName] = $tableName;
}
/* Truncate tables. */
foreach($tables as $table)
{
$dbh->exec('truncate table ' . $table);
}
/* Export SQL files to MySQL. */
$sqlDir = $zdRoot . '/sql/';
$sqlFilePath = scandir($sqlDir);
foreach($sqlFilePath as $file)
{
if(!strpos($file, '.sql')) continue;
$tableName = basename($file, '.sql');
$truncate = 'truncate table ' . $tableName;
$dbh->exec($truncate);
$sql = file_get_contents($sqlDir . $file);
$count = $dbh->exec($sql);
echo $tableName . ' insert rows ' . $count . PHP_EOL;