* [misc] Enhance unit tests for sonarqubeModel::createProject() 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 cd5a3d562b
commit fe8a806da7
2 changed files with 67 additions and 18 deletions
@@ -52,20 +52,40 @@ class sonarqubeTest
public function createProjectTest($sonarqubeID, $post)
{
dao::$errors = array(); // 清空错误信息
$result = $this->objectModel->createProject($sonarqubeID, (object)$post);
$errors = dao::getError();
if($errors)
if($errors && is_array($errors))
{
if(current($errors['name']) == "项目标识的格式不正确。允许的字符为字母、数字、'-'、''、'.'和“:”,至少有一个非数字。") return 'return error';
if(current($errors['name']) == false) return 'return false';
if(current($errors['name']) == 'Could not create Project with key: "' . $post['projectKey'] . '". A similar key already exists: "' . $post['projectKey'] . '"') return true;
// 检查项目名称长度错误
if(isset($errors['projectName']) && is_array($errors['projectName']))
{
$nameError = current($errors['projectName']);
if(strpos($nameError, '项目名称') !== false && strpos($nameError, '255') !== false) return 'return false';
}
// 检查项目键长度错误
if(isset($errors['projectKey']) && is_array($errors['projectKey']))
{
$keyError = current($errors['projectKey']);
if(strpos($keyError, '项目键') !== false && strpos($keyError, '400') !== false) return 'return false';
}
// 检查API返回的格式错误
if(isset($errors['name']) && is_array($errors['name']))
{
$apiError = current($errors['name']);
if(strpos($apiError, '项目标识的格式不正确') !== false) return 'return error';
if($apiError == false) return 'return false';
if(strpos($apiError, 'Could not create Project') !== false) return true;
}
return $errors;
}
else
{
if(empty($result)) $result = 'return false';
return $result;
}
if(empty($result)) return 'return false';
return $result;
}
/**
@@ -356,4 +376,19 @@ class sonarqubeTest
return $result;
}
/**
* Test convertApiError method.
*
* @param array|string $message
* @access public
* @return string
*/
public function convertApiErrorTest($message)
{
$result = $this->objectModel->convertApiError($message);
if(dao::isError()) return dao::getError();
return $result;
}
}
+23 -9
View File
@@ -4,11 +4,16 @@
/**
title=测试 sonarqubeModel::createProject();
timeout=0
cid=0
- 使用错误项目数据,正确的sonarqubeID创建Sonarqube项目 @return error
- 使用正确的项目数据,空的sonarqubeID创建Sonarqube项目 @return false
- 使用正确的sonarqubeID和Sonarqube项目信息创建Sonarqube项目 @1
- 步骤1:正常数据创建项目 @1
- 步骤2:无效sonarqubeID创建项目 @return false
- 步骤3:项目键格式错误创建项目 @return error
- 步骤4:项目名称长度超限创建项目 @return false
- 步骤5:项目键长度超限创建项目第projectKey条的0属性 @『项目标识』长度应当不超过『400』
- 步骤6:空项目名称创建项目第name条的0属性 @The 'name' parameter is missing
- 步骤7:空项目键创建项目第name条的0属性 @The 'project' parameter is missing
*/
include dirname(__FILE__, 5) . '/test/lib/init.php';
@@ -19,11 +24,20 @@ zenData('pipeline')->loadYaml('pipeline')->gen(5);
$sonarqubeID = 2;
$empty_post = array('projectName' => '', 'projectKey' => '');
$error_post = array('projectName' => 'unit_test', 'projectKey' => '@#');
$true_post = array('projectName' => 'unit_test17', 'projectKey' => 'unittest17');
// 测试数据准备
$validProject = array('projectName' => 'unit_test_project', 'projectKey' => 'unittest_project');
$invalidKeyProject = array('projectName' => 'unit_test', 'projectKey' => '@#$invalid');
$longNameProject = array('projectName' => str_repeat('a', 256), 'projectKey' => 'valid_key');
$longKeyProject = array('projectName' => 'valid_name', 'projectKey' => str_repeat('a', 401));
$emptyNameProject = array('projectName' => '', 'projectKey' => 'valid_key');
$emptyKeyProject = array('projectName' => 'valid_name', 'projectKey' => '');
$sonarqube = new sonarqubeTest();
r($sonarqube->createProjectTest($sonarqubeID, $error_post)) && p() && e("return error"); //使用错误项目数据,正确的sonarqubeID创建Sonarqube项目
r($sonarqube->createProjectTest(0, $true_post)) && p() && e('return false'); //使用正确的项目数据,空的sonarqubeID创建Sonarqube项目
r($sonarqube->createProjectTest($sonarqubeID, $true_post)) && p() && e(1); //使用正确的sonarqubeID和Sonarqube项目信息创建Sonarqube项目
r($sonarqube->createProjectTest($sonarqubeID, $validProject)) && p() && e(1); // 步骤1:正常数据创建项目
r($sonarqube->createProjectTest(0, $validProject)) && p() && e('return false'); // 步骤2:无效sonarqubeID创建项目
r($sonarqube->createProjectTest($sonarqubeID, $invalidKeyProject)) && p() && e('return error'); // 步骤3:项目键格式错误创建项目
r($sonarqube->createProjectTest($sonarqubeID, $longNameProject)) && p() && e('return false'); // 步骤4:项目名称长度超限创建项目
r($sonarqube->createProjectTest($sonarqubeID, $longKeyProject)) && p('projectKey:0') && e('『项目标识』长度应当不超过『400』'); // 步骤5:项目键长度超限创建项目
r($sonarqube->createProjectTest($sonarqubeID, $emptyNameProject)) && p('name:0') && e("The 'name' parameter is missing"); // 步骤6:空项目名称创建项目
r($sonarqube->createProjectTest($sonarqubeID, $emptyKeyProject)) && p('name:0') && e("The 'project' parameter is missing"); // 步骤7:空项目键创建项目