AgentDashboardTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace Tests\Integration;
  3. use App\Http\Controllers\agent\ProfileController;
  4. use App\Services\Agent\AgentReportService;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\DB;
  7. use PHPUnit\Framework\TestCase;
  8. use Tests\Support\AgentTestEnvironment;
  9. /** 代理首页 GET /agent/dashboard:只统计本代理名下会员,按日期汇总资金和投注。 */
  10. class AgentDashboardTest extends TestCase
  11. {
  12. use AgentTestEnvironment;
  13. public function test_dashboard_sums_only_own_members_within_the_date_range(): void
  14. {
  15. $agent = $this->agent();
  16. $other = $this->agent();
  17. foreach (['m1' => $agent, 'm2' => $agent, 'x1' => $other] as $memberId => $owner) {
  18. DB::table('users')->insert(['member_id' => $memberId, 'username' => $memberId, 'agent_id' => $owner->id, 'created_at' => now()]);
  19. DB::table('wallets')->insert(['member_id' => $memberId, 'available_balance' => 100]);
  20. }
  21. $in = '2026-09-07 10:00:00';
  22. $out = '2026-09-01 10:00:00';
  23. foreach ([['m1', 500, $in], ['m2', 300, $in], ['m1', 999, $out], ['x1', 777, $in]] as [$member, $amount, $at]) {
  24. DB::table('recharges')->insert(['member_id' => $member, 'status' => 1, 'amount' => $amount, 'created_at' => $at]);
  25. }
  26. DB::table('recharges')->insert(['member_id' => 'm1', 'status' => 0, 'amount' => 50, 'created_at' => $in]); // 未到账
  27. DB::table('payment_orders')->insert(['member_id' => 'm2', 'status' => 2, 'type' => '1', 'amount' => 200, 'created_at' => $in]);
  28. DB::table('withdraws')->insert(['member_id' => 'm1', 'status' => 1, 'amount' => 150, 'created_at' => $in]);
  29. foreach ([['人工充值', null, 80], ['人工扣款', null, -30], ['优惠活动', null, 20], ['返水', null, 10], ['人工充值', 0, 5]] as [$type, $related, $amount]) {
  30. DB::table('balance_logs')->insert(['member_id' => 'm1', 'change_type' => $type, 'related_id' => $related, 'amount' => $amount, 'created_at' => $in]);
  31. }
  32. $request = Request::create('/agent/dashboard', 'GET', ['start_date' => '2026-09-07', 'end_date' => '2026-09-07']);
  33. $request->attributes->set('agent', $agent);
  34. $this->app->instance('request', $request);
  35. $data = (new ProfileController())->dashboard($request, new AgentReportService())->getData(true);
  36. $this->assertSame(0, $data['code'], (string) $data['msg']);
  37. $summary = $data['data'];
  38. $this->assertSame(2, $summary['member_count']);
  39. $this->assertSame('200.0000', $summary['member_balance']);
  40. $this->assertSame('1000.0000', $summary['deposit_amount'], 'recharges + paid online orders in range');
  41. $this->assertSame('150.0000', $summary['withdraw_amount']);
  42. $this->assertSame('80.0000', $summary['manual_credit']);
  43. $this->assertSame('30.0000', $summary['manual_debit']);
  44. $this->assertSame('25.0000', $summary['bonus_amount'], '优惠活动 + 人工充值(赠送)');
  45. $this->assertSame('10.0000', $summary['rebate_amount']);
  46. $this->assertSame('815.0000', $summary['company_profit'], '1000 - 150 - 25 - 10');
  47. $this->assertSame('0.0000', $summary['commission_amount']);
  48. $this->assertSame(0, $summary['agent_count']);
  49. foreach (['bet_count', 'bet_amount', 'valid_bet_amount', 'win_loss'] as $field) $this->assertArrayHasKey($field, $summary);
  50. $request = Request::create('/agent/dashboard', 'GET', ['start_date' => '2026-09-08', 'end_date' => '2026-09-07']);
  51. $request->attributes->set('agent', $agent);
  52. $this->assertNotSame(0, (new ProfileController())->dashboard($request, new AgentReportService())->getData(true)['code']);
  53. }
  54. }