+ [misc] Add unit tests for instanceModel::generatePipelineName() method

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
liugang
2025-09-15 15:06:17 +08:00
co-authored by Claude
parent 52afe60770
commit 61d617c5d1
3 changed files with 169 additions and 0 deletions
@@ -389,4 +389,19 @@ class instanceTest
// 测试3-5:devops应用但缺少必要的依赖模块或方法
return 'noSettingsMapping';
}
/**
* Test generatePipelineName method.
*
* @param object $instance
* @access public
* @return string
*/
public function generatePipelineNameTest(object $instance): string
{
$result = $this->objectModel->generatePipelineName($instance);
if(dao::isError()) return dao::getError();
return $result;
}
}
+92
View File
@@ -0,0 +1,92 @@
#!/usr/bin/env php
<?php
/**
title=测试 instanceModel::generatePipelineName();
timeout=0
cid=0
- 步骤1:新名称不存在,返回原名称 @newapp
- 步骤2:名称存在但版本不同,返回名称+版本 @testapp-2.0.0
- 步骤3:名称和版本都存在,返回名称+数字 @testapp-1
- 步骤4:空名称处理,返回0或null @0
- 步骤5:不同chart类型的处理 @uniqueapp
*/
// 1. 导入依赖(路径固定,不可修改)
include dirname(__FILE__, 5) . '/test/lib/init.php';
include dirname(__FILE__, 2) . '/lib/instance.unittest.class.php';
// 2. zendata数据准备(根据需要配置)
// 手动插入测试数据
global $tester;
$tester->dao->delete()->from(TABLE_PIPELINE)->exec();
$tester->dao->insert(TABLE_PIPELINE)->data(array(
'type' => 'gitlab',
'name' => 'testapp',
'url' => 'http://test1.com',
'account' => 'admin',
'password' => 'password123',
'token' => 'token123',
'private' => 'private123',
'createdBy' => 'system',
'createdDate' => '2024-01-01 10:00:00',
'editedBy' => 'system',
'editedDate' => '2024-01-01 11:00:00',
'deleted' => '0'
))->exec();
$tester->dao->insert(TABLE_PIPELINE)->data(array(
'type' => 'gitlab',
'name' => 'testapp-1.0.0',
'url' => 'http://test2.com',
'account' => 'admin',
'password' => 'password123',
'token' => 'token123',
'private' => 'private123',
'createdBy' => 'system',
'createdDate' => '2024-01-01 10:00:00',
'editedBy' => 'system',
'editedDate' => '2024-01-01 11:00:00',
'deleted' => '0'
))->exec();
// 3. 用户登录(选择合适角色)
su('admin');
// 4. 创建测试实例(变量名与模块名一致)
$instanceTest = new instanceTest();
// 5. 🔴 强制要求:必须包含至少5个测试步骤
// 创建测试实例对象
$instance1 = new stdclass;
$instance1->name = 'newapp';
$instance1->chart = 'gitlab';
$instance1->appVersion = '1.5.0';
$instance2 = new stdclass;
$instance2->name = 'testapp';
$instance2->chart = 'gitlab';
$instance2->appVersion = '2.0.0';
$instance3 = new stdclass;
$instance3->name = 'testapp';
$instance3->chart = 'gitlab';
$instance3->appVersion = '1.0.0';
$instance4 = new stdclass;
$instance4->name = '';
$instance4->chart = 'jenkins';
$instance4->appVersion = '1.0.0';
$instance5 = new stdclass;
$instance5->name = 'uniqueapp';
$instance5->chart = 'sonarqube';
$instance5->appVersion = '3.0.0';
r($instanceTest->generatePipelineNameTest($instance1)) && p() && e('newapp'); // 步骤1:新名称不存在,返回原名称
r($instanceTest->generatePipelineNameTest($instance2)) && p() && e('testapp-2.0.0'); // 步骤2:名称存在但版本不同,返回名称+版本
r($instanceTest->generatePipelineNameTest($instance3)) && p() && e('testapp-1'); // 步骤3:名称和版本都存在,返回名称+数字
r($instanceTest->generatePipelineNameTest($instance4)) && p() && e('0'); // 步骤4:空名称处理,返回0或null
r($instanceTest->generatePipelineNameTest($instance5)) && p() && e('uniqueapp'); // 步骤5:不同chart类型的处理
@@ -0,0 +1,62 @@
---
title: pipeline数据定义
desc: 用于generatePipelineName方法测试的pipeline表测试数据
author: Claude
version: 1.0
fields:
- field: id
note: pipeline ID
range: 1-10
prefix: ""
postfix: ""
loop: 0
format: ""
- field: type
note: pipeline类型
range: gitlab,jenkins,sonarqube,nexus
loop: 0
- field: name
note: pipeline名称
range: testapp,testapp-1.0.0,testapp-1,testapp-2,existing-app
loop: 0
- field: url
note: pipeline URL
range: http://test1.com,http://test2.com,http://test3.com,http://test4.com,http://test5.com
loop: 0
- field: account
note: 账户名
range: admin,user{4}
loop: 0
- field: password
note: 密码
range: password123{5}
loop: 0
- field: token
note: 访问令牌
range: token123{5}
loop: 0
- field: private
note: 私有标识
range: private123{5}
loop: 0
- field: createdBy
note: 创建者
range: system,admin{4}
loop: 0
- field: createdDate
note: 创建时间
range: 2024-01-01 10:00:00{10}
loop: 0
- field: editedBy
note: 编辑者
range: system,admin{4}
loop: 0
- field: editedDate
note: 编辑时间
range: 2024-01-01 11:00:00{10}
loop: 0
- field: deleted
note: 是否删除
range: 0{9},1
loop: 0