* [misc] Enhance unit tests for sonarqubeModel::apiGetReport() method

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
liugang
2025-09-25 09:16:35 +08:00
co-authored by Claude
parent 267c846ae1
commit 397977422b
2 changed files with 63 additions and 25 deletions
@@ -313,4 +313,42 @@ class sonarqubeTest
$result = $this->objectModel->apiErrorHandling($response);
return $result;
}
/**
* Test apiGetReport method.
*
* @param int $sonarqubeID
* @param string $projectKey
* @param string $metricKeys
* @access public
* @return mixed
*/
public function apiGetReportTest($sonarqubeID, $projectKey, $metricKeys = '')
{
try {
$result = $this->objectModel->apiGetReport($sonarqubeID, $projectKey, $metricKeys);
} catch (Exception $e) {
return 'return empty';
} catch (TypeError $e) {
return 'return empty';
}
if(dao::isError()) return dao::getError();
if(empty($result)) return 'return empty';
// 处理错误情况
if(is_object($result) && isset($result->errors))
{
return $result->errors;
}
// 处理成功情况
if(is_object($result) && isset($result->component))
{
return $result;
}
return $result;
}
}
+25 -25
View File
@@ -4,37 +4,37 @@
/**
title=测试 sonarqubeModel::apiGetReport();
timeout=0
cid=0
- 通过错误的sonarqubeID、projectKey,查询sonarqube报告 @return empty
- 通过正确的sonarqubeID,不存在的projectKey查询sonarqube报告第0条的msg属性 @Component key 'wrong_project' not found
- 通过正确的sonarqubeID、projectKey,不正确的metricKeys查询sonarqube报告第0条的msg属性 @The following metric keys are not found: wrong_params
- 通过正确的sonarqubeID、projectKey、metricKeys,查询sonarqube报告 @success
- 步骤1:无效sonarqubeID和空projectKey @return empty
- 步骤2:不存在的sonarqubeID @return empty
- 步骤3:不存在的projectKey第0条的msg属性 @Component key 'wrong_project' not found
- 步骤4:无效的metricKeys第0条的msg属性 @The following metric keys are not found: invalid_metric
- 步骤5:使用默认metricKeys第component条的key属性 @unittest
- 步骤6:使用单个有效metricKeys第component条的key属性 @unittest
- 步骤7:使用多个有效metricKeys第component条的key属性 @unittest
*/
include dirname(__FILE__, 5) . '/test/lib/init.php';
// 1. 导入依赖
include dirname(__FILE__, 5) . '/test/lib/init.php';
include dirname(__FILE__, 2) . '/lib/sonarqube.unittest.class.php';
// 2. zendata数据准备
zenData('pipeline')->loadYaml('pipeline')->gen(5);
$sonarqube = $tester->loadModel('sonarqube');
// 3. 用户登录
su('admin');
$sonarqubeID = 0;
$projectKey = '';
$result = $sonarqube->apiGetReport($sonarqubeID, $projectKey);
if(empty($result)) $result = 'return empty';
r($result) && p() && e('return empty'); //通过错误的sonarqubeID、projectKey,查询sonarqube报告
// 4. 创建测试实例
$sonarqubeTest = new sonarqubeTest();
$sonarqubeID = 2;
$projectKey = 'wrong_project';
$result = $sonarqube->apiGetReport($sonarqubeID, $projectKey);
r($result->errors) && p('0:msg') && e("Component key 'wrong_project' not found"); //通过正确的sonarqubeID,不存在的projectKey查询sonarqube报告
$projectKey = 'unittest';
$metricKeys = 'wrong_params';
$result = $sonarqube->apiGetReport($sonarqubeID, $projectKey, $metricKeys);
r($result->errors) && p('0:msg') && e("The following metric keys are not found: wrong_params"); //通过正确的sonarqubeID、projectKey,不正确的metricKeys查询sonarqube报告
$metricKeys = 'bugs';
$result = $sonarqube->apiGetReport($sonarqubeID, $projectKey, $metricKeys);
if(!empty($result->component)) $result = 'success';
r($result) && p() && e('success'); //通过正确的sonarqubeID、projectKey、metricKeys,查询sonarqube报告
// 5. 测试步骤:必须包含至少5个测试步骤
r($sonarqubeTest->apiGetReportTest(0, '')) && p() && e('return empty'); // 步骤1:无效sonarqubeID和空projectKey
r($sonarqubeTest->apiGetReportTest(999, 'nonexistent')) && p() && e('return empty'); // 步骤2:不存在的sonarqubeID
r($sonarqubeTest->apiGetReportTest(2, 'wrong_project')) && p('0:msg') && e("Component key 'wrong_project' not found"); // 步骤3:不存在的projectKey
r($sonarqubeTest->apiGetReportTest(2, 'unittest', 'invalid_metric')) && p('0:msg') && e("The following metric keys are not found: invalid_metric"); // 步骤4:无效的metricKeys
r($sonarqubeTest->apiGetReportTest(2, 'unittest', '')) && p('component:key') && e('unittest'); // 步骤5:使用默认metricKeys
r($sonarqubeTest->apiGetReportTest(2, 'unittest', 'bugs')) && p('component:key') && e('unittest'); // 步骤6:使用单个有效metricKeys
r($sonarqubeTest->apiGetReportTest(2, 'unittest', 'bugs,coverage,vulnerabilities')) && p('component:key') && e('unittest'); // 步骤7:使用多个有效metricKeys