From ffbb78e31cdbbc4bfffdf8c9ce749221a483000b Mon Sep 17 00:00:00 2001 From: liumengyi Date: Tue, 26 Dec 2023 15:30:10 +0800 Subject: [PATCH] * Refactor zanodeModel::getAutomationByID, and add its unit test. --- module/zanode/model.php | 12 ++--- .../zanode/test/model/getautomationbyid.php | 49 +++++++++++++++++++ 2 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 module/zanode/test/model/getautomationbyid.php diff --git a/module/zanode/model.php b/module/zanode/model.php index e07dbca69f..6920d1a8bb 100644 --- a/module/zanode/model.php +++ b/module/zanode/model.php @@ -908,16 +908,16 @@ class zanodemodel extends model } /** + * 通过 id 获取自动化设置。 * Get automation by id. * - * @param int $id - * @return object + * @param int $id + * @access public + * @return object|false */ - public function getAutomationByID($id) + public function getAutomationByID(int $id): object|bool { - return $this->dao->select('*')->from(TABLE_AUTOMATION) - ->where('id')->eq($id) - ->fetch(); + return $this->dao->select('*')->from(TABLE_AUTOMATION)->where('id')->eq($id)->fetch(); } /** diff --git a/module/zanode/test/model/getautomationbyid.php b/module/zanode/test/model/getautomationbyid.php new file mode 100644 index 0000000000..633a6bbb23 --- /dev/null +++ b/module/zanode/test/model/getautomationbyid.php @@ -0,0 +1,49 @@ +#!/usr/bin/env php +getAutomationByID(). +cid=1 + +- 测试获取 id 1 的自动化设置 + - 属性id @1 + - 属性product @1 + - 属性node @1 + - 属性scriptPath @scriptPath1 + - 属性shell @shell1 +- 测试获取 id 3 的自动化设置 + - 属性id @3 + - 属性product @2 + - 属性node @2 + - 属性scriptPath @scriptPath3 + - 属性shell @shell3 +- 测试获取 id 5 的自动化设置 + - 属性id @5 + - 属性product @3 + - 属性node @3 + - 属性scriptPath @scriptPath5 + - 属性shell @shell5 +- 测试获取 空的 id 0 的自动化设置 @0 +- 测试获取 不存在的 id 111 的自动化设置 @0 + + */ + +include dirname(__FILE__, 5) . '/test/lib/init.php'; +include dirname(__FILE__, 2) . '/zanode.class.php'; + +zdTable('user')->gen(10); +zdTable('automation')->gen(5); + +su('admin'); + +$zanode = new zanodeTest(); + +$id = array(1, 3, 5, 0, 111); + +r($zanode->getAutomationByID($id[0])) && p('id,product,node,scriptPath,shell') && e('1,1,1,scriptPath1,shell1'); // 测试获取 id 1 的自动化设置 +r($zanode->getAutomationByID($id[1])) && p('id,product,node,scriptPath,shell') && e('3,2,2,scriptPath3,shell3'); // 测试获取 id 3 的自动化设置 +r($zanode->getAutomationByID($id[2])) && p('id,product,node,scriptPath,shell') && e('5,3,3,scriptPath5,shell5'); // 测试获取 id 5 的自动化设置 +r($zanode->getAutomationByID($id[3])) && p() && e('0'); // 测试获取 空的 id 0 的自动化设置 +r($zanode->getAutomationByID($id[4])) && p() && e('0'); // 测试获取 不存在的 id 111 的自动化设置