From a91f0419cc03370b5a3fd1fbc7c2ad8e8f84f35e Mon Sep 17 00:00:00 2001 From: zhaoke Date: Thu, 4 May 2023 09:40:30 +0800 Subject: [PATCH] * Add print errors when zendata insert mysql failed. --- .../test/model/getlistbycurrentuser.php | 11 +-- .../yaml/getlistbycurrentuser/project.yaml | 16 +++++ test/lib/init.php | 5 +- test/lib/yaml.class.php | 67 ++++++++++--------- 4 files changed, 62 insertions(+), 37 deletions(-) create mode 100644 module/project/test/model/yaml/getlistbycurrentuser/project.yaml diff --git a/module/project/test/model/getlistbycurrentuser.php b/module/project/test/model/getlistbycurrentuser.php index 1ed4729f55..2b2b07ceed 100644 --- a/module/project/test/model/getlistbycurrentuser.php +++ b/module/project/test/model/getlistbycurrentuser.php @@ -3,8 +3,8 @@ include dirname(__FILE__, 5) . "/test/lib/init.php"; su('admin'); -$project = zdTable('project'); -$project->gen(5); +$project = zdTable('project')->config('project'); +$project->gen(4); /** @@ -12,7 +12,7 @@ title=测试 projectModel->getListByCurrentUser(); timeout=0 cid=1 -- 执行projectModel模块的getListByCurrentUser方法 @1 +- 执行projectModel模块的getListByCurrentUser方法 @0 - 执行projectModel模块的getListByCurrentUser方法 @1 @@ -27,6 +27,9 @@ cid=1 global $tester; $projectModel = $tester->loadModel('project'); -r(count($projectModel->getListByCurrentUser())) && p() && e('1'); +$tester->app->user->admin = true; +$tester->app->config->vision = 'lite'; +r(count($projectModel->getListByCurrentUser())) && p() && e('0'); +$tester->app->config->vision = 'rnd'; r(count($projectModel->getListByCurrentUser())) && p() && e('1'); r($projectModel->getListByCurrentUser()) && p('11:id,name') && e('11,项目1'); \ No newline at end of file diff --git a/module/project/test/model/yaml/getlistbycurrentuser/project.yaml b/module/project/test/model/yaml/getlistbycurrentuser/project.yaml new file mode 100644 index 0000000000..ab7a7a5ae0 --- /dev/null +++ b/module/project/test/model/yaml/getlistbycurrentuser/project.yaml @@ -0,0 +1,16 @@ +title: zt_project +author: liuhong +version: "1.0" +fields: + - field: type + range: 'project,sprint,project,stage' + - field: status + range: 'wait,doing,closed,suspended' + - field: deleted + range: 0,1,1,0 + - field: vision + range: rnd,rnd,rnd,lite + - field: openedDate + range: "20220101 000000:1W" # 生成从2021年1月1日0时0分0秒到当前时间的数据,步长为1周 + type: timestamp + format: "YY/MM/DD hh:mm:ss" diff --git a/test/lib/init.php b/test/lib/init.php index e3ad2b4625..07d3e3308f 100644 --- a/test/lib/init.php +++ b/test/lib/init.php @@ -102,9 +102,6 @@ function p($keys = '', $delimiter = ',') { global $_result; - $_keys = $keys; - $_delimiter = $delimiter; - if(empty($_result)) return print(implode("\n", array_fill(0, substr_count($keys, $delimiter) + 1, 0)) . "\n"); if(is_array($_result) and isset($_result['code']) and $_result['code'] == 'fail') return print((string) $_result['message'] . "\n"); @@ -117,6 +114,8 @@ function p($keys = '', $delimiter = ',') foreach($parts as $part) { $values = getValues($_result, $part, $delimiter); + if(!is_array($values)) continue; + foreach($values as $value) echo $value . "\n"; } diff --git a/test/lib/yaml.class.php b/test/lib/yaml.class.php index 87bd834641..3d6e797a45 100644 --- a/test/lib/yaml.class.php +++ b/test/lib/yaml.class.php @@ -341,21 +341,6 @@ class yaml $this->insertDB($yamlFile, $this->tableName, $rows, $isClear); } - /** - * 打印zendata生成的sql和导入数据库产生的错误. - * Print sql and error. - * - * @param int $rows - * @param bool $isClear Truncate table if set isClear to true. - * @param bool $debug Print sql and error if set isClear to true. - * @access public - * @return void - */ - public function debug($rows, $isClear = true) - { - $yamlFile = $this->mergeYaml(); - $this->insertDB($yamlFile, $this->tableName, $rows, $isClear, true); - } /** * Insert the data into database. * @@ -363,11 +348,10 @@ class yaml * @param string $tableName * @param int $rows * @param bool $isClear Truncate table if set isClear to true. - * @param bool $debug * @access public * @return string */ - function insertDB($yamlFile, $tableName, $rows, $isClear = true, $debug = false) + function insertDB($yamlFile, $tableName, $rows, $isClear = true) { $tableSqlDir = "{$_SERVER['PWD']}/data/sql"; @@ -385,32 +369,55 @@ class yaml $dbUser = $this->config->db->user; $dbPWD = $this->config->db->password; - $command = "$zdPath -c %s -d %s -n %d -t %s -dns mysql://%s:%s@%s:%s/%s#utf8"; + $command = "mysql -u%s -p%s -h%s -P%s -D%s < %s"; $genSQL = "$zdPath -c %s -d %s -n %d -t %s -o %s"; if($isClear === true) { /* Truncate table to reset auto increment number. */ system(sprintf("mysql -u%s -p%s -h%s -P%s %s -e 'truncate %s' 2>/dev/null", $dbUser, $dbPWD, $dbHost, $dbPort, $dbName, $tableName)); - $command .= ' --clear'; } $sqlPath = "{$tableSqlDir}/{$tableName}_zd.sql"; - $execYaml = sprintf($command, $configYaml, $yamlFile, $rows, $tableName, $dbUser, $dbPWD, $dbHost, $dbPort, $dbName); + $execYaml = sprintf($command, $dbUser, $dbPWD, $dbHost, $dbPort, $dbName, $sqlPath); $execDump = sprintf($dumpCommand, $dbUser, $dbPWD, $dbHost, $dbPort, $dbName, $tableName); $execGenSQL = sprintf($genSQL, $configYaml, $yamlFile, $rows, $tableName, $sqlPath); system($execGenSQL); - if($debug) - { - echo "\n" . file_get_contents($sqlPath) . "\n"; - - $tmpl = "mysql -u%s -p%s -h%s -P%s -D%s < %s"; - system(sprintf($tmpl, $dbUser, $dbPWD, $dbHost, $dbPort, $dbName, $sqlPath)); - return; - } - system($execDump); - system($execYaml); + $stderr = ''; + $this->exec_with_stderr($execYaml, $stderr); + + if(empty($stderr)) return; + + $errors = explode(PHP_EOL, $stderr); + $errors = array_filter($errors, function($error) + { + return !empty($error) && !strpos($error, 'Using a password on the command line interface can be insecure'); + }); + + if(!empty($errors)) echo implode(PHP_EOL, $errors) . PHP_EOL; + } + + /** + * 执行系统命令,并捕捉stderror + * exec command and catch stderror. + * + * @param string $cmd + * @param string $stderr + * @access private + * @return string + */ + private function exec_with_stderr($cmd, &$stderr=null) + { + $proc = proc_open($cmd,array( + 1 => array('pipe','w'), + 2 => array('pipe','w'), + ),$pipes); + fclose($pipes[1]); + $stderr = stream_get_contents($pipes[2]); + fclose($pipes[2]); + + return proc_close($proc); } /**