getMessage() . "\n"; } catch (Error $e) { echo "❌ 错误: " . $e->getMessage() . "\n"; } } // 测试1: PHP基础功能 runTest("PHP基础扩展", function() { $required_extensions = ['curl', 'openssl', 'pdo', 'json', 'mbstring']; $missing = []; foreach ($required_extensions as $ext) { if (!extension_loaded($ext)) { $missing[] = $ext; } } if (!empty($missing)) { return "缺少扩展: " . implode(', ', $missing); } return true; }); // 测试2: mcrypt替换功能(加密解密) runTest("加密解密功能", function() { // 包含加密相关文件 if (file_exists(__DIR__ . '/source/apis/transfer.php')) { require_once __DIR__ . '/source/apis/transfer.php'; // 测试简单的OpenSSL功能 $testData = "Hello World Test"; $key = "test_key_123"; // 测试AES加密 $encrypted = openssl_encrypt($testData, 'AES-128-ECB', str_pad($key, 16, '0'), OPENSSL_RAW_DATA); if ($encrypted === false) { return "OpenSSL AES加密失败"; } $decrypted = openssl_decrypt($encrypted, 'AES-128-ECB', str_pad($key, 16, '0'), OPENSSL_RAW_DATA); if ($decrypted !== $testData) { return "OpenSSL AES解密失败"; } return true; } else { return "transfer.php文件不存在"; } }); // 测试3: CURL功能 runTest("CURL HTTP请求", function() { if (!function_exists('curl_init')) { return "CURL扩展未安装"; } $ch = curl_init(); // 测试我们修复的CURLOPT_SAFE_UPLOAD问题 try { if (version_compare(phpversion(), '5.6') >= 0 && version_compare(phpversion(), '8.0') < 0) { curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); } // 如果没有抛出异常,说明修复成功 } catch (ValueError $e) { curl_close($ch); return "CURLOPT_SAFE_UPLOAD问题未修复: " . $e->getMessage(); } curl_close($ch); return true; }); // 测试4: 数组函数(each替换) runTest("数组遍历功能", function() { $test_array = ['a' => 1, 'b' => 2, 'c' => 3]; // 测试我们的each()替换逻辑 $results = []; while (($key = key($test_array)) !== null) { $value = current($test_array); $results[] = $value; next($test_array); } if (count($results) !== 3 || $results !== [1, 2, 3]) { return "数组遍历结果不正确"; } return true; }); // 测试5: 反射功能(create_function替换) runTest("动态类创建", function() { if (!class_exists('ReflectionClass')) { return "反射类不可用"; } // 测试创建stdClass实例 try { $reflection = new ReflectionClass('stdClass'); $instance = $reflection->newInstance(); if (!is_object($instance) || get_class($instance) !== 'stdClass') { return "反射创建实例失败"; } return true; } catch (Exception $e) { return "反射操作异常: " . $e->getMessage(); } }); // 测试6: 字符串访问语法 runTest("字符串访问语法", function() { $test_string = "hello"; // 测试方括号访问(替换大括号) try { $char = $test_string[strlen($test_string)-1]; if ($char !== 'o') { return "字符串访问结果错误"; } return true; } catch (Error $e) { return "字符串访问语法错误: " . $e->getMessage(); } }); // 测试7: session功能 runTest("Session功能", function() { if (session_status() === PHP_SESSION_DISABLED) { return "Session功能被禁用"; } // 在CLI模式下session可能无法正常工作,这是正常的 if (php_sapi_name() === 'cli') { return true; // CLI模式跳过session测试 } return true; }); // 测试8: 包含关键文件 runTest("关键文件加载", function() { $key_files = [ 'source/apis/transfer.php', 'framework/function/communication.func.php', 'lib/phprs/Router.php' ]; $missing_files = []; foreach ($key_files as $file) { if (!file_exists(__DIR__ . '/' . $file)) { $missing_files[] = $file; } } if (!empty($missing_files)) { return "文件缺失: " . implode(', ', $missing_files); } return true; }); // 测试9: communication.func.php加载 runTest("通信函数库", function() { $file = __DIR__ . '/framework/function/communication.func.php'; if (!file_exists($file)) { return "文件不存在"; } // 尝试包含文件 try { require_once $file; // 检查关键函数是否存在 if (!function_exists('ihttp_request') && !function_exists('ihttp_get')) { return "关键HTTP函数不存在"; } return true; } catch (Error $e) { return "文件包含错误: " . $e->getMessage(); } catch (Exception $e) { return "文件包含异常: " . $e->getMessage(); } }); // 测试10: 基本网络连接 runTest("网络连接测试", function() { // 简单的网络连接测试 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://www.baidu.com"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $error = curl_error($ch); curl_close($ch); if ($response === false) { return "网络请求失败: $error"; } if ($httpCode !== 200) { return "HTTP状态码异常: $httpCode"; } return true; }); echo "\n=== 测试总结 ===\n"; echo "总测试数: $tests_total\n"; echo "通过测试: $tests_passed\n"; echo "失败测试: " . ($tests_total - $tests_passed) . "\n"; if ($tests_passed === $tests_total) { echo "🎉 所有测试通过!系统PHP8兼容性良好!\n"; exit(0); } else { echo "⚠️ 有测试失败,需要进一步检查\n"; exit(1); } ?>