From f0501c1320713fa4725b402878f34452d505f5d5 Mon Sep 17 00:00:00 2001 From: liugang Date: Wed, 12 Nov 2025 09:17:56 +0800 Subject: [PATCH] + [misc] Add unit tests for gitlabZen::bindUsers() 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 --- module/gitlab/test/lib/zen.class.php | 15 ++++++-- module/gitlab/test/zen/bindusers.php | 56 ++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 3 deletions(-) create mode 100755 module/gitlab/test/zen/bindusers.php diff --git a/module/gitlab/test/lib/zen.class.php b/module/gitlab/test/lib/zen.class.php index d8528b829d..a54b1feef8 100644 --- a/module/gitlab/test/lib/zen.class.php +++ b/module/gitlab/test/lib/zen.class.php @@ -32,13 +32,22 @@ class gitlabZenTest extends baseTest require_once $app->getModulePath('', 'gitlab') . 'zen.php'; } - /* 通过反射调用 bindUsers 方法 */ + /* 使用反射创建 gitlabZen 实例,跳过构造函数 */ $reflection = new ReflectionClass('gitlabZen'); + $zenInstance = $reflection->newInstanceWithoutConstructor(); + + /* 初始化必要的属性 */ + $zenInstance->app = $app; + $zenInstance->config = $app->config; + $zenInstance->lang = $app->lang; + $zenInstance->dao = $app->loadClass('dao'); + + /* 通过反射调用 bindUsers 方法 */ $method = $reflection->getMethod('bindUsers'); $method->setAccessible(true); - /* 在 gitlab model 实例上调用 zen 方法 */ - $method->invoke($this->instance, $gitlabID, $users, $gitlabNames, $zentaoUsers); + /* 调用 zen 方法 */ + $method->invoke($zenInstance, $gitlabID, $users, $gitlabNames, $zentaoUsers); if(dao::isError()) return dao::getError(); diff --git a/module/gitlab/test/zen/bindusers.php b/module/gitlab/test/zen/bindusers.php new file mode 100755 index 0000000000..5bb06c7500 --- /dev/null +++ b/module/gitlab/test/zen/bindusers.php @@ -0,0 +1,56 @@ +#!/usr/bin/env php +loadYaml('user_bindusers', false, 2)->gen(10); +zenData('oauth')->loadYaml('oauth_bindusers', false, 2)->gen(5); + +su('admin'); + +/* 设置 methodName 避免 gitlab 控制器构造函数报错 */ +global $app; +$app->setMethodName('test'); + +$gitlabTest = new gitlabZenTest(); + +/* 准备测试数据 */ +$gitlabID = 1; + +/* 构造zentaoUsers数据 */ +$user1 = new stdClass(); +$user1->realname = '用户一'; +$user2 = new stdClass(); +$user2->realname = '用户二'; +$user3 = new stdClass(); +$user3->realname = '用户三'; +$user4 = new stdClass(); +$user4->realname = '用户四'; + +$zentaoUsers = array( + 'user1' => $user1, + 'user2' => $user2, + 'user3' => $user3, + 'user4' => $user4 +); + +r($gitlabTest->bindUsersTest($gitlabID, array(500 => 'user1'), array(500 => 'gitlab_user1'), $zentaoUsers)) && p('500:account') && e('user1'); // 测试新用户首次绑定,绑定成功 +r($gitlabTest->bindUsersTest($gitlabID, array(100 => 'user1'), array(100 => 'gitlab_user1'), $zentaoUsers)) && p('100:account') && e('user1'); // 测试用户已绑定且account未变化,不进行操作 +r($gitlabTest->bindUsersTest($gitlabID, array(100 => 'user2'), array(100 => 'gitlab_user1'), $zentaoUsers)) && p('100:account') && e('user2'); // 测试用户已绑定但account发生变化,先解绑再绑定 +r($gitlabTest->bindUsersTest($gitlabID, array(600 => ''), array(600 => 'gitlab_user2'), $zentaoUsers)) && p('600') && e('~~'); // 测试绑定空account的情况,跳过绑定 +r($gitlabTest->bindUsersTest($gitlabID, array(700 => 'user4', 800 => ''), array(700 => 'gitlab_user3', 800 => 'gitlab_user4'), $zentaoUsers)) && p('700:account') && e('user4'); // 测试混合场景:新绑定、更新绑定、跳过空值 \ No newline at end of file