{$detail}" : '') . "\n"; } else { $fail++; $fails[] = "{$name} ({$detail})"; echo " [FAIL] {$name}" . ($detail !== '' ? " -> {$detail}" : '') . "\n"; } } function approx($a, $b, $eps = 0.01) { return abs((float)$a - (float)$b) < $eps; } echo "════════════════════════════════════════════════════════\n"; echo " 方案A:师傅报价外加抽成模式 自动化回归测试\n"; echo "════════════════════════════════════════════════════════\n"; $createdOrderIds = []; $createdQuoteIds = []; $createdPayIds = []; $createdLogIds = []; $createdMsgIds = []; $createdWalletLogIds = []; try { // 1. 纯算法测试:基础 10% 规则 echo "\n■ 1. CommissionService 算法单元测试\n"; $snapMatch = CommissionService::matchOrderSnapshot([ 'price_type' => 'quote', 'customer_type' => 'personal', 'cat_id' => 0, 'urgent' => 0, 'order_amount' => 0, ]); $mockOrder = [ 'price_type' => 'quote', 'commission_rate' => $snapMatch['commission_rate'], 'commission_snapshot' => $snapMatch['commission_snapshot'], ]; $m200 = CommissionService::calcQuoteMarkupByOrder($mockOrder, 200.00); check('师傅报200(10%规则):master_amount=200', approx($m200['master_amount'], 200.00), $m200['master_amount']); check('师傅报200(10%规则):platform_fee=20', approx($m200['platform_fee'], 20.00), $m200['platform_fee']); check('师傅报200(10%规则):user_amount=220', approx($m200['user_amount'], 220.00), $m200['user_amount']); check('师傅报200(10%规则):有效抽成率=9.09%', approx($m200['commission_rate'], 9.09), $m200['commission_rate']); // 选标快照刷新 + 结算 $quoteRowNew = [ 'id' => 999, 'master_amount' => $m200['master_amount'], 'platform_fee' => $m200['platform_fee'], 'amount' => $m200['user_amount'], ]; $refreshed = CommissionService::refreshQuoteOrderSnapshotOnAmountConfirmed( 220.00, $mockOrder['commission_snapshot'], $mockOrder['commission_rate'], $quoteRowNew ); $settledOrder = [ 'pay_amount' => 220.00, 'total_amount' => 220.00, 'diff_status' => 0, 'diff_amount' => 0.00, 'commission_rate' => $refreshed['commission_rate'], 'commission_snapshot' => $refreshed['commission_snapshot'], ]; $calcBase = CommissionService::calcByOrder($settledOrder); check('无增项全额结算:师傅实得=200.00', approx($calcBase['settle_amount'], 200.00), $calcBase['settle_amount']); check('无增项全额结算:平台抽佣=20.00', approx($calcBase['commission'], 20.00), $calcBase['commission']); // 2. 叠加现场增项补差价(增项仍按现行10%抽佣) echo "\n■ 2. 增项补差价联动与优惠券边界\n"; $settledWithDiff = $settledOrder; $settledWithDiff['pay_amount'] = 320.00; $settledWithDiff['total_amount'] = 320.00; $settledWithDiff['diff_status'] = 2; $settledWithDiff['diff_amount'] = 100.00; $calcDiff = CommissionService::calcByOrder($settledWithDiff); check('叠加增项100:师傅实得=290.00(200+90)', approx($calcDiff['settle_amount'], 290.00), $calcDiff['settle_amount']); check('叠加增项100:平台抽佣=30.00(20+10)', approx($calcDiff['commission'], 30.00), $calcDiff['commission']); // 优惠券抵扣10元(用户实付210) $refreshedCoupon = CommissionService::refreshQuoteOrderSnapshotOnAmountConfirmed( 210.00, $mockOrder['commission_snapshot'], $mockOrder['commission_rate'], $quoteRowNew ); $calcCoupon = CommissionService::calcByOrder([ 'pay_amount' => 210.00, 'total_amount' => 220.00, 'diff_status' => 0, 'diff_amount' => 0.00, 'commission_rate' => $refreshedCoupon['commission_rate'], 'commission_snapshot' => $refreshedCoupon['commission_snapshot'], ]); check('用户用10元优惠券实付210:师傅仍实得=200.00', approx($calcCoupon['settle_amount'], 200.00), $calcCoupon['settle_amount']); check('用户用10元优惠券实付210:平台实收抽佣=10.00', approx($calcCoupon['commission'], 10.00), $calcCoupon['commission']); // 3. 金额阶梯与混合/固定费规则验证 echo "\n■ 3. 多维阶梯与混合计佣校验\n"; $tierSnap = [ 'version' => 1, 'snapshot_time' => time(), 'include_diff_in_commission' => 1, 'active_rule' => [ 'rule_id' => 101, 'rule_name' => '小额档', 'min_amount' => 0, 'max_amount' => 200, 'calc_mode' => 'percent', 'commission_rate' => 10.00, 'fixed_fee' => 0, 'min_commission' => 0, 'max_commission' => 0, ], 'fallback_rule' => [ 'rule_id' => 0, 'rule_name' => '兜底', 'min_amount' => 0, 'max_amount' => 0, 'calc_mode' => 'percent', 'commission_rate' => 10.00, 'fixed_fee' => 0, 'min_commission' => 0, 'max_commission' => 0, ], 'candidate_tiers' => [ [ 'rule_id' => 101, 'rule_name' => '小额档', 'min_amount' => 0, 'max_amount' => 200, 'calc_mode' => 'percent', 'commission_rate' => 10.00, 'fixed_fee' => 0, 'min_commission' => 0, 'max_commission' => 0, ], [ 'rule_id' => 102, 'rule_name' => '大额封顶档', 'min_amount' => 200.01, 'max_amount' => 0, 'calc_mode' => 'mixed', 'commission_rate' => 8.00, 'fixed_fee' => 5.00, 'min_commission' => 0, 'max_commission' => 30.00, ], ], ]; $tierOrder = ['price_type' => 'quote', 'commission_rate' => 10.00, 'commission_snapshot' => json_encode($tierSnap, JSON_UNESCAPED_UNICODE)]; $mBig = CommissionService::calcQuoteMarkupByOrder($tierOrder, 500.00); // 500 * 8% + 5 = 45 -> 封顶30 -> user_amount = 530 check('阶梯+混合+封顶规则:platform_fee=30.00', approx($mBig['platform_fee'], 30.00), $mBig['platform_fee']); check('阶梯+混合+封顶规则:user_amount=530.00', approx($mBig['user_amount'], 530.00), $mBig['user_amount']); // 4. 数据库真实链路闭环(余额选标支付 -> 通知文案校验 -> 结算计算) echo "\n■ 4. 余额选标与通知文案数据库闭环测试\n"; $user = Db::name('user')->order('id asc')->find(); $master = Db::name('master')->where('verify_status', 'pass')->order('id asc')->find(); if ($user && $master) { $now = time(); $origUserMoney = (float)$user['money']; Db::name('user')->where('id', (int)$user['id'])->update(['money' => $origUserMoney + 500.00]); $orderNo = 'QMT' . date('YmdHis') . mt_rand(1000, 9999); $orderId = (int)Db::name('order')->insertGetId([ 'order_no' => $orderNo, 'user_id' => (int)$user['id'], 'master_id' => 0, 'cat_id' => 1, 'service_id' => 0, 'service_name' => '方案A测试报价单', 'price_type' => 'quote', 'contact' => '测试业主', 'mobile' => '13800000000', 'province' => '贵州省', 'city' => '遵义市', 'district' => '汇川区', 'address' => '测试路1号', 'base_amount' => 0, 'total_amount' => 0, 'pay_amount' => 0, 'commission_rate' => $snapMatch['commission_rate'], 'commission_snapshot' => $snapMatch['commission_snapshot'], 'status' => OrderStatus::QUOTING, 'pay_status' => 0, 'verify_code' => '123456', 'createtime' => $now, 'updatetime' => $now, ]); $createdOrderIds[] = $orderId; $quoteId = (int)Db::name('order_quote')->insertGetId([ 'order_id' => $orderId, 'master_id' => (int)$master['id'], 'master_amount' => 200.00, 'platform_fee' => 20.00, 'amount' => 220.00, 'description' => '回归测试报价', 'valid_hours' => 24, 'expire_time' => $now + 86400, 'status' => 'wait', 'createtime' => $now, ]); $createdQuoteIds[] = $quoteId; $orderRow = Db::name('order')->where('id', $orderId)->find(); $payRes = PaymentService::payOrderByBalance($orderRow, (int)$user['id'], 'quote', $quoteId); check('余额选标支付成功', !empty($payRes['paid']) && approx($payRes['amount'], 220.00), 'paid=' . ($payRes['amount'] ?? 'null')); $afterOrder = Db::name('order')->where('id', $orderId)->find(); check('选标后订单pay_amount=220.00', approx($afterOrder['pay_amount'], 220.00), $afterOrder['pay_amount']); check('选标后订单master_id已锁定', (int)$afterOrder['master_id'] === (int)$master['id'], $afterOrder['master_id']); $afterSettle = CommissionService::calcByOrder($afterOrder); check('选标后预计结算金额:师傅得200.00', approx($afterSettle['settle_amount'], 200.00), $afterSettle['settle_amount']); check('选标后预计平台抽佣:20.00', approx($afterSettle['commission'], 20.00), $afterSettle['commission']); // 验证微信回调路径通知也用净报价,同时模拟清理余额 Db::name('user')->where('id', (int)$user['id'])->update(['money' => $origUserMoney]); $createdPayIds = Db::name('pay_order')->where('order_id', $orderId)->column('id'); $createdLogIds = Db::name('order_log')->where('order_id', $orderId)->column('id'); $createdMsgIds = Db::name('message')->where('order_id', $orderId)->column('id'); $createdWalletLogIds = Db::name('wallet_log')->where('order_id', $orderId)->column('id'); } else { check('数据库存在测试账号与师傅记录', false, '跳过DB闭环'); } // 5. 真实 HTTP API 端到端链路测试(用户创单 -> 师傅报价 -> 双端详情/列表 -> 余额选标 -> 核销入账) echo "\n■ 5. 真实 HTTP API 端到端链路测试\n"; $apiBase = 'http://yjyf:9560/api'; $httpJson = function ($path, $method = 'GET', $data = null, $headers = []) use ($apiBase) { $opts = [ 'http' => [ 'method' => $method, 'timeout' => 5, 'ignore_errors' => true, 'header' => implode("\r\n", array_merge(['Content-Type: application/x-www-form-urlencoded'], $headers)), ], ]; if ($data !== null) { $opts['http']['content'] = http_build_query($data); } $raw = @file_get_contents($apiBase . $path, false, stream_context_create($opts)); return $raw ? json_decode($raw, true) : null; }; $stamp = substr((string)time(), -6) . mt_rand(10, 99); $uMobile = '139' . $stamp; $mMobile = '137' . $stamp; $uLogin = $httpJson('/auth/login', 'POST', ['mobile' => $uMobile, 'password' => 'QmTest@123']); $mMasterLogin = $httpJson('/master_auth/login', 'POST', ['mobile' => $mMobile, 'password' => 'QmTest@123']); $uIdNew = (int)($uLogin['data']['userinfo']['id'] ?? 0); $mIdNew = (int)($mMasterLogin['data']['masterinfo']['id'] ?? 0); $uTok = $uLogin['data']['token'] ?? ''; $mTok = $mMasterLogin['data']['token'] ?? ''; $addrIdNew = 0; if ($uIdNew > 0 && $mIdNew > 0 && $uTok !== '' && $mTok !== '') { // 给新用户设余额、支付密码,并使新师傅通过实名审核且处于听单状态 Db::name('user')->where('id', $uIdNew)->update(['money' => 500.00]); \app\common\library\AccountSecurity::ensure($uIdNew); Db::name('user_security')->where('user_id', $uIdNew)->update([ 'payment_password_hash' => password_hash('123456', PASSWORD_DEFAULT), ]); Db::name('master')->where('id', $mIdNew)->update([ 'realname' => 'E2E测试师傅', 'city' => '遵义市', 'lng' => '106.927', 'lat' => '27.725', 'work_status' => 'on', 'verify_status' => 'pass', 'money' => 0.00, ]); $addrRes = $httpJson('/address/save', 'POST', [ 'contact' => 'E2E测试', 'mobile' => $uMobile, 'province' => '贵州省', 'city' => '遵义市', 'district' => '汇川区', 'address' => '测试大道88号', 'floor' => 2, 'has_elevator' => 1, 'lng' => '106.927', 'lat' => '27.725', ], ['token: ' . $uTok]); $addrIdNew = (int)($addrRes['data']['id'] ?? 0); $svcRow = Db::name('service')->where('status', 'normal')->order('id asc')->find(); $ordCreate = $httpJson('/order/create', 'POST', [ 'service_id' => (int)$svcRow['id'], 'address_id' => $addrIdNew, 'price_type' => 'quote', ], ['token: ' . $uTok]); $ordIdNew = (int)($ordCreate['data']['order_id'] ?? 0); check('HTTP 创建报价单成功', $ordIdNew > 0, 'order_id=' . $ordIdNew); if ($ordIdNew > 0) { $createdOrderIds[] = $ordIdNew; $qSub = $httpJson('/master/quote', 'POST', [ 'order_id' => $ordIdNew, 'amount' => 200, 'description' => 'HTTP测试报价', 'valid_hours' => 24, ], ['token: ' . $mTok]); check('HTTP 师傅提交净报价200成功', ($qSub['code'] ?? 0) === 1, $qSub['msg'] ?? ''); $uDet = $httpJson('/order/detail?id=' . $ordIdNew, 'GET', null, ['token: ' . $uTok]); $uQuoteAmt = (float)($uDet['data']['quotes'][0]['amount'] ?? 0); check('HTTP 用户端详情看到加价后报价=220', approx($uQuoteAmt, 220.00), $uQuoteAmt); $mDetBefore = $httpJson('/master/orderDetail?id=' . $ordIdNew, 'GET', null, ['token: ' . $mTok]); $mQuoteAmt = (float)($mDetBefore['data']['my_quote']['amount'] ?? 0); $mQuoteFee = (float)($mDetBefore['data']['my_quote']['master_fee'] ?? 0); check('HTTP 师傅端详情my_quote仅显示200(无抽成感)', approx($mQuoteAmt, 200.00) && approx($mQuoteFee, 200.00), "amt={$mQuoteAmt}, fee={$mQuoteFee}"); $mQuotesRes = $httpJson('/master/quotes', 'GET', null, ['token: ' . $mTok]); $mQRow = $mQuotesRes['data']['list'][0] ?? null; check('HTTP 师傅端我的报价列表显示200/200', $mQRow && approx($mQRow['amount'], 200.00) && approx($mQRow['master_fee'], 200.00), $mQRow ? "{$mQRow['amount']}/{$mQRow['master_fee']}" : 'null'); $qIdNew = (int)($uDet['data']['quotes'][0]['id'] ?? 0); $chooseRes = $httpJson('/order/chooseQuote', 'POST', [ 'order_id' => $ordIdNew, 'quote_id' => $qIdNew, 'pay_type' => 'balance', 'payment_password' => '123456', ], ['token: ' . $uTok]); check('HTTP 用户选标支付托管220成功', ($chooseRes['code'] ?? 0) === 1 && approx($chooseRes['data']['amount'] ?? 0, 220.00), $chooseRes['msg'] ?? ''); $mOrdersRes = $httpJson('/master/orders?status=active', 'GET', null, ['token: ' . $mTok]); $mOrdRow = $mOrdersRes['data']['list'][0] ?? null; check('HTTP 师傅端我的订单列表master_fee=200(非220)', $mOrdRow && approx($mOrdRow['master_fee'], 200.00), $mOrdRow ? $mOrdRow['master_fee'] : 'null'); // 履约并核销结算 $httpJson('/master/contact', 'POST', ['order_id' => $ordIdNew], ['token: ' . $mTok]); $httpJson('/master/schedule', 'POST', ['order_id' => $ordIdNew, 'appoint_time' => time() + 3600], ['token: ' . $mTok]); $httpJson('/master/arrive', 'POST', ['order_id' => $ordIdNew, 'lng' => '106.927', 'lat' => '27.725'], ['token: ' . $mTok]); $httpJson('/master/startJob', 'POST', ['order_id' => $ordIdNew], ['token: ' . $mTok]); $httpJson('/master/finish', 'POST', ['order_id' => $ordIdNew, 'images' => ['https://example.com/1.jpg']], ['token: ' . $mTok]); $vCode = Db::name('order')->where('id', $ordIdNew)->value('verify_code'); $verRes = $httpJson('/master/verifyCode', 'POST', ['order_id' => $ordIdNew, 'verify_code' => $vCode], ['token: ' . $mTok]); check('HTTP 核销结算师傅实得=200.00', ($verRes['code'] ?? 0) === 1 && approx($verRes['data']['settle_amount'] ?? 0, 200.00) && approx($verRes['data']['balance'] ?? 0, 200.00), json_encode($verRes['data'] ?? [], JSON_UNESCAPED_UNICODE)); $settleDb = Db::name('settle')->where('order_id', $ordIdNew)->find(); check('HTTP 结算单落库(实付220/抽佣20/结算200)', $settleDb && approx($settleDb['order_amount'], 220.00) && approx($settleDb['commission'], 20.00) && approx($settleDb['settle_amount'], 200.00), $settleDb ? "{$settleDb['order_amount']}/{$settleDb['commission']}/{$settleDb['settle_amount']}" : 'null'); // 检查通知表:给师傅的选标通知不含220 $mNotifies = Db::name('message')->where('order_id', $ordIdNew)->where('receive_type', 'master')->select(); $noLeak220 = true; foreach ($mNotifies as $mn) { if (strpos((string)$mn['content'], '220') !== false) { $noLeak220 = false; } } check('HTTP 师傅站内消息未泄露用户加价金额220', $noLeak220, count($mNotifies) . '条'); // 清理 E2E 临时记录 Db::name('settle')->where('order_id', $ordIdNew)->delete(); Db::name('order_media')->where('order_id', $ordIdNew)->delete(); Db::name('order_quote')->where('order_id', $ordIdNew)->delete(); Db::name('pay_order')->where('order_id', $ordIdNew)->delete(); Db::name('order_log')->where('order_id', $ordIdNew)->delete(); Db::name('message')->where('order_id', $ordIdNew)->delete(); Db::name('wallet_log')->where('order_id', $ordIdNew)->delete(); Db::name('order')->where('id', $ordIdNew)->delete(); } if ($addrIdNew > 0) Db::name('address')->where('id', $addrIdNew)->delete(); Db::name('master_token')->where('master_id', $mIdNew)->delete(); Db::name('master')->where('id', $mIdNew)->delete(); Db::name('user_security')->where('user_id', $uIdNew)->delete(); Db::name('user_token')->where('user_id', $uIdNew)->delete(); Db::name('user')->where('id', $uIdNew)->delete(); } else { check('HTTP 登录创建测试账号成功', false, json_encode([$uLogin, $mMasterLogin], JSON_UNESCAPED_UNICODE)); } } finally { // 清理测试构造的数据 if ($createdWalletLogIds) { Db::name('wallet_log')->whereIn('id', $createdWalletLogIds)->delete(); } if ($createdMsgIds) { Db::name('message')->whereIn('id', $createdMsgIds)->delete(); } if ($createdLogIds) { Db::name('order_log')->whereIn('id', $createdLogIds)->delete(); } if ($createdPayIds) { Db::name('pay_order')->whereIn('id', $createdPayIds)->delete(); } if ($createdQuoteIds) { Db::name('order_quote')->whereIn('id', $createdQuoteIds)->delete(); } if ($createdOrderIds) { Db::name('order')->whereIn('id', $createdOrderIds)->delete(); } } echo "\n════════════════════════════════════════════════════════\n"; echo " 结果汇总: PASS {$pass} / FAIL {$fail}\n"; if ($fail > 0) { echo " 失败项:\n"; foreach ($fails as $f) { echo " - {$f}\n"; } } echo "════════════════════════════════════════════════════════\n"; exit($fail > 0 ? 1 : 0);