文件
mashangyoujiu/_ai_archive/scripts/test_sync.php

199 行
6.3 KiB
PHP

<?php
/**
* 订单同步功能测试脚本
*/
// 引入必要的文件
require_once 'Application/Common/Business/OrderSyncService.class.php';
// 测试配置
$testConfig = [
'db_host' => 'localhost',
'db_name' => 'test_db',
'db_user' => 'root',
'db_pass' => ''
];
class OrderSyncTest {
private $syncService;
public function __construct() {
$this->syncService = new OrderSyncService();
}
/**
* 测试获取可同步订单
*/
public function testGetSyncableOrders() {
echo "=== 测试获取可同步订单 ===\n";
try {
$orders = $this->syncService->getSyncableOrders([
'start_date' => date('Y-m-d 00:00:00', strtotime('-7 days')),
'end_date' => date('Y-m-d 23:59:59'),
'limit' => 10
]);
echo "找到 " . count($orders) . " 个可同步订单\n";
if (!empty($orders)) {
echo "示例订单信息:\n";
$firstOrder = $orders[0];
echo "订单号: " . $firstOrder['order_number'] . "\n";
echo "客户: " . $firstOrder['customer_name'] . "\n";
echo "商户: " . $firstOrder['merchant_name'] . "\n";
echo "金额: ¥" . $firstOrder['total_amount'] . "\n";
echo "下单时间: " . $firstOrder['created_at'] . "\n";
}
return true;
} catch (Exception $e) {
echo "错误: " . $e->getMessage() . "\n";
return false;
}
}
/**
* 测试单个订单同步
*/
public function testSyncSingleOrder() {
echo "\n=== 测试单个订单同步 ===\n";
try {
// 获取一个测试订单ID
$orders = $this->syncService->getSyncableOrders(['limit' => 1]);
if (empty($orders)) {
echo "没有可同步的订单\n";
return false;
}
$testOrderId = $orders[0]['bill_id'];
echo "测试订单ID: " . $testOrderId . "\n";
$result = $this->syncService->syncSingleOrder($testOrderId);
if ($result['success']) {
echo "同步成功!\n";
echo "骑手订单ID: " . $result['rider_order_id'] . "\n";
echo "骑手订单号: " . $result['rider_order_number'] . "\n";
} else {
echo "同步失败: " . $result['message'] . "\n";
}
return $result['success'];
} catch (Exception $e) {
echo "错误: " . $e->getMessage() . "\n";
return false;
}
}
/**
* 测试批量订单同步
*/
public function testBatchSyncOrders() {
echo "\n=== 测试批量订单同步 ===\n";
try {
// 获取多个测试订单ID
$orders = $this->syncService->getSyncableOrders(['limit' => 3]);
if (empty($orders)) {
echo "没有可同步的订单\n";
return false;
}
$orderIds = array_column($orders, 'bill_id');
echo "测试订单数量: " . count($orderIds) . "\n";
$result = $this->syncService->batchSyncOrders($orderIds);
echo "同步结果:\n";
echo "成功: " . $result['success_count'] . " 个\n";
echo "失败: " . $result['failed_count'] . " 个\n";
if (!empty($result['failed_orders'])) {
echo "失败订单:\n";
foreach ($result['failed_orders'] as $failedOrder) {
echo "- 订单ID " . $failedOrder['order_id'] . ": " . $failedOrder['error'] . "\n";
}
}
return $result['success_count'] > 0;
} catch (Exception $e) {
echo "错误: " . $e->getMessage() . "\n";
return false;
}
}
/**
* 测试同步统计
*/
public function testSyncStatistics() {
echo "\n=== 测试同步统计 ===\n";
try {
$stats = $this->syncService->getSyncStatistics();
echo "同步统计信息:\n";
echo "可同步订单: " . $stats['total_syncable'] . "\n";
echo "今日已同步: " . $stats['today_synced'] . "\n";
echo "总计已同步: " . $stats['total_synced'] . "\n";
echo "同步失败: " . $stats['failed_count'] . "\n";
return true;
} catch (Exception $e) {
echo "错误: " . $e->getMessage() . "\n";
return false;
}
}
/**
* 运行所有测试
*/
public function runAllTests() {
echo "开始订单同步功能测试...\n\n";
$tests = [
'testGetSyncableOrders' => '获取可同步订单',
'testSyncStatistics' => '同步统计',
'testSyncSingleOrder' => '单个订单同步',
'testBatchSyncOrders' => '批量订单同步'
];
$results = [];
foreach ($tests as $method => $description) {
$results[$description] = $this->$method();
}
echo "\n=== 测试结果汇总 ===\n";
foreach ($results as $test => $result) {
$status = $result ? '✓ 通过' : '✗ 失败';
echo $test . ": " . $status . "\n";
}
$passedCount = array_sum($results);
$totalCount = count($results);
echo "\n总计: {$passedCount}/{$totalCount} 个测试通过\n";
return $passedCount === $totalCount;
}
}
// 运行测试
if (php_sapi_name() === 'cli') {
$test = new OrderSyncTest();
$allPassed = $test->runAllTests();
exit($allPassed ? 0 : 1);
} else {
// Web界面测试
header('Content-Type: text/html; charset=utf-8');
echo '<pre>';
$test = new OrderSyncTest();
$test->runAllTests();
echo '</pre>';
}
?>