From 2c9d7d64776bdf3434211250cce9c41e5bb0adea Mon Sep 17 00:00:00 2001 From: liumengyi Date: Wed, 13 Sep 2023 13:59:56 +0800 Subject: [PATCH] * Refactor testcaseModel::buildMenuQuery, and add its unit test. --- module/testcase/model.php | 18 +++--- module/testcase/test/model/buildmenuquery.php | 56 +++++++++++++++++++ module/testcase/test/testcase.class.php | 18 ++++++ module/testcase/test/yaml/modulescene.yaml | 13 +++++ 4 files changed, 96 insertions(+), 9 deletions(-) create mode 100644 module/testcase/test/model/buildmenuquery.php create mode 100644 module/testcase/test/yaml/modulescene.yaml diff --git a/module/testcase/model.php b/module/testcase/model.php index 676f9c390e..f5e42de489 100644 --- a/module/testcase/model.php +++ b/module/testcase/model.php @@ -1866,33 +1866,33 @@ class testcaseModel extends model } /** + * 构建菜单查询。 * Build menu query. * * @param int $rootID * @param int $moduleID - * @param string $type * @param int $startScene * @param string $branch * @access public - * @return object + * @return string */ - public function buildMenuQuery($rootID, $moduleID, $type, $startScene = 0, $branch = 'all') + public function buildMenuQuery(int $rootID, int $moduleID, int $startScene = 0, string $branch = 'all'): string { /* Set the start module. */ $startScenePath = ''; if($startScene > 0) { - $startScene = $this->dao->findById((int)$startScene)->from(VIEW_SCENECASE)->fetch(); + $startScene = $this->getSceneByID($startScene); if($startScene) $startScenePath = $startScene->path . '%'; } - return $this->dao->select('*')->from(VIEW_SCENECASE) + /* Return scene query. */ + return $this->dao->select('*')->from(TABLE_SCENE) ->where('deleted')->eq(0) - ->beginIF($rootID)->andWhere('product')->eq((int)$rootID)->fi() - ->beginIF(intval($moduleID) > 0)->andWhere('module')->eq((int)$moduleID)->fi() + ->beginIF($rootID)->andWhere('product')->eq($rootID)->fi() + ->beginIF($moduleID > 0)->andWhere('module')->eq($moduleID)->fi() ->beginIF($startScenePath)->andWhere('path')->like($startScenePath)->fi() ->beginIF($branch !== 'all' and $branch !== '' and $branch !== false)->andWhere('branch')->eq((int)$branch)->fi() - ->andWhere('isCase')->eq(2) ->orderBy('grade desc, sort') ->get(); } @@ -2268,7 +2268,7 @@ class testcaseModel extends model foreach($branches as $branchID => $branch) { $scenes = array(); - $stmt = $this->dbh->query($this->buildMenuQuery($rootID, $moduleID, $type, $startScene, $branchID)); + $stmt = $this->dbh->query($this->buildMenuQuery($rootID, $moduleID, $startScene, $branchID)); while($scene = $stmt->fetch()) { if ($scene->id != $currentScene) $scenes[$scene->id] = $scene; diff --git a/module/testcase/test/model/buildmenuquery.php b/module/testcase/test/model/buildmenuquery.php new file mode 100644 index 0000000000..2280bae4f6 --- /dev/null +++ b/module/testcase/test/model/buildmenuquery.php @@ -0,0 +1,56 @@ +#!/usr/bin/env php +config('modulescene')->gen('170'); +zdTable('user')->gen('1'); + +su('admin'); + +/** + +title=测试 testcaseModel->buildMenuQuery(); +cid=1 +pid=1 + +*/ + +$productIdList = array(0, 1, 2, 41); +$moduleIdList = array(0, 1821, 1825); +$sceneIdList = array(0, 1, 6, 161); +$branchIdList = array('all', '', '0'); + +$testcase = new testcaseTest(); + +r($testcase->buildMenuQueryTest($productIdList[0], $moduleIdList[0], $sceneIdList[0])) && p() && e("SELECT * FROM `zt_scene` WHERE `deleted` = '0' ORDER BY `grade` desc,`sort`"); // 测试获取产品 0 模块 0 起始场景 0 的查询语句 +r($testcase->buildMenuQueryTest($productIdList[0], $moduleIdList[0], $sceneIdList[0], $branchIdList[0])) && p() && e("SELECT * FROM `zt_scene` WHERE `deleted` = '0' ORDER BY `grade` desc,`sort`"); // 测试获取产品 0 模块 0 起始场景 0 分支 all 的查询语句 +r($testcase->buildMenuQueryTest($productIdList[0], $moduleIdList[0], $sceneIdList[0], $branchIdList[1])) && p() && e("SELECT * FROM `zt_scene` WHERE `deleted` = '0' ORDER BY `grade` desc,`sort`"); // 测试获取产品 0 模块 0 起始场景 0 分支 '' 的查询语句 +r($testcase->buildMenuQueryTest($productIdList[0], $moduleIdList[0], $sceneIdList[0], $branchIdList[2])) && p() && e("SELECT * FROM `zt_scene` WHERE `deleted` = '0' AND `branch` = '0' ORDER BY `grade` desc,`sort`"); // 测试获取产品 0 模块 0 起始场景 0 分支 0 的查询语句 + +r($testcase->buildMenuQueryTest($productIdList[0], $moduleIdList[0], $sceneIdList[1])) && p() && e("SELECT * FROM `zt_scene` WHERE `deleted` = '0' AND `path` LIKE '%' ORDER BY `grade` desc,`sort`"); // 测试获取产品 0 模块 0 起始场景 1 的查询语句 +r($testcase->buildMenuQueryTest($productIdList[0], $moduleIdList[0], $sceneIdList[1], $branchIdList[2])) && p() && e("SELECT * FROM `zt_scene` WHERE `deleted` = '0' AND `path` LIKE '%' AND `branch` = '0' ORDER BY `grade` desc,`sort`"); // 测试获取产品 0 模块 0 起始场景 1 分支 0 的查询语句 + +r($testcase->buildMenuQueryTest($productIdList[0], $moduleIdList[0], $sceneIdList[2])) && p() && e("SELECT * FROM `zt_scene` WHERE `deleted` = '0' AND `path` LIKE '%' ORDER BY `grade` desc,`sort`"); // 测试获取产品 0 模块 0 起始场景 6 的查询语句 +r($testcase->buildMenuQueryTest($productIdList[0], $moduleIdList[0], $sceneIdList[2], $branchIdList[2])) && p() && e("SELECT * FROM `zt_scene` WHERE `deleted` = '0' AND `path` LIKE '%' AND `branch` = '0' ORDER BY `grade` desc,`sort`"); // 测试获取产品 0 模块 0 起始场景 6 分支 0 的查询语句 + + +r($testcase->buildMenuQueryTest($productIdList[1], $moduleIdList[0], $sceneIdList[0])) && p() && e("SELECT * FROM `zt_scene` WHERE `deleted` = '0' AND `product` = '1' ORDER BY `grade` desc,`sort`"); // 测试获取产品 1 模块 0 起始场景 0 的查询语句 +r($testcase->buildMenuQueryTest($productIdList[1], $moduleIdList[0], $sceneIdList[0], $branchIdList[0])) && p() && e("SELECT * FROM `zt_scene` WHERE `deleted` = '0' AND `product` = '1' ORDER BY `grade` desc,`sort`"); // 测试获取产品 1 模块 0 起始场景 0 分支 all 的查询语句 +r($testcase->buildMenuQueryTest($productIdList[1], $moduleIdList[0], $sceneIdList[0], $branchIdList[1])) && p() && e("SELECT * FROM `zt_scene` WHERE `deleted` = '0' AND `product` = '1' ORDER BY `grade` desc,`sort`"); // 测试获取产品 1 模块 0 起始场景 0 分支 '' 的查询语句 +r($testcase->buildMenuQueryTest($productIdList[1], $moduleIdList[0], $sceneIdList[0], $branchIdList[2])) && p() && e("SELECT * FROM `zt_scene` WHERE `deleted` = '0' AND `product` = '1' AND `branch` = '0' ORDER BY `grade` desc,`sort`"); // 测试获取产品 1 模块 0 起始场景 0 分支 0 的查询语句 + +r($testcase->buildMenuQueryTest($productIdList[1], $moduleIdList[0], $sceneIdList[1])) && p() && e("SELECT * FROM `zt_scene` WHERE `deleted` = '0' AND `product` = '1' AND `path` LIKE '%' ORDER BY `grade` desc,`sort`"); // 测试获取产品 1 模块 0 起始场景 1 的查询语句 +r($testcase->buildMenuQueryTest($productIdList[1], $moduleIdList[0], $sceneIdList[1], $branchIdList[2])) && p() && e("SELECT * FROM `zt_scene` WHERE `deleted` = '0' AND `product` = '1' AND `path` LIKE '%' AND `branch` = '0' ORDER BY `grade` desc,`sort`"); // 测试获取产品 1 模块 0 起始场景 1 分支 0 的查询语句 + +r($testcase->buildMenuQueryTest($productIdList[1], $moduleIdList[1], $sceneIdList[1])) && p() && e("SELECT * FROM `zt_scene` WHERE `deleted` = '0' AND `product` = '1' AND `module` = '1821' AND `path` LIKE '%' ORDER BY `grade` desc,`sort`"); // 测试获取产品 1 模块 1821 起始场景 1 的查询语句 +r($testcase->buildMenuQueryTest($productIdList[1], $moduleIdList[1], $sceneIdList[1], $branchIdList[2])) && p() && e("SELECT * FROM `zt_scene` WHERE `deleted` = '0' AND `product` = '1' AND `module` = '1821' AND `path` LIKE '%' AND `branch` = '0' ORDER BY `grade` desc,`sort`"); // 测试获取产品 1 模块 1821 起始场景 1 分支 0 的查询语句 + + +r($testcase->buildMenuQueryTest($productIdList[1], $moduleIdList[0], $sceneIdList[0])) && p() && e("SELECT * FROM `zt_scene` WHERE `deleted` = '0' AND `product` = '1' ORDER BY `grade` desc,`sort`"); // 测试获取产品 41 模块 0 起始场景 0 的查询语句 +r($testcase->buildMenuQueryTest($productIdList[1], $moduleIdList[0], $sceneIdList[0], $branchIdList[0])) && p() && e("SELECT * FROM `zt_scene` WHERE `deleted` = '0' AND `product` = '1' ORDER BY `grade` desc,`sort`"); // 测试获取产品 41 模块 0 起始场景 0 分支 all 的查询语句 +r($testcase->buildMenuQueryTest($productIdList[1], $moduleIdList[0], $sceneIdList[0], $branchIdList[1])) && p() && e("SELECT * FROM `zt_scene` WHERE `deleted` = '0' AND `product` = '1' ORDER BY `grade` desc,`sort`"); // 测试获取产品 41 模块 0 起始场景 0 分支 '' 的查询语句 +r($testcase->buildMenuQueryTest($productIdList[1], $moduleIdList[0], $sceneIdList[0], $branchIdList[2])) && p() && e("SELECT * FROM `zt_scene` WHERE `deleted` = '0' AND `product` = '1' AND `branch` = '0' ORDER BY `grade` desc,`sort`"); // 测试获取产品 41 模块 0 起始场景 0 分支 0 的查询语句 + +r($testcase->buildMenuQueryTest($productIdList[1], $moduleIdList[0], $sceneIdList[1])) && p() && e("SELECT * FROM `zt_scene` WHERE `deleted` = '0' AND `product` = '1' AND `path` LIKE '%' ORDER BY `grade` desc,`sort`"); // 测试获取产品 41 模块 0 起始场景 161 的查询语句 +r($testcase->buildMenuQueryTest($productIdList[1], $moduleIdList[0], $sceneIdList[1], $branchIdList[2])) && p() && e("SELECT * FROM `zt_scene` WHERE `deleted` = '0' AND `product` = '1' AND `path` LIKE '%' AND `branch` = '0' ORDER BY `grade` desc,`sort`"); // 测试获取产品 41 模块 0 起始场景 161 分支 0 的查询语句 diff --git a/module/testcase/test/testcase.class.php b/module/testcase/test/testcase.class.php index 8cfda633d3..d817278177 100644 --- a/module/testcase/test/testcase.class.php +++ b/module/testcase/test/testcase.class.php @@ -1258,4 +1258,22 @@ class testcaseTest if(dao::isError()) return dao::getError(); return $summary; } + + /** + * 测试构建菜单查询。 + * Test build menu query. + * + * @param int $rootID + * @param int $moduleID + * @param int $startScene + * @param string $branch + * @access public + * @return string + */ + public function buildMenuQueryTest(int $rootID, int $moduleID, int $startScene = 0, string $branch = 'all'): string + { + $query = $this->objectModel->buildMenuQuery($rootID, $moduleID, $startScene, $branch); + if(dao::isError()) return dao::getError(); + return $query; + } } diff --git a/module/testcase/test/yaml/modulescene.yaml b/module/testcase/test/yaml/modulescene.yaml new file mode 100644 index 0000000000..69a35f42e2 --- /dev/null +++ b/module/testcase/test/yaml/modulescene.yaml @@ -0,0 +1,13 @@ +title: table zt_scene +desc: "用例" +author: Mengyi Liu +version: "1.0" +fields: + - field: id + range: 1-1000 + - field: module + range: 1821,1822,0,0,1825,1827,0,0,0,1829,1832,0,0,0,0{10000} + - field: product + range: 1-100{4} + - field: branch + range: 0{160},1-160{2},0{1000}