log_test('开始订阅流程测试'); try { // 1. 环境检查 $this->test_environment(); // 2. 创建测试数据 $this->setup_test_data(); // 3. 测试订阅创建 $this->test_subscription_creation(); // 4. 测试订阅激活 $this->test_subscription_activation(); // 5. 测试订阅暂停 $this->test_subscription_pause(); // 6. 测试订阅恢复 $this->test_subscription_resume(); // 7. 测试续费处理 $this->test_subscription_renewal(); // 8. 测试订阅取消 $this->test_subscription_cancellation(); // 9. 清理测试数据 $this->cleanup_test_data(); $this->log_test('订阅流程测试完成'); } catch (Exception $e) { $this->log_test('测试失败: ' . $e->getMessage(), 'error'); $this->cleanup_test_data(); } return $this->test_results; } /** * 环境检查 */ private function test_environment() { $this->log_test('检查测试环境'); // 检查必要的类是否存在 $required_classes = array( 'Yoone_Subscription', 'Yoone_Subscription_Manager', 'Yoone_Payment_Token', 'WC_Product' ); foreach ($required_classes as $class) { if (!class_exists($class)) { throw new Exception("必需的类 {$class} 不存在"); } } // 检查数据库表 global $wpdb; $tables = array( $wpdb->prefix . 'yoone_subscriptions', $wpdb->prefix . 'yoone_subscription_items', $wpdb->prefix . 'yoone_payment_tokens' ); foreach ($tables as $table) { $exists = $wpdb->get_var("SHOW TABLES LIKE '{$table}'"); if (!$exists) { throw new Exception("数据库表 {$table} 不存在"); } } $this->log_test('环境检查通过', 'success'); } /** * 创建测试数据 */ private function setup_test_data() { $this->log_test('创建测试数据'); // 创建测试客户 $this->customer_id = Yoone_Test_Config::create_test_customer(); if (!$this->customer_id) { throw new Exception('创建测试客户失败'); } // 创建测试产品 $this->product_id = Yoone_Test_Config::create_test_product('subscription'); if (!$this->product_id) { throw new Exception('创建测试产品失败'); } $this->log_test("测试数据创建成功 - 客户ID: {$this->customer_id}, 产品ID: {$this->product_id}", 'success'); } /** * 测试订阅创建 */ private function test_subscription_creation() { $this->log_test('测试订阅创建'); $subscription = new Yoone_Subscription(); $subscription->set_customer_id($this->customer_id); $subscription->set_status('pending'); $subscription->set_billing_period('month'); $subscription->set_billing_interval(1); $subscription->set_start_date(current_time('mysql')); $subscription->set_total(29.99); $subscription->set_currency('CAD'); // 添加订阅商品 $subscription->add_item(array( 'product_id' => $this->product_id, 'quantity' => 1, 'line_total' => 29.99 )); // 保存订阅 $this->subscription_id = $subscription->save(); if (!$this->subscription_id) { throw new Exception('订阅创建失败'); } // 验证订阅数据 $saved_subscription = new Yoone_Subscription($this->subscription_id); if ($saved_subscription->get_customer_id() !== $this->customer_id) { throw new Exception('订阅数据验证失败'); } $this->log_test("订阅创建成功 - ID: {$this->subscription_id}", 'success'); } /** * 测试订阅激活 */ private function test_subscription_activation() { $this->log_test('测试订阅激活'); $subscription = new Yoone_Subscription($this->subscription_id); $result = $subscription->activate(); if (!$result) { throw new Exception('订阅激活失败'); } // 验证状态 $subscription = new Yoone_Subscription($this->subscription_id); if ($subscription->get_status() !== 'active') { throw new Exception('订阅状态验证失败'); } $this->log_test('订阅激活成功', 'success'); } /** * 测试订阅暂停 */ private function test_subscription_pause() { $this->log_test('测试订阅暂停'); $subscription = new Yoone_Subscription($this->subscription_id); $result = $subscription->pause(); if (!$result) { throw new Exception('订阅暂停失败'); } // 验证状态 $subscription = new Yoone_Subscription($this->subscription_id); if ($subscription->get_status() !== 'on-hold') { throw new Exception('订阅暂停状态验证失败'); } $this->log_test('订阅暂停成功', 'success'); } /** * 测试订阅恢复 */ private function test_subscription_resume() { $this->log_test('测试订阅恢复'); $subscription = new Yoone_Subscription($this->subscription_id); $result = $subscription->resume(); if (!$result) { throw new Exception('订阅恢复失败'); } // 验证状态 $subscription = new Yoone_Subscription($this->subscription_id); if ($subscription->get_status() !== 'active') { throw new Exception('订阅恢复状态验证失败'); } $this->log_test('订阅恢复成功', 'success'); } /** * 测试续费处理 */ private function test_subscription_renewal() { $this->log_test('测试续费处理'); $subscription = new Yoone_Subscription($this->subscription_id); // 设置下次付款日期为过去时间以触发续费 $past_date = date('Y-m-d H:i:s', strtotime('-1 day')); $subscription->set_next_payment_date($past_date); $subscription->save(); // 模拟续费处理 $renewal_result = $subscription->process_renewal(); if (!$renewal_result) { // 续费可能因为没有支付方式而失败,这在测试中是正常的 $this->log_test('续费处理完成(可能因缺少支付方式而失败,这是正常的)', 'warning'); } else { $this->log_test('续费处理成功', 'success'); } } /** * 测试订阅取消 */ private function test_subscription_cancellation() { $this->log_test('测试订阅取消'); $subscription = new Yoone_Subscription($this->subscription_id); $result = $subscription->cancel(); if (!$result) { throw new Exception('订阅取消失败'); } // 验证状态 $subscription = new Yoone_Subscription($this->subscription_id); if ($subscription->get_status() !== 'cancelled') { throw new Exception('订阅取消状态验证失败'); } $this->log_test('订阅取消成功', 'success'); } /** * 清理测试数据 */ private function cleanup_test_data() { $this->log_test('清理测试数据'); // 删除测试订阅 if ($this->subscription_id) { $subscription = new Yoone_Subscription($this->subscription_id); $subscription->delete(); } // 删除测试产品 if ($this->product_id) { wp_delete_post($this->product_id, true); } // 删除测试客户 if ($this->customer_id) { wp_delete_user($this->customer_id); } $this->log_test('测试数据清理完成', 'success'); } /** * 记录测试结果 */ private function log_test($message, $type = 'info') { $this->test_results[] = array( 'timestamp' => current_time('mysql'), 'message' => $message, 'type' => $type ); // 同时记录到日志系统 Yoone_Logger::info('订阅流程测试: ' . $message); } /** * 获取测试结果 */ public function get_test_results() { return $this->test_results; } }