OperationExchangeListTest.php 21 KB

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