From 35faf64ed4da52bbcc27fdd6d783248faf87a246 Mon Sep 17 00:00:00 2001 From: liugang Date: Tue, 9 Sep 2025 21:19:30 +0800 Subject: [PATCH] + [misc] Add unit tests for storeModel::__construct() 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 --- .../store/test/lib/store.unittest.class.php | 45 +++++++++++++++ module/store/test/model/__construct.php | 57 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100755 module/store/test/model/__construct.php diff --git a/module/store/test/lib/store.unittest.class.php b/module/store/test/lib/store.unittest.class.php index a655aa0d58..6b685be82e 100755 --- a/module/store/test/lib/store.unittest.class.php +++ b/module/store/test/lib/store.unittest.class.php @@ -36,6 +36,51 @@ class storeTest return $this->objectModel->$name(...$arguments); } + /** + * Test __construct method. + * + * @param string $appName + * @access public + * @return object + */ + public function __constructTest(string $appName = '') + { + global $config, $app; + + // 创建新的store模型实例进行测试 + $storeModel = new storeModel($appName); + + // 返回测试结果对象 + $result = new stdClass(); + $result->appName = $appName; + $result->hasHeaders = !empty($config->cloud->api->headers); + $result->authHeaderExists = false; + $result->channelChanged = false; + + // 检查API headers是否正确设置 + if($result->hasHeaders) + { + foreach($config->cloud->api->headers as $header) + { + if(strpos($header, $config->cloud->api->auth) !== false) + { + $result->authHeaderExists = true; + break; + } + } + } + + // 检查channel是否被改变 + if($config->cloud->api->switchChannel && isset($app->session->cloudChannel)) + { + $result->channelChanged = ($config->cloud->api->channel === $app->session->cloudChannel); + } + + if(dao::isError()) return dao::getError(); + + return $result; + } + /** * 测试根据关键字查询应用市场应用列表。 * Test get app list from cloud market. diff --git a/module/store/test/model/__construct.php b/module/store/test/model/__construct.php new file mode 100755 index 0000000000..2d591571a1 --- /dev/null +++ b/module/store/test/model/__construct.php @@ -0,0 +1,57 @@ +#!/usr/bin/env php +loadModel('store'); + +// 初始化时API headers应该被设置 +$originalHeaders = $config->cloud->api->headers ?? array(); + +// 测试1:验证API headers被初始化 +r(count($originalHeaders) > 0) && p() && e('1'); // 测试默认构造函数初始化API头信息 + +// 测试2:验证headers中包含auth信息 +$hasAuthHeader = false; +foreach($originalHeaders as $header) { + if(strpos($header, $config->cloud->api->auth . ':') !== false) { + $hasAuthHeader = true; + break; + } +} +r($hasAuthHeader) && p() && e('1'); // 测试构造函数设置API认证头包含token + +// 测试3:验证默认channel值 +r($config->cloud->api->channel) && p() && e('stable'); // 测试构造函数初始化后config.cloud.api.channel值 + +// 测试4和5:测试创建新实例时appName的处理(通过创建新storeModel实例测试) +class TestStoreModel extends storeModel { + public $testAppName; + public function __construct($appName = '') { + parent::__construct($appName); + $this->testAppName = $appName; + } +} + +$testStore1 = new TestStoreModel('zentao'); +r($testStore1->testAppName) && p() && e('zentao'); // 测试构造函数传入appName参数为zentao + +$testStore2 = new TestStoreModel(''); +r($testStore2->testAppName === '') && p() && e('1'); // 测试构造函数传入空appName参数 \ No newline at end of file