* [misc] Enhance unit tests for repoModel::saveEffortForCommit() 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:32 +08:00
co-authored by Claude
parent 59f171adc6
commit 0eb5815143
2 changed files with 323 additions and 26 deletions
@@ -415,6 +415,196 @@ class repoTest
return $objects;
}
/**
* Test saveCommit method with mock data.
*
* @param int $repoID
* @param string $scmType
* @param int $version
* @access public
* @return mixed
*/
public function saveCommitWithMockDataTest(int $repoID, string $scmType = 'Git', int $version = 1)
{
global $dao;
// 模拟提交数据
$logs = array();
$logs['commits'] = array();
// 根据SCM类型创建不同的测试数据
if($scmType == 'Git')
{
for($i = 1; $i <= 3; $i++)
{
$commit = new stdclass();
$commit->revision = 'git-commit-' . $i . '-' . time();
$commit->committer = 'test-user-' . $i;
$commit->time = date('Y-m-d H:i:s', time() - (3600 * $i));
$commit->comment = 'Test commit message ' . $i;
$logs['commits'][] = $commit;
}
}
else if($scmType == 'Subversion')
{
for($i = 1; $i <= 2; $i++)
{
$commit = new stdclass();
$commit->revision = 'svn-r' . (1000 + $i);
$commit->committer = 'svn-user-' . $i;
$commit->time = date('Y-m-d H:i:s', time() - (3600 * $i));
$commit->comment = 'SVN test commit ' . $i;
$logs['commits'][] = $commit;
}
// 为SVN添加文件变更信息
$logs['files'] = array();
for($i = 0; $i < count($logs['commits']); $i++)
{
$files = array();
for($j = 1; $j <= 2; $j++)
{
$file = new stdclass();
$file->path = "/trunk/test/file{$i}_{$j}.php";
$file->action = $j % 2 == 0 ? 'M' : 'A';
$file->type = 'file';
$file->oldPath = '';
$files[] = $file;
}
$logs['files'][] = $files;
}
}
$count = $this->objectModel->saveCommit($repoID, $logs, $version);
if(dao::isError()) return dao::getError();
if($scmType == 'Subversion')
{
$result = array();
$result['count'] = $count;
$result['files'] = $this->objectModel->dao->select('*')->from(TABLE_REPOFILES)->where('repo')->eq($repoID)->fetchAll('id');
return $result;
}
return $count;
}
/**
* Test saveCommit method with empty data.
*
* @param int $repoID
* @access public
* @return int
*/
public function saveCommitWithEmptyDataTest(int $repoID)
{
$logs = array();
$logs['commits'] = array(); // 空提交数据
$count = $this->objectModel->saveCommit($repoID, $logs, 1);
if(dao::isError()) return dao::getError();
return $count;
}
/**
* Test saveCommit method with branch information.
*
* @param int $repoID
* @param string $scmType
* @param int $version
* @param string $branch
* @access public
* @return mixed
*/
public function saveCommitWithBranchTest(int $repoID, string $scmType = 'Git', int $version = 1, string $branch = '')
{
global $dao;
$logs = array();
$logs['commits'] = array();
// 创建2个带分支的提交
for($i = 1; $i <= 2; $i++)
{
$commit = new stdclass();
$commit->revision = 'branch-commit-' . $i . '-' . time();
$commit->committer = 'branch-user-' . $i;
$commit->time = date('Y-m-d H:i:s', time() - (1800 * $i));
$commit->comment = 'Branch test commit ' . $i;
$logs['commits'][] = $commit;
}
$count = $this->objectModel->saveCommit($repoID, $logs, $version, $branch);
if(dao::isError()) return dao::getError();
return $count;
}
/**
* Test saveCommit method with large data set.
*
* @param int $repoID
* @param string $scmType
* @param int $version
* @access public
* @return int
*/
public function saveCommitWithLargeDataTest(int $repoID, string $scmType = 'Git', int $version = 1)
{
$logs = array();
$logs['commits'] = array();
// 创建10个提交记录测试批量处理
for($i = 1; $i <= 10; $i++)
{
$commit = new stdclass();
$commit->revision = 'large-commit-' . $i . '-' . time();
$commit->committer = 'large-user-' . ($i % 3 + 1);
$commit->time = date('Y-m-d H:i:s', time() - (600 * $i));
$commit->comment = 'Large test commit ' . $i . ' with longer description for testing purposes';
$logs['commits'][] = $commit;
}
$count = $this->objectModel->saveCommit($repoID, $logs, $version);
if(dao::isError()) return dao::getError();
return $count;
}
/**
* Test saveCommit method with invalid data.
*
* @param int $repoID
* @param string $scmType
* @param int $version
* @access public
* @return int
*/
public function saveCommitWithInvalidDataTest(int $repoID, string $scmType = 'Git', int $version = 1)
{
$logs = array();
$logs['commits'] = array();
// 创建包含一些异常字段的提交数据,测试容错能力
$commit = new stdclass();
$commit->revision = 'invalid-commit-' . time();
$commit->committer = 'invalid-user';
$commit->time = date('Y-m-d H:i:s');
$commit->comment = 'Test commit with <script>alert("xss")</script> & special chars';
$logs['commits'][] = $commit;
$count = $this->objectModel->saveCommit($repoID, $logs, $version);
if(dao::isError()) return dao::getError();
return $count;
}
public function saveOneCommitTest(int $repoID, int $version, string $branch = '')
{
global $dao;
+133 -26
View File
@@ -1,22 +1,39 @@
#!/usr/bin/env php
<?php
include dirname(__FILE__, 5) . '/test/lib/init.php';
include dirname(__FILE__, 2) . '/lib/repo.unittest.class.php';
su('admin');
/**
title=测试 repoModel->saveEffortForCommit();
title=测试 repoModel::saveEffortForCommit();
timeout=0
cid=1
cid=0
- 工时计算
- 执行$result1
- 属性status @doing
- 属性consumed @11
- 属性left @3
- 执行$result2
- 属性status @doing
- 属性consumed @12
- 属性left @0
- 执行$result3
- 属性status @doing
- 属性consumed @10
- 属性left @5
- 执行$result4
- 属性status @doing
- 属性consumed @15
- 属性left @1
- 执行$result5
- 属性status @doing
- 属性consumed @13
- 属性left @0
*/
include dirname(__FILE__, 5) . '/test/lib/init.php';
include dirname(__FILE__, 2) . '/lib/repo.unittest.class.php';
su('admin');
zenData('task')->gen(10);
$bug = zenData('bug');
$bug->execution->range('0');
@@ -27,28 +44,118 @@ $repoID = 1;
$repoRoot = '';
$scm = 'gitlab';
$log = new stdclass();
$log->revision = '61e51cadb1aa21ef3d2b51e3f193be3cc19cfef6';
$log->committer = 'root';
$log->time = '2023-12-29 10:44:36';
$log->comment = 'Effort Task #8 Cost:1h Left:3h';
$log->author = 'user4';
$log->msg = 'Effort Task #8 Cost:1h Left:3h';
$log->date = '2023-12-29 10:44:36';
$log->files = array('M' => array('/README.md'));
$log->change = array('/README.md' => array('action' => 'M', 'kind' => 'file', 'oldPath' => ''));
$action = new stdclass();
$action->actor = 'user4';
$action->date = '2023-12-29 13:14:36';
$action->extra = $scm == 'svn' ? $log->revision : substr($log->revision, 0, 10);
$action->action = 'gitcommited';
global $app;
include($app->getModuleRoot() . '/repo/control.php');
$app->control = new repo();
$repo = new repoTest();
$repo->saveEffortForCommitTest($log, $action, $repoID);
$result = $tester->loadModel('task')->getById(8);
r($result) && p('status,consumed,left') && e('doing,11,3'); //工时计算
// 测试步骤1:基础工时记录(消耗1小时,剩余3小时)
$log1 = new stdclass();
$log1->revision = '61e51cadb1aa21ef3d2b51e3f193be3cc19cfef6';
$log1->committer = 'root';
$log1->time = '2023-12-29 10:44:36';
$log1->comment = 'Effort Task #8 Cost:1h Left:3h';
$log1->author = 'user4';
$log1->msg = 'Effort Task #8 Cost:1h Left:3h';
$log1->date = '2023-12-29 10:44:36';
$log1->files = array('M' => array('/README.md'));
$log1->change = array('/README.md' => array('action' => 'M', 'kind' => 'file', 'oldPath' => ''));
$action1 = new stdclass();
$action1->actor = 'user4';
$action1->date = '2023-12-29 13:14:36';
$action1->extra = $scm == 'svn' ? $log1->revision : substr($log1->revision, 0, 10);
$action1->action = 'gitcommited';
$repo->saveEffortForCommitTest($log1, $action1, $repoID);
$result1 = $tester->loadModel('task')->getById(8);
r($result1) && p('status,consumed,left') && e('doing,11,3');
// 测试步骤2:零剩余时间(消耗2小时,剩余0小时)
$log2 = new stdclass();
$log2->revision = '71e51cadb1aa21ef3d2b51e3f193be3cc19cfef7';
$log2->committer = 'admin';
$log2->time = '2023-12-29 11:00:00';
$log2->comment = 'Effort Task #9 Cost:2h Left:0h';
$log2->author = 'admin';
$log2->msg = 'Effort Task #9 Cost:2h Left:0h';
$log2->date = '2023-12-29 11:00:00';
$log2->files = array('M' => array('/src/main.php'));
$log2->change = array('/src/main.php' => array('action' => 'M', 'kind' => 'file', 'oldPath' => ''));
$action2 = new stdclass();
$action2->actor = 'admin';
$action2->date = '2023-12-29 14:00:00';
$action2->extra = $scm == 'svn' ? $log2->revision : substr($log2->revision, 0, 10);
$action2->action = 'gitcommited';
$repo->saveEffortForCommitTest($log2, $action2, $repoID);
$result2 = $tester->loadModel('task')->getById(9);
r($result2) && p('status,consumed,left') && e('doing,12,0');
// 测试步骤3:边界值测试(消耗0小时,剩余5小时)
$log3 = new stdclass();
$log3->revision = '81e51cadb1aa21ef3d2b51e3f193be3cc19cfef8';
$log3->committer = 'user1';
$log3->time = '2023-12-29 12:00:00';
$log3->comment = 'Effort Task #10 Cost:0h Left:5h';
$log3->author = 'user1';
$log3->msg = 'Effort Task #10 Cost:0h Left:5h';
$log3->date = '2023-12-29 12:00:00';
$log3->files = array('A' => array('/docs/README.txt'));
$log3->change = array('/docs/README.txt' => array('action' => 'A', 'kind' => 'file', 'oldPath' => ''));
$action3 = new stdclass();
$action3->actor = 'user1';
$action3->date = '2023-12-29 15:00:00';
$action3->extra = $scm == 'svn' ? $log3->revision : substr($log3->revision, 0, 10);
$action3->action = 'gitcommited';
$repo->saveEffortForCommitTest($log3, $action3, $repoID);
$result3 = $tester->loadModel('task')->getById(10);
r($result3) && p('status,consumed,left') && e('doing,10,5');
// 测试步骤4:高工时记录(消耗5小时,剩余1小时)
$log4 = new stdclass();
$log4->revision = '91e51cadb1aa21ef3d2b51e3f193be3cc19cfef9';
$log4->committer = 'user2';
$log4->time = '2023-12-29 13:00:00';
$log4->comment = 'Effort Task #1 Cost:5h Left:1h';
$log4->author = 'user2';
$log4->msg = 'Effort Task #1 Cost:5h Left:1h';
$log4->date = '2023-12-29 13:00:00';
$log4->files = array('M' => array('/lib/common.php'));
$log4->change = array('/lib/common.php' => array('action' => 'M', 'kind' => 'file', 'oldPath' => ''));
$action4 = new stdclass();
$action4->actor = 'user2';
$action4->date = '2023-12-29 16:00:00';
$action4->extra = $scm == 'svn' ? $log4->revision : substr($log4->revision, 0, 10);
$action4->action = 'gitcommited';
$repo->saveEffortForCommitTest($log4, $action4, $repoID);
$result4 = $tester->loadModel('task')->getById(1);
r($result4) && p('status,consumed,left') && e('doing,15,1');
// 测试步骤5:完整工时记录(消耗3小时,剩余0小时)
$log5 = new stdclass();
$log5->revision = 'a1e51cadb1aa21ef3d2b51e3f193be3cc19cfefa';
$log5->committer = 'user3';
$log5->time = '2023-12-29 14:00:00';
$log5->comment = 'Effort Task #2 Cost:3h Left:0h';
$log5->author = 'user3';
$log5->msg = 'Effort Task #2 Cost:3h Left:0h';
$log5->date = '2023-12-29 14:00:00';
$log5->files = array('M' => array('/config/config.php'));
$log5->change = array('/config/config.php' => array('action' => 'M', 'kind' => 'file', 'oldPath' => ''));
$action5 = new stdclass();
$action5->actor = 'user3';
$action5->date = '2023-12-29 17:00:00';
$action5->extra = $scm == 'svn' ? $log5->revision : substr($log5->revision, 0, 10);
$action5->action = 'gitcommited';
$repo->saveEffortForCommitTest($log5, $action5, $repoID);
$result5 = $tester->loadModel('task')->getById(2);
r($result5) && p('status,consumed,left') && e('doing,13,0');