From 38b4ec771da41c1bd7b34af2a054c8e4e0d55bce Mon Sep 17 00:00:00 2001 From: liugang Date: Sat, 13 Sep 2025 12:59:37 +0800 Subject: [PATCH] + [misc] Add unit tests for blockZen::getProjectsStatisticData() method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- .../block/test/lib/block.unittest.class.php | 93 +++++++++++++++++++ .../test/zen/getprojectsstatisticdata.php | 52 +++++++++++ 2 files changed, 145 insertions(+) create mode 100755 module/block/test/zen/getprojectsstatisticdata.php diff --git a/module/block/test/lib/block.unittest.class.php b/module/block/test/lib/block.unittest.class.php index 3d16dcb6a7..02df1788c4 100644 --- a/module/block/test/lib/block.unittest.class.php +++ b/module/block/test/lib/block.unittest.class.php @@ -5638,4 +5638,97 @@ class blockTest return $result; } + + /** + * Test getProjectsStatisticData method. + * + * @param array $projectIdList + * @access public + * @return array + */ + public function getProjectsStatisticDataTest($projectIdList = array()) + { + global $tester; + + include_once dirname(__FILE__, 3) . '/model.php'; + + if (!class_exists('block')) { + class_alias('blockModel', 'block'); + } + + include_once dirname(__FILE__, 3) . '/zen.php'; + + $blockZen = new blockZen(); + $blockZen->block = $this->objectModel; + + // 初始化必要的属性 + $blockZen->app = $tester->app; + $blockZen->config = $tester->app->config; + $blockZen->dao = $tester->dao; + + // 模拟metric模型 + $blockZen->metric = new stdclass(); + $blockZen->metric->getResultByCodeWithArray = function($code, $filters, $mode, $extra, $vision) use ($projectIdList) { + // 模拟不同类型的统计数据 + switch($code) { + case 'count_of_opened_risk_in_project': + $result = array(); + foreach($projectIdList as $id) { + $result[] = array('project' => $id, 'value' => rand(1, 10)); + } + return $result; + + case 'count_of_opened_issue_in_project': + $result = array(); + foreach($projectIdList as $id) { + $result[] = array('project' => $id, 'value' => rand(1, 8)); + } + return $result; + + case 'day_of_invested_in_project': + case 'consume_of_task_in_project': + case 'left_of_task_in_project': + case 'scale_of_story_in_project': + case 'count_of_finished_story_in_project': + case 'count_of_unclosed_story_in_project': + case 'count_of_task_in_project': + case 'count_of_wait_task_in_project': + case 'count_of_doing_task_in_project': + case 'count_of_bug_in_project': + case 'count_of_closed_bug_in_project ': + case 'count_of_activated_bug_in_project': + $result = array(); + foreach($projectIdList as $id) { + $result[] = array('project' => $id, 'value' => rand(5, 50)); + } + return $result; + + default: + return array(); + } + }; + + // 模拟loadModel方法 + $blockZen->loadModel = function($modelName) use ($tester, $blockZen) { + if($modelName == 'metric') { + return $blockZen->metric; + } + return $tester->loadModel($modelName); + }; + + // 使用反射调用私有方法 + $reflection = new ReflectionClass($blockZen); + $method = $reflection->getMethod('getProjectsStatisticData'); + $method->setAccessible(true); + + try { + $result = $method->invoke($blockZen, $projectIdList); + } catch (Exception $e) { + return array('error' => $e->getMessage()); + } + + if(dao::isError()) return dao::getError(); + + return $result; + } } diff --git a/module/block/test/zen/getprojectsstatisticdata.php b/module/block/test/zen/getprojectsstatisticdata.php new file mode 100755 index 0000000000..91fd68cd3e --- /dev/null +++ b/module/block/test/zen/getprojectsstatisticdata.php @@ -0,0 +1,52 @@ +#!/usr/bin/env php +id->range('1-10'); +$project->name->range('项目1,项目2,项目3,项目4,项目5,项目6,项目7,项目8,项目9,项目10'); +$project->model->range('scrum{3},waterfall{2},kanban{2},agileplus{3}'); +$project->type->range('project{5},sprint{3},stage{2}'); +$project->status->range('wait{2},doing{5},suspended{1},done{2}'); +$project->deleted->range('0{9},1{1}'); +$project->gen(10); + +// 3. 用户登录(选择合适角色) +su('admin'); + +// 4. 创建测试实例(变量名与模块名一致) +$blockTest = new blockTest(); + +// 5. 🔴 强制要求:必须包含至少5个测试步骤 +$result1 = $blockTest->getProjectsStatisticDataTest(array(1)); +r(count($result1['riskCountGroup']) >= 0 && count($result1['issueCountGroup']) >= 0) && p() && e('1'); // 步骤1:单个项目ID测试 + +$result2 = $blockTest->getProjectsStatisticDataTest(array(1, 2, 3)); +r(is_array($result2) && isset($result2['riskCountGroup'])) && p() && e('1'); // 步骤2:多个项目ID测试 + +$result3 = $blockTest->getProjectsStatisticDataTest(array()); +r(is_array($result3) && isset($result3['riskCountGroup'])) && p() && e('1'); // 步骤3:空数组测试 + +$result4 = $blockTest->getProjectsStatisticDataTest(array(999)); +r(is_array($result4) && isset($result4['riskCountGroup'])) && p() && e('1'); // 步骤4:无效项目ID测试 + +$result5 = $blockTest->getProjectsStatisticDataTest(array(1)); +r(isset($result5['investedGroup']) && isset($result5['consumeTaskGroup'])) && p() && e('1'); // 步骤5:验证敏捷项目统计字段 \ No newline at end of file