From 419d889153ce98a0cbb262aff30e409ba0ce9503 Mon Sep 17 00:00:00 2001 From: liuyongkai Date: Sat, 6 May 2023 07:54:56 +0000 Subject: [PATCH] * Add get ztf report function. --- test/lib/coverage.php | 137 +++++++++++++++++++++--------------------- 1 file changed, 67 insertions(+), 70 deletions(-) diff --git a/test/lib/coverage.php b/test/lib/coverage.php index 252d3648db..4e612f5228 100644 --- a/test/lib/coverage.php +++ b/test/lib/coverage.php @@ -62,12 +62,15 @@ class coverage /** * Load saved traces from file. * + * @param string $key * @access public - * @return array + * @return array|string */ - private function loadTraceFromFile(): array + private function loadTraceFromFile(string $key = ''): array|string { - return json_decode(file_get_contents($this->traceFile), true); + $report = json_decode(file_get_contents($this->traceFile), true); + if($key == '') return $report; + return isset($report[$key]) ? $report[$key] : array(); } /** @@ -75,7 +78,6 @@ class coverage * * @access public * @return string - */ public function getTraceFile(): string { return $this->traceFile; @@ -90,7 +92,7 @@ class coverage */ private function mergeTraces(array $traces): array { - $savedTraces = $this->loadTraceFromFile(); + $savedTraces = $this->loadTraceFromFile('traces'); if(!is_array($savedTraces)) return $traces; @@ -132,7 +134,12 @@ class coverage $traces = $this->groupTraceByModule($traces); $traces = $this->mergeTraces($traces); - return file_put_contents($this->traceFile, json_encode($traces)); + $log = new stdclass; + $log->time = date('Y-m-d H:i:s'); + $log->ztfPath = getenv('ZTF_REPORT_DIR'); + $log->traces = $traces; + + return file_put_contents($this->traceFile, json_encode($log)); } /** @@ -332,72 +339,10 @@ EOT; public function genSummaryReport(string $module='', string $file=''): string { /* Get trace from file. */ - $traces = $this->loadTraceFromFile(); + $traces = $this->loadTraceFromFile('traces'); /* Generate report. */ - $reportHtml = ''; - $reportHtml .= '单元测试行覆盖率报告'; - $reportHtml .= <<'; - $reportHtml .= '

单元测试行覆盖率报告

'; + $reportHtml = empty($file) ? '' . PHP_EOL: '' . PHP_EOL; if(empty($file)) { $reportHtml .= $this->genModuleStatsReport($traces); @@ -412,6 +357,58 @@ STYLE; return $reportHtml; } + /** + * Get ztf report. + * + * @access public + * @return object + */ + public function getZtfReport(): object|false + { + $reportFile = $this->getZtfReportFile(); + if(!$reportFile) return false; + + $content = file_get_contents($reportFile); + $report = json_decode($content); + if(!is_object($report) || !isset($report->funcResult)) return false; + + $report->funcResult = ''; + $report->log = ''; + + $report->time = date('Y-m-d H:i:s', $report->endTime); + $report->passPercent = round($report->pass / $report->total * 100, 2); + $report->failPercent = round($report->fail / $report->total * 100, 2); + $report->skipPercent = round($report->skip / $report->total * 100, 2); + return $report; + } + + /** + * Get ztf report. + * + * @access public + * @return string|false + */ + public function getZtfReportFile(): string|false + { + $latestTime = 0; + $latestFile = ''; + $reportPath = $this->loadTraceFromFile('ztfPath'); + + exec("find $reportPath -type f -name result.json", $files, $returnCode); + if($returnCode !== 0 || empty($files)) return false; + + foreach($files as $file) + { + if(is_file($file) && filemtime($file) > $latestTime) + { + $latestFile = $file; + $latestTime = filemtime($file); + } + } + + return $latestFile; + } + /** * Get the effective lines of code. *