AdminListAccountTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace Tests\Integration;
  3. use App\Services\BackflowService;
  4. use App\Services\RebateService;
  5. use App\Services\RechargeService;
  6. use Illuminate\Config\Repository;
  7. use Illuminate\Database\Capsule\Manager;
  8. use Illuminate\Database\Schema\Blueprint;
  9. use Illuminate\Foundation\Application;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Facades\Facade;
  12. use PHPUnit\Framework\TestCase;
  13. /** 后台列表返回会员账号 account,并支持按 account 模糊筛选(关联查询与 join 查询两种写法)。 */
  14. class AdminListAccountTest extends TestCase
  15. {
  16. protected function setUp(): void
  17. {
  18. parent::setUp();
  19. Facade::clearResolvedInstances();
  20. $app = new Application(dirname(__DIR__, 2));
  21. $app->instance('config', new Repository(['app' => ['timezone' => 'Asia/Shanghai', 'url' => 'http://x']]));
  22. Facade::setFacadeApplication($app);
  23. $db = new Manager($app);
  24. $db->addConnection(['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => 'bot_']);
  25. $db->setAsGlobal();
  26. $db->bootEloquent();
  27. $app->instance('db', $db->getDatabaseManager());
  28. $app->bind('db.schema', fn () => $db->schema());
  29. $schema = $db->schema();
  30. $schema->create('users', function (Blueprint $t) {
  31. $t->id(); $t->string('member_id'); $t->string('account')->nullable(); $t->string('username')->nullable();
  32. $t->string('first_name')->nullable(); $t->string('admin_note')->nullable(); $t->integer('status')->default(0);
  33. $t->string('phone')->nullable(); $t->string('visitor_id')->nullable(); $t->string('register_ip')->nullable(); $t->timestamps();
  34. });
  35. $schema->create('rebates', function (Blueprint $t) {
  36. $t->id(); $t->string('member_id'); $t->date('date'); $t->integer('status')->default(0);
  37. $t->decimal('betting_amount', 20, 2)->default(0); $t->timestamps();
  38. });
  39. $schema->create('backflow', function (Blueprint $t) {
  40. $t->id(); $t->string('member_id'); $t->string('date'); $t->integer('status')->default(0);
  41. $t->decimal('amount', 20, 2)->default(0); $t->decimal('granted_amount', 20, 2)->default(0); $t->timestamps();
  42. });
  43. $schema->create('recharges', function (Blueprint $t) {
  44. $t->id(); $t->string('member_id'); $t->string('coin')->default('CNY'); $t->decimal('amount', 20, 2)->default(0);
  45. $t->integer('status')->default(0); $t->timestamps();
  46. });
  47. DB::table('users')->insert([
  48. ['member_id' => 'm1', 'account' => 'alice'],
  49. ['member_id' => 'm2', 'account' => 'bob'],
  50. ['member_id' => 'm3', 'account' => null],
  51. ]);
  52. }
  53. protected function tearDown(): void
  54. {
  55. DB::disconnect();
  56. Facade::clearResolvedInstances();
  57. parent::tearDown();
  58. }
  59. public function test_rebate_list_shows_and_filters_account(): void
  60. {
  61. foreach (['m1', 'm2', 'm3'] as $m) {
  62. DB::table('rebates')->insert(['member_id' => $m, 'date' => '2026-01-01']);
  63. }
  64. $rows = $this->accounts(RebateService::paginate(['limit' => 10])['data']);
  65. $this->assertSame(['m1' => 'alice', 'm2' => 'bob', 'm3' => ''], $rows);
  66. $this->assertSame(['m1' => 'alice'], $this->accounts(RebateService::paginate(['account' => 'lic'])['data']));
  67. $this->assertSame(0, RebateService::paginate(['account' => 'nobody'])['total']);
  68. }
  69. public function test_rebate_list_shows_the_current_day_but_only_past_unpaid_days_can_be_granted(): void
  70. {
  71. \Carbon\Carbon::setTestNow(\Carbon\Carbon::parse('2026-11-05 15:00:00', 'Asia/Shanghai')); // 纽约 2026-11-05 02:00
  72. try {
  73. DB::table('rebates')->insert([
  74. ['member_id' => 'm1', 'date' => '2026-11-05', 'status' => 0],
  75. ['member_id' => 'm2', 'date' => '2026-11-04', 'status' => 0],
  76. ['member_id' => 'm3', 'date' => '2026-11-04', 'status' => 1],
  77. ]);
  78. $rows = [];
  79. foreach (json_decode(json_encode(RebateService::paginate(['limit' => 10])['data']), true) as $row) {
  80. $rows[$row['member_id']] = [$row['date'], $row['can_grant']];
  81. }
  82. $this->assertSame(['m1' => ['2026-11-05', false], 'm2' => ['2026-11-04', true], 'm3' => ['2026-11-04', false]], $rows);
  83. } finally {
  84. \Carbon\Carbon::setTestNow();
  85. }
  86. }
  87. public function test_backflow_list_shows_and_filters_account(): void
  88. {
  89. foreach (['m1', 'm2'] as $m) {
  90. DB::table('backflow')->insert(['member_id' => $m, 'date' => '2026-01']);
  91. }
  92. $this->assertSame(['m1' => 'alice', 'm2' => 'bob'], $this->accounts(BackflowService::paginate(['limit' => 10])['data']));
  93. $this->assertSame(['m2' => 'bob'], $this->accounts(BackflowService::paginate(['limit' => 10, 'account' => 'bo'])['data']));
  94. }
  95. public function test_backflow_list_shows_granted_pending_and_whether_more_can_be_granted(): void
  96. {
  97. \Carbon\Carbon::setTestNow(\Carbon\Carbon::parse('2026-11-05 15:00:00', 'Asia/Shanghai'));
  98. try {
  99. DB::table('backflow')->insert([
  100. ['member_id' => 'm1', 'date' => '2026-11', 'status' => 0, 'amount' => 8, 'granted_amount' => 0], // 当月未发
  101. ['member_id' => 'm2', 'date' => '2026-11', 'status' => 2, 'amount' => 50, 'granted_amount' => 8], // 当月部分发放,可补发
  102. ['member_id' => 'm3', 'date' => '2026-11', 'status' => 2, 'amount' => 8, 'granted_amount' => 8], // 当月已发完,暂无差额
  103. ['member_id' => 'm1', 'date' => '2026-10', 'status' => 0, 'amount' => 0, 'granted_amount' => 0], // 往月未结清
  104. ['member_id' => 'm2', 'date' => '2026-10', 'status' => 1, 'amount' => 35, 'granted_amount' => 35], // 已发放
  105. ['member_id' => 'm1', 'date' => '2026-12', 'status' => 0, 'amount' => 0, 'granted_amount' => 0], // 未来月份不展示
  106. ]);
  107. $rows = [];
  108. foreach (json_decode(json_encode(BackflowService::paginate(['limit' => 10])['data']), true) as $row) {
  109. $rows[$row['member_id'] . '|' . $row['date']] = [$row['granted_amount'], $row['pending_amount'], $row['can_grant'], $row['status_text']];
  110. }
  111. ksort($rows);
  112. $this->assertSame([
  113. 'm1|2026-10' => ['0.00', '0.00', true, '未发放'],
  114. 'm1|2026-11' => ['0.00', '8.00', true, '未发放'],
  115. 'm2|2026-10' => ['35.00', '0.00', false, '已发放'],
  116. 'm2|2026-11' => ['8.00', '42.00', true, '部分发放'],
  117. 'm3|2026-11' => ['8.00', '0.00', false, '部分发放'],
  118. ], $rows);
  119. $this->assertSame(2, BackflowService::paginate(['limit' => 10, 'status' => 2])['total']);
  120. } finally {
  121. \Carbon\Carbon::setTestNow();
  122. }
  123. }
  124. public function test_recharge_list_shows_and_filters_account(): void
  125. {
  126. foreach (['m1', 'm2'] as $m) {
  127. DB::table('recharges')->insert(['member_id' => $m, 'amount' => 1, 'created_at' => '2026-01-01 00:00:00']);
  128. }
  129. $this->assertSame(['m1' => 'alice', 'm2' => 'bob'], $this->accounts(RechargeService::paginate([])['data']));
  130. $this->assertSame(['m1' => 'alice'], $this->accounts(RechargeService::paginate(['account' => 'ALI'])['data']));
  131. }
  132. private function accounts(array $rows): array
  133. {
  134. $out = [];
  135. foreach (json_decode(json_encode($rows), true) as $row) {
  136. $out[$row['member_id']] = $row['account'];
  137. }
  138. ksort($out);
  139. return $out;
  140. }
  141. }