文件
xionggeshop/tests/OrderDetailCommissionTest.php

147 行
5.9 KiB
PHP

<?php
/**
* 订单详情佣金分成明细查询与展示单元测试 (TDD)
*/
class OrderDetailCommissionTest
{
private $passed = 0;
private $failed = 0;
public function run()
{
echo "=== 开始执行 订单详情佣金分成明细查询与展示单元测试 (TDD) ===\n";
$this->testOrderControllerHasCommissionMethod();
$this->testOrderControllerDetailIncludesCommissions();
$this->testOrderDetailViewHasCommissionSection();
$this->testOrderDetailJsHasCommissionData();
$this->testOrder64CommissionDataQuery();
echo "\n测试结果: {$this->passed} 通过, {$this->failed} 失败\n";
if ($this->failed > 0) {
exit(1);
}
}
private function assert($condition, $message)
{
if ($condition) {
echo " [PASS] {$message}\n";
$this->passed++;
} else {
echo " [FAIL] {$message}\n";
$this->failed++;
}
}
/**
* 1. 验证 Order 控制器中必须包含 getOrderCommissions 辅助方法
*/
public function testOrderControllerHasCommissionMethod()
{
$controllerPath = __DIR__ . '/../application/admin/controller/shopro/order/Order.php';
$this->assert(is_file($controllerPath), "Order.php 必须存在: {$controllerPath}");
$content = file_get_contents($controllerPath);
$hasMethod = strpos($content, 'function getOrderCommissions(') !== false;
$this->assert($hasMethod, "Order 控制器必须包含 getOrderCommissions 方法以获取分销与区域代理等分成明细");
}
/**
* 2. 验证 Order 控制器的 detail 方法返回值中必须包含 commissions 和 commission_total
*/
public function testOrderControllerDetailIncludesCommissions()
{
$controllerPath = __DIR__ . '/../application/admin/controller/shopro/order/Order.php';
$content = file_get_contents($controllerPath);
$startPos = strpos($content, 'public function detail(');
$this->assert($startPos !== false, "Order 控制器必须包含 detail 方法");
$nextFuncPos = strpos($content, 'public function ', $startPos + 20);
$methodBody = substr($content, $startPos, ($nextFuncPos !== false ? $nextFuncPos - $startPos : 1000));
$hasCommissionsKey = strpos($methodBody, "'commissions'") !== false || strpos($methodBody, '"commissions"') !== false;
$hasTotalKey = strpos($methodBody, "'commission_total'") !== false || strpos($methodBody, '"commission_total"') !== false;
$this->assert($hasCommissionsKey, "Order::detail 返回数据必须包含 'commissions' 键");
$this->assert($hasTotalKey, "Order::detail 返回数据必须包含 'commission_total' 键");
}
/**
* 3. 验证 detail.html 视图中必须包含佣金分成明细表格区域
*/
public function testOrderDetailViewHasCommissionSection()
{
$viewPath = __DIR__ . '/../application/admin/view/shopro/order/order/detail.html';
$this->assert(is_file($viewPath), "detail.html 必须存在: {$viewPath}");
$content = file_get_contents($viewPath);
$hasSection = strpos($content, 'commissionList') !== false && strpos($content, '佣金分成明细') !== false;
$this->assert($hasSection, "detail.html 必须包含佣金分成明细 (commissionList) 展示模块");
}
/**
* 4. 验证 order.js 中包含 commissionList 状态与数据接收
*/
public function testOrderDetailJsHasCommissionData()
{
$jsPath = __DIR__ . '/../public/assets/js/backend/shopro/order/order.js';
$this->assert(is_file($jsPath), "order.js 必须存在: {$jsPath}");
$content = file_get_contents($jsPath);
$hasState = strpos($content, 'commissionList') !== false;
$hasReceive = strpos($content, 'res.data.commissions') !== false;
$this->assert($hasState, "order.js 必须声明 commissionList 变量");
$this->assert($hasReceive, "order.js 的 getDetail 必须接收并赋值 res.data.commissions");
}
/**
* 5. 验证数据库中订单 64 的实际分成数据
*/
public function testOrder64CommissionDataQuery()
{
$dbConfigFile = __DIR__ . '/../application/database.php';
if (!is_file($dbConfigFile)) {
$this->assert(false, "数据库配置文件不存在");
return;
}
require_once __DIR__ . '/../thinkphp/base.php';
$dbConfig = require $dbConfigFile;
try {
$pdo = new PDO(
"mysql:host={$dbConfig['hostname']};dbname={$dbConfig['database']};charset=utf8mb4",
$dbConfig['username'],
$dbConfig['password']
);
// 查询分销佣金
$stmt = $pdo->query("SELECT * FROM fa_shopro_commission_reward WHERE order_id = 64");
$commRewards = $stmt->fetchAll(PDO::FETCH_ASSOC);
$this->assert(count($commRewards) >= 1, "订单 64 必须存在至少 1 笔分销佣金记录 (当前: " . count($commRewards) . ")");
// 查询区域代理佣金
$stmt = $pdo->query("SELECT * FROM fa_shopro_regional_agent_reward WHERE order_id = 64");
$regRewards = $stmt->fetchAll(PDO::FETCH_ASSOC);
$this->assert(count($regRewards) >= 1, "订单 64 必须存在至少 1 笔区域代理佣金记录 (当前: " . count($regRewards) . ")");
$total = 0;
foreach ($commRewards as $r) {
$total += floatval($r['commission']);
}
foreach ($regRewards as $r) {
$total += floatval($r['commission']);
}
$this->assert(abs($total - 208.00) < 0.01, "订单 64 的分成总金额必须为 208.00 (计算得: {$total})");
} catch (Exception $e) {
$this->assert(false, "数据库测试失败: " . $e->getMessage());
}
}
}
$test = new OrderDetailCommissionTest();
$test->run();