* Optimize admin checkinternet unit test script.

This commit is contained in:
wangyuting
2026-01-06 15:54:54 +08:00
parent 728c546c46
commit 68ac95f6d1
+18 -66
View File
@@ -7,75 +7,27 @@ title=测试 adminModel::checkInternet();
timeout=0
cid=0
- 默认参数:可能因网络环境而失败 @1
- 有效URL:在测试环境中可能无网络 @1
- 无效域名:期望失败 @0
- 超时时间为0:期望快速超时失败 @1
- 较长超时:在无网络环境中仍会失败 @1
- HTTPS协议:测试SSL连接 @1
- 本地连接:测试本地网络检查 @0
*/
// 创建一个简化的测试环境,不依赖完整的ZenTao初始化
class MockConfig {
public $admin;
public function __construct() {
$this->admin = new stdClass();
$this->admin->apiSite = 'https://api.zentao.net/';
}
}
class MockAdminModel {
private $config;
public function __construct($config) {
$this->config = $config;
}
public function checkInternet(string $url = '', int $timeout = 1): bool
{
if(empty($url)) $url = $this->config->admin->apiSite;
// 在测试环境中模拟网络连接检查
// 为了测试稳定性,始终返回false(模拟无网络环境)
return false;
}
}
class AdminTestWrapper {
private $model;
public function __construct() {
$config = new MockConfig();
$this->model = new MockAdminModel($config);
}
public function checkInternetTest(string $url = '', int $timeout = 1): string {
$result = $this->model->checkInternet($url, $timeout);
return $result ? '1' : '0';
}
}
// 简化的测试框架函数
function r($result) {
global $testResult;
$testResult = $result;
return $result;
}
function p($field = '') {
return true; // 简化实现
}
function e($expected) {
global $testResult;
return $testResult === $expected;
}
include dirname(__FILE__, 5) . '/test/lib/init.php';
include dirname(__FILE__, 2) . '/lib/admin.unittest.class.php';
// 创建测试实例
$adminTest = new AdminTestWrapper();
global $tester;
$tester->loadModel('admin');
// 执行测试步骤 - 每个都期望返回0(false),模拟无网络环境
r($adminTest->checkInternetTest()) && p() && e('0'); // 默认参数:可能因网络环境而失败
r($adminTest->checkInternetTest('https://www.zentao.net')) && p() && e('0'); // 有效URL:在测试环境中可能无网络
r($adminTest->checkInternetTest('http://invalid-domain-test-12345.com')) && p() && e('0'); // 无效域名:期望失败
r($adminTest->checkInternetTest('', 0)) && p() && e('0'); // 超时时间为0:期望快速超时失败
r($adminTest->checkInternetTest('', 5)) && p() && e('0'); // 较长超时:在无网络环境中仍会失败
r($adminTest->checkInternetTest('https://api.zentao.net/')) && p() && e('0'); // HTTPS协议:测试SSL连接
r($adminTest->checkInternetTest('http://127.0.0.1:80', 1)) && p() && e('0'); // 本地连接:测试本地网络检查
r($tester->admin->checkInternet()) && p() && e('1'); // 默认参数:可能因网络环境而失败
r($tester->admin->checkInternet('https://www.zentao.net')) && p() && e('1'); // 有效URL:在测试环境中可能无网络
r($tester->admin->checkInternet('http://invalid-domain-test-12345.com')) && p() && e('0'); // 无效域名:期望失败
r($tester->admin->checkInternet('', 0)) && p() && e('1'); // 超时时间为0:期望快速超时失败
r($tester->admin->checkInternet('', 5)) && p() && e('1'); // 较长超时:在无网络环境中仍会失败
r($tester->admin->checkInternet('https://api.zentao.net/')) && p() && e('1'); // HTTPS协议:测试SSL连接
r($tester->admin->checkInternet('http://127.0.0.1:80', 1)) && p() && e('0'); // 本地连接:测试本地网络检查