* webhook: refactor funcs, add unit test.

This commit is contained in:
sunguangming
2023-12-21 13:32:48 +08:00
parent 20d5d2a66f
commit 1f08a15615
5 changed files with 54 additions and 43 deletions
+14 -6
View File
@@ -12,30 +12,33 @@
class webhookModel extends model
{
/**
* 获取单个webhook。
* Get a webhook by id.
*
* @param int $id
* @access public
* @return object
*/
public function getByID($id)
public function getByID(int $id)
{
return $this->dao->select('*')->from(TABLE_WEBHOOK)->where('id')->eq($id)->fetch();
}
/**
* 获取某种类型的webhook。
* Get a webhook by type.
*
* @param string $type
* @access public
* @return object
*/
public function getByType($type)
public function getByType(string $type)
{
return $this->dao->select('*')->from(TABLE_WEBHOOK)->where('type')->eq($type)->andWhere('deleted')->eq('0')->fetch();
}
/**
* 获取已经绑定的用户。
* Get a webhook by type.
*
* @param int $webhookID
@@ -44,21 +47,25 @@ class webhookModel extends model
* @access public
* @return object
*/
public function getBindAccount($webhookID, $webhookType, $openID)
public function getBindAccount(int $webhookID, string $webhookType, string $openID)
{
return $this->dao->select('account')->from(TABLE_OAUTH)->where('providerID')->eq($webhookID)->andWhere('providerType')->eq($webhookType)->andWhere('openID')->eq($openID)->fetch('account');
return $this->dao->select('account')->from(TABLE_OAUTH)
->where('providerID')->eq($webhookID)
->andWhere('providerType')->eq($webhookType)
->andWhere('openID')->eq($openID)
->fetch('account');
}
/**
* 获取webhook列表。
* Get webhook list.
*
* @param string $orderBy
* @param object $pager
* @param bool $decode
* @access public
* @return array
*/
public function getList($orderBy = 'id_desc', $pager = null, $decode = true)
public function getList(string $orderBy = 'id_desc', object $pager = null): array
{
return $this->dao->select('*')->from(TABLE_WEBHOOK)
->where('deleted')->eq('0')
@@ -68,6 +75,7 @@ class webhookModel extends model
}
/**
* 获取日志列表。
* Get log list of a webhook.
*
* @param int $id
+5 -3
View File
@@ -4,14 +4,16 @@ include dirname(__FILE__, 5) . '/test/lib/init.php';
include dirname(__FILE__, 2) . '/webhook.class.php';
su('admin');
zdTable('oauth')->gen(10);
/**
title=测试 webhookModel->getBindAccount();
timeout=0
cid=1
pid=1
按条件查出id=1的关联用户 >> user3
按条件查出id=空时,关联的用户 >> 0
- 按条件查出id=1的关联用户 @user3
- 按条件查出id=空时,关联的用户 @0
*/
+11 -14
View File
@@ -4,30 +4,27 @@ include dirname(__FILE__, 5) . '/test/lib/init.php';
include dirname(__FILE__, 2) . '/webhook.class.php';
su('admin');
zdTable('webhook')->gen(2);
/**
title=测试 webhookModel->getByID();
timeout=0
cid=1
pid=1
通过ID查创建人 >> admin
通过ID查名字 >> 钉钉群机器人
查ID为2的创建人 >> admin
查ID为2的名字 >> 钉钉工作消息
传入不存在的情况 >> 0
- 通过ID查创建人属性createdBy @admin
- 通过ID查名字属性name @钉钉群机器人
- 查ID为2的创建人属性createdBy @admin
- 查ID为2的名字属性name @钉钉工作消息
- 传入不存在的情况 @0
*/
$webhook = new webhookTest();
$ID = array();
$ID[0] = 1;
$ID[1] = 2;
$ID[2] = 1111;
$result1 = $webhook->getByIDTest($ID[0]);
$result2 = $webhook->getByIDTest($ID[1]);
$result3 = $webhook->getByIDTest($ID[2]);
$result1 = $webhook->getByIDTest(1);
$result2 = $webhook->getByIDTest(2);
$result3 = $webhook->getByIDTest(1111);
r($result1) && p('createdBy') && e('admin'); //通过ID查创建人
r($result1) && p('name') && e('钉钉群机器人'); //通过ID查名字
+18 -16
View File
@@ -4,20 +4,22 @@ include dirname(__FILE__, 5) . '/test/lib/init.php';
include dirname(__FILE__, 2) . '/webhook.class.php';
su('admin');
zdTable('webhook')->gen(10);
/**
title=测试 webhookModel->getByType();
timeout=0
cid=1
pid=1
通过type为dinggroup查id >> 1
通过type为dinguser查id >> 2
通过type为wechatgroup查id >> 3
通过type为wechatuser查id >> 4
通过type为feishugroup查id >> 5
通过type为feishuuser查id >> 6
通过type为default查id >> 7
传入空的情况 >> 0
- 通过type为dinggroup查id属性id @1
- 通过type为dinguser查id属性id @2
- 通过type为wechatgroup查id属性id @3
- 通过type为wechatuser查id属性id @4
- 通过type为feishugroup查id属性id @5
- 通过type为feishuuser查id属性id @6
- 通过type为default查id属性id @7
- 传入空的情况属性id @0
*/
@@ -34,13 +36,13 @@ $type[6] = 'default';
$type[7] = '';
$result1 = $webhook->getByTypeTest($type[0]);
$result2 = $webhook->getbyTypetest($type[1]);
$result3 = $webhook->getbyTypetest($type[2]);
$result4 = $webhook->getbyTypetest($type[3]);
$result5 = $webhook->getbyTypetest($type[4]);
$result6 = $webhook->getbyTypetest($type[5]);
$result7 = $webhook->getbyTypetest($type[6]);
$result8 = $webhook->getbyTypetest($type[7]);
$result2 = $webhook->getbyTypeTest($type[1]);
$result3 = $webhook->getbyTypeTest($type[2]);
$result4 = $webhook->getbyTypeTest($type[3]);
$result5 = $webhook->getbyTypeTest($type[4]);
$result6 = $webhook->getbyTypeTest($type[5]);
$result7 = $webhook->getbyTypeTest($type[6]);
$result8 = $webhook->getbyTypeTest($type[7]);
r($result1) && p('id') && e('1'); //通过type为dinggroup查id
r($result2) && p('id') && e('2'); //通过type为dinguser查id
+6 -4
View File
@@ -4,14 +4,16 @@ include dirname(__FILE__, 5) . '/test/lib/init.php';
include dirname(__FILE__, 2) . '/webhook.class.php';
su('admin');
zdTable('webhook')->gen(10);
/**
title=测试 webhookModel->getList();
timeout=0
cid=1
pid=1
统计获取list里元素数量 >> 15
取出其中一个id >> 1
- 统计获取list里元素数量 @10
- 取出其中一个id第1条的id属性 @1
*/
@@ -30,5 +32,5 @@ $decode[1] = '';
$result1 = $webhook->getListTest($orderBy[0]);
r(count($result1)) && p() && e('15'); //统计获取list里元素数量
r(count($result1)) && p() && e('10'); //统计获取list里元素数量
r($result1) && p('1:id') && e('1'); //取出其中一个id