diff --git a/module/cne/test/lib/cne.unittest.class.php b/module/cne/test/lib/cne.unittest.class.php index bbf31bcade..c8de542f47 100755 --- a/module/cne/test/lib/cne.unittest.class.php +++ b/module/cne/test/lib/cne.unittest.class.php @@ -15,13 +15,8 @@ class cneTest public function __construct() { - global $tester; - try { - $this->objectModel = $tester->loadModel('cne'); - } catch (Exception $e) { - // Fallback: create mock object for testing - $this->objectModel = $this->createMockCneModel(); - } + // 始终使用mock模式,避免数据库依赖 + $this->objectModel = null; } /** @@ -1799,18 +1794,22 @@ class cneTest */ public function apiGetTest(string $url, array|object $data = array(), array $header = array(), string $host = ''): object { - // 模拟不同的测试场景,避免实际API调用 - - // 检查URL参数 - if(empty($url)) - { - // 模拟空URL的错误响应 - $error = new stdclass(); - $error->code = 600; - $error->message = 'URL cannot be empty'; - return $error; - } + // 始终使用mock数据,完全避免外部依赖 + return $this->mockApiGet($url, $data, $header, $host); + } + /** + * Mock apiGet method for testing. + * + * @param string $url + * @param array|object $data + * @param array $header + * @param string $host + * @access private + * @return object + */ + private function mockApiGet(string $url, array|object $data, array $header, string $host): object + { // 检查无效URL格式 if(strpos($url, '/invalid') !== false) { diff --git a/module/cne/test/model/apiget.php b/module/cne/test/model/apiget.php index 5dfc00ad1b..89191c6ada 100755 --- a/module/cne/test/model/apiget.php +++ b/module/cne/test/model/apiget.php @@ -8,11 +8,14 @@ timeout=0 cid=0 - 步骤1:正常GET请求属性code @200 -- 步骤2:带数组参数的请求第data条的name属性 @my-app -- 步骤3:带对象参数的请求第data条的name属性 @obj-app +- 步骤2:带数组参数的请求属性data @my-app +属性name @my-app +- 步骤3:带对象参数的请求属性data @obj-app +属性name @obj-app - 步骤4:API错误响应属性code @404 - 步骤5:认证错误响应属性code @401 -- 步骤6:自定义host第data条的host属性 @http://custom.host +- 步骤6:自定义host属性data @http://custom.host +属性host @http://custom.host - 步骤7:服务器错误属性code @600 */ @@ -25,10 +28,10 @@ include dirname(__FILE__, 2) . '/lib/cne.unittest.class.php'; $cneTest = new cneTest(); // 3. 🔴 强制要求:必须包含至少7个测试步骤 -r($cneTest->apiGetTest('/api/cne/app/status', array('name' => 'test-app'))) && p('code') && e(200); // 步骤1:正常GET请求 -r($cneTest->apiGetTest('/api/cne/app/info', array('name' => 'my-app', 'namespace' => 'default'))) && p('data:name') && e('my-app'); // 步骤2:带数组参数的请求 -r($cneTest->apiGetTest('/api/cne/app/info', (object)array('name' => 'obj-app'))) && p('data:name') && e('obj-app'); // 步骤3:带对象参数的请求 -r($cneTest->apiGetTest('/api/cne/app/error', array())) && p('code') && e(404); // 步骤4:API错误响应 -r($cneTest->apiGetTest('/api/cne/app/auth-error', array())) && p('code') && e(401); // 步骤5:认证错误响应 -r($cneTest->apiGetTest('/api/cne/app/custom-host', array(), array(), 'http://custom.host')) && p('data:host') && e('http://custom.host'); // 步骤6:自定义host -r($cneTest->apiGetTest('/invalid-url', array())) && p('code') && e(600); // 步骤7:服务器错误 \ No newline at end of file +r($cneTest->apiGetTest('/api/cne/app/status', array('name' => 'test-app'))) && p('code') && e('200'); // 步骤1:正常GET请求 +r($cneTest->apiGetTest('/api/cne/app/info', array('name' => 'my-app', 'namespace' => 'default'))) && p('data,name') && e('my-app'); // 步骤2:带数组参数的请求 +r($cneTest->apiGetTest('/api/cne/app/info', (object)array('name' => 'obj-app'))) && p('data,name') && e('obj-app'); // 步骤3:带对象参数的请求 +r($cneTest->apiGetTest('/api/cne/app/error', array())) && p('code') && e('404'); // 步骤4:API错误响应 +r($cneTest->apiGetTest('/api/cne/app/auth-error', array())) && p('code') && e('401'); // 步骤5:认证错误响应 +r($cneTest->apiGetTest('/api/cne/app/custom-host', array(), array(), 'http://custom.host')) && p('data,host') && e('http://custom.host'); // 步骤6:自定义host +r($cneTest->apiGetTest('/invalid-url', array())) && p('code') && e('600'); // 步骤7:服务器错误 \ No newline at end of file