MemberFundTotalsTest.php 5.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace Tests\Integration;
  3. use App\Http\Controllers\admin\User;
  4. use Illuminate\Config\Repository;
  5. use Illuminate\Database\Capsule\Manager;
  6. use Illuminate\Foundation\Application;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Facade;
  10. use PHPUnit\Framework\TestCase;
  11. class MemberFundTotalsTest extends TestCase
  12. {
  13. private Application $app;
  14. protected function setUp(): void
  15. {
  16. Facade::clearResolvedInstances();
  17. $this->app = new Application(dirname(__DIR__, 2));
  18. $this->app->instance('config', new Repository(['app' => ['locale' => 'zh']]));
  19. $this->app->instance('log', new \Psr\Log\NullLogger());
  20. $translator = new \Illuminate\Translation\Translator(new \Illuminate\Translation\ArrayLoader(), 'zh');
  21. $this->app->instance('translator', $translator);
  22. $this->app->instance('validator', new \Illuminate\Validation\Factory($translator, $this->app));
  23. $this->app->instance(\Illuminate\Contracts\Routing\ResponseFactory::class, new \Tests\Support\JsonResponseFactory());
  24. Facade::setFacadeApplication($this->app);
  25. $db = new Manager($this->app);
  26. $db->addConnection(['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => 'bot_']);
  27. $db->setAsGlobal(); $db->bootEloquent(); $this->app->instance('db', $db->getDatabaseManager());
  28. $this->app->bind('db.schema', fn () => $db->schema());
  29. DB::statement('CREATE TABLE bot_balance_logs (id INTEGER PRIMARY KEY,member_id TEXT,type INTEGER,change_type TEXT,amount DECIMAL(30,10),related_id INTEGER,frozen_status INTEGER,created_at TEXT)');
  30. DB::statement('CREATE TABLE bot_users (id INTEGER PRIMARY KEY,member_id TEXT,user_type INTEGER DEFAULT 0)');
  31. DB::statement('CREATE TABLE bot_withdraws (id INTEGER PRIMARY KEY,status INTEGER)');
  32. DB::statement('CREATE TABLE bot_payment_orders (id INTEGER PRIMARY KEY,status INTEGER)');
  33. DB::table('users')->insert([['member_id' => 'm1', 'user_type' => 0], ['member_id' => 'empty', 'user_type' => 0], ['member_id' => 'sim', 'user_type' => 2]]);
  34. DB::table('withdraws')->insert([['id' => 23, 'status' => 1], ['id' => 25, 'status' => 0]]);
  35. DB::table('payment_orders')->insert([['id' => 21, 'status' => 3], ['id' => 22, 'status' => 2], ['id' => 24, 'status' => 2], ['id' => 26, 'status' => 5]]);
  36. Request::macro('validate', function (array $rules) { return app('validator')->make($this->all(), $rules)->validate(); });
  37. }
  38. protected function tearDown(): void
  39. {
  40. DB::disconnect(); Facade::clearResolvedInstances(); parent::tearDown();
  41. }
  42. private function totals(array $input): array
  43. {
  44. $this->app->instance('request', Request::create('/admin/user/fundTotals', 'GET', $input));
  45. return (new User())->fundTotals()->getData(true);
  46. }
  47. public function test_totals_group_deposits_withdrawals_and_bonuses_for_one_member(): void
  48. {
  49. $rows = [
  50. // 总充值:三方、前台提交审核通过、后台人工充值、其他;冲正的负数会抵减
  51. ['三方充值', 100, 11], ['充值', 200, 12], ['人工充值', 50, null], ['其他', 7, null], ['充值', -20, 12],
  52. // 总提现:只算成功的。21 失败已退款、25 和 26 还在审核,都不算
  53. ['三方提现', -300, 21], ['三方提现', 300, 21], ['三方提现', -80, 22], ['提现', -40, 23], ['人工提现', -10, 24], ['人工扣款', -5, null],
  54. ['提现', -70, 25], ['三方提现', -90, 26],
  55. // 总赠送彩金:后台三种加款、活动、充值送、返水、分享佣金、自动发放的人工充值(related_id 不为空);收回的负数会抵减
  56. ['注册赠送', 18, null], ['优惠活动', 6.66, null], ['神秘彩金', 8, null], ['即充即送', 5, null], ['充值返现', 3, null],
  57. ['笔笔返', 1.5, null], ['回水', 2, null], ['返水', 1, null], ['老用户回归', 4, null], ['分享佣金领取', 9, 31], ['人工充值', 28, 0],
  58. ['优惠活动', -1.66, null],
  59. // 不算进任何一项
  60. ['订单补偿', 60, null], ['余额宝利息', 2, null], ['加拿大28投注', -30, 41], ['资产转移', -20, null], ['流水解冻', 6, null],
  61. ];
  62. foreach ($rows as [$type, $amount, $related]) {
  63. DB::table('balance_logs')->insert(['member_id' => 'm1', 'type' => 1, 'change_type' => $type, 'amount' => $amount, 'related_id' => $related, 'created_at' => '2026-11-05 12:00:00']);
  64. }
  65. // 还冻结着的旧奖励不算已赠送,解冻过的算
  66. DB::table('balance_logs')->insert([
  67. ['member_id' => 'm1', 'type' => 2, 'change_type' => '即充即送', 'amount' => 500, 'frozen_status' => 1, 'created_at' => '2026-11-05 12:00:00'],
  68. ['member_id' => 'm1', 'type' => 2, 'change_type' => '老用户回归', 'amount' => 10, 'frozen_status' => 2, 'created_at' => '2026-11-05 12:00:00'],
  69. ['member_id' => 'sim', 'type' => 1, 'change_type' => '三方充值', 'amount' => 999, 'frozen_status' => null, 'created_at' => '2026-11-05 12:00:00'],
  70. ]);
  71. $body = $this->totals(['member_id' => 'm1']);
  72. $this->assertSame(0, $body['code'], $body['msg']);
  73. $this->assertSame(['total_deposit' => '337.00', 'total_withdraw' => '135.00', 'total_bonus' => '94.50'], $body['data']);
  74. $this->assertSame(['total_deposit' => '0.00', 'total_withdraw' => '0.00', 'total_bonus' => '0.00'], $this->totals(['member_id' => 'empty'])['data']);
  75. foreach ([[], ['member_id' => 'nobody'], ['member_id' => 'sim']] as $input) $this->assertNotSame(0, $this->totals($input)['code'], 'unknown and simulated members are refused');
  76. }
  77. }