subscription/tests/test-bundle-products.php

360 lines
12 KiB
PHP

<?php
/**
* 捆绑产品测试脚本
*
* @package Yoone_Subscriptions
* @subpackage Tests
*/
// 防止直接访问
if (!defined('ABSPATH')) {
exit;
}
/**
* 捆绑产品测试类
*/
class Yoone_Bundle_Products_Test {
private $test_results = array();
private $bundle_product_id;
private $child_product_ids = array();
private $customer_id;
private $subscription_id;
/**
* 运行完整的捆绑产品测试
*/
public function run_full_test() {
$this->log_test('开始捆绑产品测试');
try {
// 1. 环境检查
$this->test_environment();
// 2. 创建测试数据
$this->setup_test_data();
// 3. 测试捆绑产品创建
$this->test_bundle_product_creation();
// 4. 测试价格计算
$this->test_price_calculation();
// 5. 测试捆绑产品订阅
$this->test_bundle_subscription();
// 6. 测试子产品管理
$this->test_child_product_management();
// 7. 测试库存管理
$this->test_inventory_management();
// 8. 清理测试数据
$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_Bundle_Product',
'Yoone_Subscription'
);
foreach ($required_classes as $class) {
if (!class_exists($class)) {
throw new Exception("必需的类 {$class} 不存在");
}
}
// 检查WooCommerce是否激活
if (!class_exists('WooCommerce')) {
throw new Exception('WooCommerce未激活');
}
$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('创建测试客户失败');
}
// 创建子产品
$child_products = Yoone_Test_Config::get_bundle_products();
foreach ($child_products as $product_data) {
$product_id = Yoone_Test_Config::create_test_product($product_data);
if ($product_id) {
$this->child_product_ids[] = $product_id;
}
}
if (empty($this->child_product_ids)) {
throw new Exception('创建子产品失败');
}
$this->log_test("捆绑产品测试数据创建成功 - 客户ID: {$this->customer_id}, 子产品数量: " . count($this->child_product_ids), 'success');
}
/**
* 测试捆绑产品创建
*/
private function test_bundle_product_creation() {
$this->log_test('测试捆绑产品创建');
// 创建捆绑产品
$bundle_data = array(
'name' => '测试捆绑产品',
'type' => 'yoone_bundle',
'regular_price' => 99.99,
'subscription_price' => 89.99,
'subscription_period' => 'month',
'subscription_period_interval' => 1,
'status' => 'publish'
);
$product = new WC_Product();
$product->set_name($bundle_data['name']);
$product->set_regular_price($bundle_data['regular_price']);
$product->set_status($bundle_data['status']);
$this->bundle_product_id = $product->save();
if (!$this->bundle_product_id) {
throw new Exception('捆绑产品创建失败');
}
// 设置产品类型为捆绑产品
wp_set_object_terms($this->bundle_product_id, $bundle_data['type'], 'product_type');
// 设置捆绑产品属性
update_post_meta($this->bundle_product_id, '_yoone_subscription_price', $bundle_data['subscription_price']);
update_post_meta($this->bundle_product_id, '_yoone_subscription_period', $bundle_data['subscription_period']);
update_post_meta($this->bundle_product_id, '_yoone_subscription_period_interval', $bundle_data['subscription_period_interval']);
// 添加子产品
$bundle_items = array();
foreach ($this->child_product_ids as $index => $child_id) {
$bundle_items[] = array(
'product_id' => $child_id,
'quantity' => 1,
'discount' => 10 // 10% 折扣
);
}
update_post_meta($this->bundle_product_id, '_yoone_bundle_items', $bundle_items);
$this->log_test("捆绑产品创建成功 - ID: {$this->bundle_product_id}", 'success');
}
/**
* 测试价格计算
*/
private function test_price_calculation() {
$this->log_test('测试捆绑产品价格计算');
$bundle_product = wc_get_product($this->bundle_product_id);
if (!$bundle_product) {
throw new Exception('获取捆绑产品失败');
}
// 计算子产品总价
$child_total = 0;
foreach ($this->child_product_ids as $child_id) {
$child_product = wc_get_product($child_id);
if ($child_product) {
$child_total += $child_product->get_regular_price();
}
}
// 验证价格计算
$bundle_price = $bundle_product->get_regular_price();
if ($bundle_price <= 0) {
throw new Exception('捆绑产品价格无效');
}
// 检查折扣是否正确应用
$expected_discount = $child_total * 0.1; // 10% 折扣
$actual_savings = $child_total - $bundle_price;
if (abs($actual_savings - $expected_discount) > 0.01) {
$this->log_test("价格计算警告 - 预期节省: {$expected_discount}, 实际节省: {$actual_savings}", 'warning');
}
$this->log_test("价格计算测试通过 - 捆绑价格: {$bundle_price}, 子产品总价: {$child_total}", 'success');
}
/**
* 测试捆绑产品订阅
*/
private function test_bundle_subscription() {
$this->log_test('测试捆绑产品订阅');
// 创建订阅
$subscription = new Yoone_Subscription();
$subscription->set_customer_id($this->customer_id);
$subscription->set_billing_period('month');
$subscription->set_billing_interval(1);
$subscription->set_status('pending');
// 添加捆绑产品到订阅
$subscription->add_item(array(
'product_id' => $this->bundle_product_id,
'quantity' => 1,
'price' => 89.99
));
$this->subscription_id = $subscription->save();
if (!$this->subscription_id) {
throw new Exception('创建捆绑产品订阅失败');
}
// 验证订阅项目
$items = $subscription->get_items();
if (empty($items)) {
throw new Exception('订阅项目为空');
}
$bundle_item = reset($items);
if ($bundle_item['product_id'] != $this->bundle_product_id) {
throw new Exception('订阅项目产品ID不匹配');
}
$this->log_test("捆绑产品订阅创建成功 - 订阅ID: {$this->subscription_id}", 'success');
}
/**
* 测试子产品管理
*/
private function test_child_product_management() {
$this->log_test('测试子产品管理');
// 获取捆绑产品的子产品
$bundle_items = get_post_meta($this->bundle_product_id, '_yoone_bundle_items', true);
if (empty($bundle_items)) {
throw new Exception('获取捆绑产品子项目失败');
}
// 验证每个子产品
foreach ($bundle_items as $item) {
$child_product = wc_get_product($item['product_id']);
if (!$child_product) {
throw new Exception("子产品 {$item['product_id']} 不存在");
}
if (!$child_product->is_purchasable()) {
$this->log_test("子产品 {$item['product_id']} 不可购买", 'warning');
}
}
// 测试子产品数量验证
if (count($bundle_items) !== count($this->child_product_ids)) {
throw new Exception('捆绑产品子项目数量不匹配');
}
$this->log_test('子产品管理测试通过', 'success');
}
/**
* 测试库存管理
*/
private function test_inventory_management() {
$this->log_test('测试捆绑产品库存管理');
$bundle_product = wc_get_product($this->bundle_product_id);
// 检查库存状态
if ($bundle_product->managing_stock()) {
$stock_quantity = $bundle_product->get_stock_quantity();
if ($stock_quantity <= 0) {
$this->log_test('捆绑产品库存不足', 'warning');
}
}
// 检查子产品库存
foreach ($this->child_product_ids as $child_id) {
$child_product = wc_get_product($child_id);
if ($child_product && $child_product->managing_stock()) {
$child_stock = $child_product->get_stock_quantity();
if ($child_stock <= 0) {
$this->log_test("子产品 {$child_id} 库存不足", 'warning');
}
}
}
$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->bundle_product_id) {
wp_delete_post($this->bundle_product_id, true);
}
// 删除子产品
foreach ($this->child_product_ids as $child_id) {
wp_delete_post($child_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;
}
}