From 53421d6093cc826869a4e3fc558420766fbc7ffb Mon Sep 17 00:00:00 2001
From: liyuchun <563917701@qq.com>
Date: Thu, 27 Oct 2022 13:48:51 +0800
Subject: [PATCH] * Add unit test.
---
module/repo/model.php | 34 +---------------------------------
test/class/repo.class.php | 16 ++++++++++------
test/model/repo/addlink.php | 9 ++++++++-
test/model/repo/gethistory.php | 20 ++++++++++++++++++--
4 files changed, 37 insertions(+), 42 deletions(-)
diff --git a/module/repo/model.php b/module/repo/model.php
index 9d245c2deb..208c350a23 100644
--- a/module/repo/model.php
+++ b/module/repo/model.php
@@ -1006,7 +1006,7 @@ class repoModel extends model
$logs = array_reverse($logs, true);
foreach($logs as $i => $log)
{
- if($lastInDB->revision == $log->revision)
+ if(isset($lastInDB->revision) and $lastInDB->revision == $log->revision)
{
unset($logs[$i]);
continue;
@@ -2107,38 +2107,6 @@ class repoModel extends model
->fetchPairs('path', 'product');
}
- /**
- * Sync the latest commit.
- *
- * @param int $repoID
- * @param string $branchID
- * @access public
- * @return void
- */
- public function syncCommit($repoID, $branchID)
- {
- $repo = $this->getRepoByID($repoID);
- $this->scm = $this->app->loadClass('scm');
- $this->scm->setEngine($repo);
-
- $latestInDB = $this->dao->select('DISTINCT t1.*')->from(TABLE_REPOHISTORY)->alias('t1')
- ->leftJoin(TABLE_REPOBRANCH)->alias('t2')->on('t1.id=t2.revision')
- ->where('t1.repo')->eq($repoID)
- ->beginIF($repo->SCM == 'Git' and $branchID)->andWhere('t2.branch')->eq($branchID)->fi()
- ->beginIF($repo->SCM == 'Gitlab' and $branchID)->andWhere('t2.branch')->eq($branchID)->fi()
- ->orderBy('t1.time desc')
- ->limit(1)
- ->fetch();
- $version = empty($latestInDB) ? 1 : $latestInDB->commit + 1;
- $revision = $version == 1 ? 'HEAD' : ($repo->SCM == 'Git' ? $latestInDB->commit : $latestInDB->revision);
-
- $logs = $this->scm->getCommits('since' . $revision, $this->config->repo->batchNum, $branchID);
- $commitCount = $this->saveCommit($repoID, $logs, $version, $branchID);
- $this->dao->update(TABLE_REPO)->set('commits=commits + ' . $commitCount)->where('id')->eq($repoID)->exec();
-
- $this->fixCommit($repoID);
- }
-
/**
* Get execution pairs.
*
diff --git a/test/class/repo.class.php b/test/class/repo.class.php
index f883ab0e73..1f3313337b 100644
--- a/test/class/repo.class.php
+++ b/test/class/repo.class.php
@@ -194,7 +194,7 @@ class repoTest
if(dao::isError()) return dao::getError();
- return $objects;
+ return !empty($objects) ? array_keys($objects) : 'empty';
}
public function getGitRevisionNameTest($revision, $commit)
@@ -386,13 +386,17 @@ class repoTest
return $objects;
}
- public function addLinkTest($matches, $method)
+ public function addLinkTest($comment, $type)
{
- $objects = $this->objectModel->addLink($matches, $method);
+ $rules = $this->objectModel->processRules();
+ $objectReg = '/' . $rules[$type . 'Reg'] . '/i';
+ if(preg_match_all($objectReg, $comment, $result))
+ {
+ $links = $this->objectModel->addLink($result, $type);
+ foreach($links as $link) return $link;
+ }
- if(dao::isError()) return dao::getError();
-
- return $objects;
+ return 'empty';
}
public function parseCommentTest($comment)
diff --git a/test/model/repo/addlink.php b/test/model/repo/addlink.php
index 60520df8c0..65fb341fec 100644
--- a/test/model/repo/addlink.php
+++ b/test/model/repo/addlink.php
@@ -14,4 +14,11 @@ pid=1
$repo = new repoTest();
-r($repo->addLinkTest()) && p() && e();
\ No newline at end of file
+$commentTask = 'Finish task #1.';
+$commentStory = 'Effort story #1 Cost 1h.';
+$commentBug = 'Fix bug #1.';
+$commentElse = 'Anything else.';
+r($repo->addLinkTest($commentTask, 'task')) && p('') && e("task #1"); //任务链接
+r($repo->addLinkTest($commentStory, 'story')) && p('') && e("story #1"); //需求链接
+r($repo->addLinkTest($commentBug, 'bug')) && p('') && e("bug #1"); //Bug链接
+r($repo->addLinkTest($commentElse, 'task')) && p('') && e("empty"); //无链接
diff --git a/test/model/repo/gethistory.php b/test/model/repo/gethistory.php
index 098ea77b2a..c4d5023998 100644
--- a/test/model/repo/gethistory.php
+++ b/test/model/repo/gethistory.php
@@ -12,6 +12,22 @@ pid=1
*/
-$repo = new repoTest();
+global $tester, $dao;
+$repo = $tester->loadModel('repo')->getRepoByID(1);
+$logs = $tester->repo->getUnsyncedCommits($repo);
+$revision = 1;
+$revisions = array();
+foreach($logs as $log)
+{
+ $tester->repo->saveOneCommit($repo->id, $log, $revision);
+ $revisions[] = $log->revision;
+ $revision ++;
+}
-r($repo->getHistoryTest()) && p() && e();
\ No newline at end of file
+$repoTest = new repoTest();
+
+r($repoTest->getHistoryTest(1, $revisions)) && p('1') && e('d5789b703dc09c44be1e89a991c3506de1f678e3');
+r($repoTest->getHistoryTest(1, array())) && p('') && e('empty');
+
+$dao->exec('truncate table zt_repohistory');
+$dao->exec('truncate table zt_repobranch');