OperationExchangeListTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. namespace Tests\Integration;
  3. use App\Http\Controllers\admin\Operation;
  4. use App\Services\Sharing\ShareDefaults;
  5. use Carbon\Carbon;
  6. use Illuminate\Config\Repository;
  7. use Illuminate\Database\Capsule\Manager;
  8. use Illuminate\Foundation\Application;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Facades\Facade;
  12. use Illuminate\Translation\ArrayLoader;
  13. use Illuminate\Translation\Translator;
  14. use Illuminate\Validation\Factory;
  15. use PHPUnit\Framework\Attributes\DataProvider;
  16. use PHPUnit\Framework\TestCase;
  17. /** Real controller and source orders, isolated from the application database and .env. */
  18. class OperationExchangeListTest extends TestCase
  19. {
  20. private Application $app;
  21. protected function setUp(): void
  22. {
  23. parent::setUp();
  24. Facade::clearResolvedInstances();
  25. $this->app = new Application(dirname(__DIR__, 2));
  26. $this->app->instance('config', new Repository(['app' => ['locale' => 'zh', 'timezone' => 'Asia/Shanghai']]));
  27. Facade::setFacadeApplication($this->app);
  28. $db = new Manager($this->app);
  29. $db->addConnection(['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => 'bot_']);
  30. $db->setAsGlobal();
  31. $db->bootEloquent();
  32. $this->app->instance('db', $db->getDatabaseManager());
  33. $translator = new Translator(new ArrayLoader(), 'zh');
  34. $this->app->instance('translator', $translator);
  35. $this->app->instance('validator', new Factory($translator, $this->app));
  36. $this->app->instance(\Illuminate\Contracts\Routing\ResponseFactory::class, new \Tests\Support\JsonResponseFactory());
  37. Request::macro('validate', function (array $rules) {
  38. return app('validator')->make($this->all(), $rules)->validate();
  39. });
  40. foreach ([
  41. 'users' => 'id INTEGER PRIMARY KEY, user_id TEXT, member_id TEXT, account TEXT, first_name TEXT, "from" INTEGER DEFAULT 1, created_at TEXT',
  42. 'wallets' => 'id INTEGER PRIMARY KEY, member_id TEXT, available_balance NUMERIC DEFAULT 0',
  43. 'balance_logs' => 'id INTEGER PRIMARY KEY, member_id TEXT, change_type TEXT, amount NUMERIC, created_at TEXT',
  44. 'third_game_orders' => 'id INTEGER PRIMARY KEY, user_id INTEGER, member_id TEXT, game_type INTEGER, status INTEGER DEFAULT 1, bet_amount NUMERIC DEFAULT 10, bet_time TEXT',
  45. 'sport_order' => 'id INTEGER PRIMARY KEY, user_id TEXT, amount NUMERIC DEFAULT 10, status INTEGER DEFAULT 1, pay_status INTEGER DEFAULT 1, return_status INTEGER DEFAULT 0, is_faker INTEGER DEFAULT 0, create_time INTEGER',
  46. 'sport_game_order' => 'id INTEGER PRIMARY KEY, member_id TEXT, amount NUMERIC DEFAULT 10, status INTEGER DEFAULT 1, created_at TEXT',
  47. 'lhc_order' => 'id INTEGER PRIMARY KEY, member_id TEXT, amount NUMERIC DEFAULT 10, lottery_status INTEGER DEFAULT 1, is_faker INTEGER DEFAULT 0, created_at INTEGER',
  48. 'bets' => 'id INTEGER PRIMARY KEY, member_id TEXT, amount NUMERIC DEFAULT 10, status INTEGER DEFAULT 2, created_at TEXT',
  49. 'jisu_game_order' => 'id INTEGER PRIMARY KEY, member_id TEXT, amount NUMERIC DEFAULT 10, status INTEGER DEFAULT 1, created_at TEXT',
  50. ] as $table => $columns) {
  51. DB::statement('CREATE TABLE bot_' . $table . ' (' . $columns . ')');
  52. }
  53. foreach ([['100', 'alice', 1], ['200', 'bob', 1], ['300', 'robot', 2]] as $i => [$member, $account, $from]) {
  54. DB::table('users')->insert(['id' => $i + 1, 'user_id' => $member, 'member_id' => $member,
  55. 'account' => $account, 'first_name' => $account, 'from' => $from, 'created_at' => '2026-11-0' . ($i + 1) . ' 12:00:00']);
  56. DB::table('wallets')->insert(['member_id' => $member, 'available_balance' => 88]);
  57. }
  58. }
  59. protected function tearDown(): void
  60. {
  61. DB::disconnect();
  62. Facade::clearResolvedInstances();
  63. parent::tearDown();
  64. }
  65. private function response(array $params = []): array
  66. {
  67. $this->app->instance('request', Request::create('/admin/operation/exchangeList', 'GET', $params));
  68. return (new Operation())->exchangeList()->getData(true);
  69. }
  70. private function rows(array $params = []): array
  71. {
  72. $body = $this->response($params);
  73. $this->assertSame(0, $body['code'], $body['msg']);
  74. return array_column($body['data']['data'], null, 'member_id');
  75. }
  76. private function order(string $table, string $member = '100', array $extra = [], string $at = '2026-11-04 12:00:00'): void
  77. {
  78. $timestamp = in_array($table, ['sport_order', 'lhc_order'], true);
  79. $memberColumn = $table === 'sport_order' ? 'user_id' : 'member_id';
  80. $timeColumn = $table === 'sport_order' ? 'create_time' : ($table === 'third_game_orders' ? 'bet_time' : 'created_at');
  81. DB::table($table)->insert($extra + [$memberColumn => $member,
  82. $timeColumn => $timestamp ? Carbon::parse($at, 'Asia/Shanghai')->timestamp : $at]);
  83. }
  84. private function funds(string $type, string $amount, string $member = '100', string $at = '2026-11-04 12:00:00'): void
  85. {
  86. DB::table('balance_logs')->insert(['member_id' => $member, 'change_type' => $type, 'amount' => $amount, 'created_at' => $at]);
  87. }
  88. public function test_all_nine_commission_categories_are_deduplicated_and_old_orders_are_included(): void
  89. {
  90. foreach (range(1, 7) as $type) $this->order('third_game_orders', extra: ['game_type' => $type]);
  91. $this->order('third_game_orders', extra: ['game_type' => 1]);
  92. $this->order('sport_order');
  93. $this->order('sport_game_order');
  94. // No sharing facts, child orders or legacy ID cutoff are needed to recognize historical play.
  95. $this->order('lhc_order', extra: ['id' => 1]);
  96. $this->order('bets');
  97. $this->order('jisu_game_order');
  98. $rows = $this->rows();
  99. $this->assertSame(ShareDefaults::categories(), array_column($rows['100']['played_games'], 'label', 'category'));
  100. $this->assertSame([], $rows['200']['played_games']);
  101. $this->assertArrayNotHasKey('300', $rows);
  102. }
  103. public function test_cancelled_unpaid_fake_zero_stake_and_unknown_games_do_not_count_as_play(): void
  104. {
  105. $this->order('third_game_orders', extra: ['game_type' => 1, 'status' => 2]);
  106. $this->order('third_game_orders', extra: ['game_type' => 2, 'status' => 3]);
  107. $this->order('third_game_orders', extra: ['game_type' => 8]);
  108. $this->order('third_game_orders', extra: ['game_type' => 3, 'bet_amount' => 0]);
  109. $this->order('sport_order', extra: ['pay_status' => 0]);
  110. $this->order('sport_order', extra: ['return_status' => 2]);
  111. $this->order('sport_order', extra: ['is_faker' => 1]);
  112. $this->order('sport_game_order', extra: ['status' => 3]);
  113. $this->order('lhc_order', extra: ['lottery_status' => 3]);
  114. $this->order('lhc_order', extra: ['lottery_status' => 4]);
  115. $this->order('lhc_order', extra: ['is_faker' => 1]);
  116. $this->order('bets', extra: ['status' => 3]);
  117. $this->order('jisu_game_order', extra: ['status' => 3]);
  118. $this->assertSame([], $this->rows()['100']['played_games']);
  119. }
  120. public function test_pending_bets_count_without_waiting_for_commission_settlement(): void
  121. {
  122. $this->order('third_game_orders', extra: ['game_type' => 1, 'status' => 0]);
  123. $this->order('sport_order');
  124. $this->order('sport_game_order', extra: ['status' => 0]);
  125. $this->order('lhc_order', extra: ['lottery_status' => 0]);
  126. $this->order('bets', extra: ['status' => 1]);
  127. $this->order('jisu_game_order', extra: ['status' => 0]);
  128. $this->assertSame(['third:1', 'self:sport', 'self:lottery'], array_column($this->rows()['100']['played_games'], 'category'));
  129. }
  130. public function test_third_party_identity_supports_user_id_only_and_does_not_mix_members(): void
  131. {
  132. $this->order('third_game_orders', extra: ['user_id' => 1, 'member_id' => null, 'game_type' => 1]);
  133. $this->order('third_game_orders', '200', ['user_id' => 2, 'game_type' => 2]);
  134. $this->order('third_game_orders', '100', ['user_id' => 2, 'game_type' => 3]); // conflicting identity
  135. $rows = $this->rows();
  136. $this->assertSame(['third:1'], array_column($rows['100']['played_games'], 'category'));
  137. $this->assertSame(['third:2'], array_column($rows['200']['played_games'], 'category'));
  138. }
  139. public static function gameSources(): array
  140. {
  141. return array_map(fn ($table) => [$table], ['third_game_orders', 'sport_order', 'sport_game_order', 'lhc_order', 'bets', 'jisu_game_order']);
  142. }
  143. #[DataProvider('gameSources')]
  144. public function test_each_game_source_respects_the_selected_dates(string $table): void
  145. {
  146. $extra = $table === 'third_game_orders' ? ['game_type' => 1] : [];
  147. $this->order($table, extra: $extra, at: '2026-11-03 23:59:59');
  148. $this->order($table, extra: $extra, at: '2026-11-05 00:00:00');
  149. $params = ['start_time' => '2026-11-04', 'end_time' => '2026-11-04'];
  150. $this->assertSame([], $this->rows($params)['100']['played_games']);
  151. $this->order($table, extra: $extra, at: '2026-11-04 00:00:00');
  152. $this->assertCount(1, $this->rows($params)['100']['played_games']);
  153. }
  154. public function test_profit_is_recharge_minus_withdraw_and_deducts_refunded_withdrawals(): void
  155. {
  156. $this->funds('充值', '1000.11');
  157. $this->funds('人工充值', '200.22');
  158. $this->funds('三方充值', '300.33');
  159. $this->funds('提现', '-2000.44');
  160. $this->funds('三方提现', '-500.55');
  161. $this->funds('三方提现', '500.55'); // failed withdrawal returned in full
  162. $this->funds('优惠活动', '9999');
  163. $this->funds('人工扣款', '-9999');
  164. $this->funds('充值', '8888', '300');
  165. $row = $this->rows()['100'];
  166. $this->assertSame('1,500.66', $row['recharge']);
  167. $this->assertSame('2,000.44', $row['withdraw']);
  168. $this->assertSame('-499.78', $row['withdraw_recharge_difference']);
  169. $this->funds('提现', '-100', '200');
  170. $this->funds('充值', '500', '200');
  171. $this->assertSame('400.00', $this->rows()['200']['withdraw_recharge_difference']);
  172. }
  173. public function test_date_filters_apply_to_money_categories_and_existing_order_amount_at_day_boundaries(): void
  174. {
  175. foreach (['2026-11-04 00:00:00', '2026-11-04 23:59:59'] as $at) {
  176. $this->order('sport_order', at: $at);
  177. $this->order('lhc_order', at: $at);
  178. $this->funds('充值', '100', at: $at);
  179. }
  180. $this->order('third_game_orders', extra: ['game_type' => 1], at: '2026-11-03 23:59:59');
  181. $this->order('third_game_orders', extra: ['game_type' => 2], at: '2026-11-05 00:00:00');
  182. $this->order('sport_order', at: '2026-11-03 23:59:59');
  183. $this->order('sport_order', at: '2026-11-05 00:00:00');
  184. $this->funds('充值', '999', at: '2026-11-03 23:59:59');
  185. $this->funds('提现', '-999', at: '2026-11-05 00:00:00');
  186. $this->funds('提现', '-80');
  187. $row = $this->rows(['start_time' => '2026-11-04', 'end_time' => '2026-11-04'])['100'];
  188. $this->assertSame('200.00', $row['recharge']);
  189. $this->assertSame('80.00', $row['withdraw']);
  190. $this->assertSame('120.00', $row['withdraw_recharge_difference']);
  191. $this->assertSame('20.00', $row['order_amount']);
  192. $this->assertSame(['self:sport', 'self:lottery'], array_column($row['played_games'], 'category'));
  193. $this->assertCount(4, $this->rows()['100']['played_games']);
  194. }
  195. public function test_empty_users_default_to_zero_and_filters_and_pagination_keep_the_response_shape(): void
  196. {
  197. $row = $this->rows(['account' => 'lic'])['100'];
  198. foreach (['recharge', 'withdraw', 'withdraw_recharge_difference', 'order_amount'] as $key) $this->assertSame('0.00', $row[$key]);
  199. $this->assertSame([], $row['played_games']);
  200. $this->assertSame([], $this->rows(['account' => 'missing']));
  201. $body = $this->response(['page' => 2, 'limit' => 1]);
  202. $this->assertSame(2, $body['data']['total']);
  203. $this->assertSame(['100'], array_column($body['data']['data'], 'member_id'));
  204. $this->assertNotSame(0, $this->response(['start_time' => '2026-11-04'])['code']);
  205. }
  206. private function sortingMembers(): void
  207. {
  208. foreach (['400' => 'carol', '500' => 'dave'] as $member => $account) {
  209. DB::table('users')->insert(['user_id' => (string) $member, 'member_id' => (string) $member,
  210. 'account' => $account, 'first_name' => $account, 'created_at' => '2026-11-04 12:00:00']);
  211. DB::table('wallets')->insert(['member_id' => (string) $member]);
  212. }
  213. $this->funds('充值', '2000.10');
  214. $this->funds('提现', '-50.10');
  215. $this->funds('三方充值', '100.00', '200');
  216. $this->funds('三方提现', '-1500.01', '200');
  217. $this->funds('三方提现', '-9000', '200');
  218. $this->funds('三方提现', '9000', '200'); // refunded, must not raise the withdrawal ranking
  219. $this->funds('人工充值', '900', '400');
  220. $this->funds('提现', '-100', '400');
  221. $this->funds('充值', '999999', '300'); // virtual user
  222. foreach (['100' => '9', '200' => '1000', '400' => '90', '500' => '0'] as $member => $balance) {
  223. DB::table('wallets')->where('member_id', (string) $member)->update(['available_balance' => $balance]);
  224. }
  225. }
  226. public static function amountSorts(): array
  227. {
  228. $cases = [];
  229. foreach ([
  230. 'recharge' => ['500', '200', '400', '100'],
  231. 'withdraw' => ['500', '100', '400', '200'],
  232. 'withdraw_recharge_difference' => ['200', '500', '400', '100'],
  233. 'money' => ['500', '100', '400', '200'],
  234. ] as $field => $ascending) {
  235. $cases[$field . '_asc'] = [$field, 'asc', $ascending];
  236. $cases[$field . '_desc'] = [$field, 'desc', array_reverse($ascending)];
  237. }
  238. return $cases;
  239. }
  240. #[DataProvider('amountSorts')]
  241. public function test_amount_sorting_is_numeric_and_precedes_pagination(string $field, string $direction, array $expected): void
  242. {
  243. $this->sortingMembers();
  244. $actual = [];
  245. foreach ([1, 2] as $page) {
  246. $body = $this->response(['sort_field' => $field, 'sort_order' => $direction, 'limit' => 2, 'page' => $page]);
  247. $this->assertSame(0, $body['code'], $body['msg']);
  248. $this->assertSame(4, $body['data']['total']);
  249. array_push($actual, ...array_column($body['data']['data'], 'member_id'));
  250. }
  251. $this->assertSame($expected, $actual);
  252. }
  253. public function test_amount_sorting_uses_selected_dates_and_member_filters(): void
  254. {
  255. $this->sortingMembers();
  256. $this->funds('充值', '100000', '200', '2026-11-03 12:00:00');
  257. $this->funds('提现', '-100000', '100', '2026-11-05 12:00:00');
  258. $dates = ['start_time' => '2026-11-04', 'end_time' => '2026-11-04'];
  259. foreach (['recharge' => '100', 'withdraw' => '200', 'withdraw_recharge_difference' => '100'] as $field => $member) {
  260. $body = $this->response($dates + ['sort_field' => $field, 'limit' => 1]);
  261. $this->assertSame(0, $body['code'], $body['msg']);
  262. $this->assertSame($member, $body['data']['data'][0]['member_id']);
  263. }
  264. $body = $this->response(['sort_field' => 'recharge', 'limit' => 1]);
  265. $this->assertSame('200', $body['data']['data'][0]['member_id']);
  266. $this->assertSame(['400'], array_column($this->response($dates + ['sort_field' => 'recharge', 'account' => 'car'])['data']['data'], 'member_id'));
  267. }
  268. public function test_equal_amounts_have_stable_order_across_pages_and_default_order_is_preserved(): void
  269. {
  270. DB::table('users')->insert(['id' => 4, 'user_id' => '400', 'member_id' => '400',
  271. 'account' => 'carol', 'first_name' => 'carol', 'created_at' => '2026-11-02 12:00:00']);
  272. DB::table('wallets')->insert(['member_id' => '400', 'available_balance' => 88]);
  273. foreach (['recharge', 'withdraw', 'withdraw_recharge_difference', 'money'] as $field) {
  274. $members = [];
  275. foreach ([1, 2, 3] as $page) {
  276. $body = $this->response(['sort_field' => $field, 'sort_order' => 'asc', 'limit' => 1, 'page' => $page]);
  277. $this->assertSame(0, $body['code'], $body['msg']);
  278. $members[] = $body['data']['data'][0]['member_id'];
  279. }
  280. $this->assertSame(['400', '200', '100'], $members);
  281. }
  282. $this->assertSame(['400', '200', '100'], array_column($this->response()['data']['data'], 'member_id'));
  283. }
  284. public static function invalidSorts(): array
  285. {
  286. return [
  287. [['sort_field' => 'created_at']],
  288. [['sort_field' => 'money desc, users.id']],
  289. [['sort_field' => ['money']]],
  290. [['sort_field' => 'money', 'sort_order' => 'descending']],
  291. [['sort_field' => 'money', 'sort_order' => ['asc']]],
  292. ];
  293. }
  294. #[DataProvider('invalidSorts')]
  295. public function test_invalid_sort_fields_and_directions_are_rejected(array $params): void
  296. {
  297. $this->assertNotSame(0, $this->response($params)['code']);
  298. }
  299. }