subscription/run-tests.php

276 lines
10 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* 测试运行脚本
*
* 用于在命令行或浏览器中运行插件测试
*
* @package Yoone_Subscriptions
* @subpackage Tests
*/
// 设置WordPress环境
if (!defined('ABSPATH')) {
// 尝试找到WordPress根目录
$wp_root = dirname(dirname(dirname(dirname(__FILE__))));
if (file_exists($wp_root . '/wp-config.php')) {
require_once $wp_root . '/wp-config.php';
} else {
die('无法找到WordPress配置文件');
}
}
// 确保插件已加载
if (!class_exists('Yoone_Subscriptions')) {
die('Yoone Subscriptions插件未激活');
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Yoone Subscriptions 测试运行器</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
background-color: #f5f5f5;
}
.container {
background: white;
border-radius: 8px;
padding: 30px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
h1 {
color: #333;
border-bottom: 3px solid #0073aa;
padding-bottom: 10px;
}
.test-section {
margin: 20px 0;
padding: 20px;
border: 1px solid #ddd;
border-radius: 5px;
background: #fafafa;
}
.test-result {
margin: 10px 0;
padding: 10px;
border-radius: 3px;
}
.success {
background-color: #d4edda;
border-color: #c3e6cb;
color: #155724;
}
.error {
background-color: #f8d7da;
border-color: #f5c6cb;
color: #721c24;
}
.warning {
background-color: #fff3cd;
border-color: #ffeaa7;
color: #856404;
}
.info {
background-color: #d1ecf1;
border-color: #bee5eb;
color: #0c5460;
}
.btn {
display: inline-block;
padding: 10px 20px;
margin: 5px;
background-color: #0073aa;
color: white;
text-decoration: none;
border-radius: 3px;
border: none;
cursor: pointer;
}
.btn:hover {
background-color: #005a87;
}
.environment-info {
background: #e8f4f8;
padding: 15px;
border-radius: 5px;
margin-bottom: 20px;
}
.environment-info table {
width: 100%;
border-collapse: collapse;
}
.environment-info th,
.environment-info td {
padding: 8px;
text-align: left;
border-bottom: 1px solid #ddd;
}
.environment-info th {
background-color: #f2f2f2;
font-weight: bold;
}
</style>
</head>
<body>
<div class="container">
<h1>🧪 Yoone Subscriptions 测试运行器</h1>
<?php
// 显示环境信息
echo '<div class="environment-info">';
echo '<h3>📋 测试环境信息</h3>';
echo '<table>';
if (class_exists('Yoone_Test_Config')) {
$env_info = Yoone_Test_Config::get_environment_info();
foreach ($env_info as $key => $value) {
$label = ucwords(str_replace('_', ' ', $key));
echo "<tr><th>{$label}</th><td>{$value}</td></tr>";
}
} else {
echo '<tr><td colspan="2">无法获取环境信息 - Yoone_Test_Config类不存在</td></tr>';
}
echo '</table>';
echo '</div>';
// 运行测试的按钮
echo '<div class="test-section">';
echo '<h3>🚀 运行测试</h3>';
echo '<p>选择要运行的测试类型:</p>';
$test_types = array(
'subscription' => '订阅功能测试',
'payment' => '支付集成测试',
'bundle' => '捆绑产品测试',
'cron' => '定时任务测试',
'all' => '运行所有测试'
);
foreach ($test_types as $type => $label) {
echo "<a href='?run_test={$type}' class='btn'>{$label}</a>";
}
echo '</div>';
// 处理测试运行
if (isset($_GET['run_test'])) {
$test_type = sanitize_text_field($_GET['run_test']);
echo '<div class="test-section">';
echo "<h3>🔍 运行 {$test_types[$test_type]} 结果</h3>";
if (class_exists('Yoone_Test_Suite')) {
$test_suite = new Yoone_Test_Suite();
try {
switch ($test_type) {
case 'subscription':
echo '<h4>订阅功能测试</h4>';
$results = $test_suite->run_test_suite('subscription');
break;
case 'payment':
echo '<h4>支付集成测试</h4>';
$results = $test_suite->run_test_suite('payment');
break;
case 'bundle':
echo '<h4>捆绑产品测试</h4>';
$results = $test_suite->run_test_suite('bundle');
break;
case 'cron':
echo '<h4>定时任务测试</h4>';
$results = $test_suite->run_test_suite('cron');
break;
case 'all':
echo '<h4>运行所有测试</h4>';
$results = $test_suite->run_test_suite('all');
break;
}
// 显示测试结果
if (empty($results['tests'])) {
echo '<div class="test-result info">没有测试结果</div>';
} else {
// 显示摘要
$summary = $results['summary'];
echo "<div class='test-result info'>";
echo "<strong>测试摘要:</strong><br>";
echo "总计: {$summary['total']} | 通过: {$summary['passed']} | 失败: {$summary['failed']} | 跳过: {$summary['skipped']}";
echo "</div>";
// 显示详细结果
foreach ($results['tests'] as $result) {
$class = $result['status'] === 'passed' ? 'success' :
($result['status'] === 'failed' ? 'error' : 'warning');
echo "<div class='test-result {$class}'>";
echo "<strong>{$result['name']}</strong><br>";
echo "状态: " . ($result['status'] === 'passed' ? '✅ 通过' :
($result['status'] === 'failed' ? '❌ 失败' : '⚠️ 警告')) . "<br>";
echo "描述: {$result['description']}";
if (!empty($result['error'])) {
echo "<br><strong>错误:</strong> {$result['error']}";
}
if (!empty($result['result']) && $result['result'] !== true) {
echo "<br><strong>结果:</strong> {$result['result']}";
}
echo "</div>";
}
}
} catch (Exception $e) {
echo "<div class='test-result error'>";
echo "<strong>测试运行失败</strong><br>";
echo "错误: " . $e->getMessage();
echo "</div>";
}
} else {
echo '<div class="test-result error">Yoone_Test_Suite类不存在请确保插件正确安装</div>';
}
echo '</div>';
}
?>
<div class="test-section">
<h3>📚 测试说明</h3>
<ul>
<li><strong>订阅功能测试</strong>: 测试订阅的创建、激活、暂停、恢复、续费和取消流程</li>
<li><strong>支付集成测试</strong>: 测试Moneris支付网关集成和支付令牌管理</li>
<li><strong>捆绑产品测试</strong>: 测试产品捆绑功能和价格计算</li>
<li><strong>定时任务测试</strong>: 测试自动续费、过期处理等定时任务</li>
<li><strong>运行所有测试</strong>: 执行完整的测试套件</li>
</ul>
<h4>⚠️ 注意事项</h4>
<ul>
<li>测试会创建临时数据,测试完成后会自动清理</li>
<li>建议在开发环境中运行测试,避免影响生产数据</li>
<li>某些测试可能需要有效的支付配置才能完全通过</li>
</ul>
</div>
<div class="test-section">
<h3>🔧 快速操作</h3>
<a href="?" class="btn">刷新页面</a>
<a href="<?php echo admin_url('admin.php?page=yoone-test-suite'); ?>" class="btn">管理后台测试</a>
<a href="<?php echo admin_url('admin.php?page=yoone-logs'); ?>" class="btn">查看日志</a>
</div>
</div>
</body>
</html>