AgentApiRegressionTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <?php
  2. namespace Tests\Integration;
  3. use App\Http\Controllers\admin\Agent as AdminAgent;
  4. use App\Http\Controllers\AgentApiController;
  5. use App\Http\Middleware\AgentAuthMiddleware;
  6. use App\Models\Agent\Agent;
  7. use App\Models\Agent\AgentBalanceLog;
  8. use App\Models\Agent\AgentDailyGameStat;
  9. use App\Models\Agent\AgentFlowCommissionProfile;
  10. use App\Models\Agent\AgentLoginLog;
  11. use App\Models\Agent\AgentRebateRecord;
  12. use App\Models\Agent\AgentToken;
  13. use App\Models\Agent\AgentWithdrawAccount;
  14. use App\Models\Agent\AgentWithdrawal;
  15. use App\Services\Agent\AgentCommissionService;
  16. use App\Services\Agent\AgentFlowCommissionAssignmentService;
  17. use App\Services\Agent\AgentMemberService;
  18. use App\Services\Agent\AgentProfitCommissionAssignmentService;
  19. use App\Services\Agent\AgentReportService;
  20. use App\Services\Agent\AgentService;
  21. use App\Services\Agent\AgentWalletService;
  22. use App\Services\Agent\AgentWithdrawalService;
  23. use App\Services\PaymentOrderService;
  24. use Carbon\Carbon;
  25. use Illuminate\Config\Repository;
  26. use Illuminate\Contracts\Debug\ExceptionHandler;
  27. use Illuminate\Database\Capsule\Manager;
  28. use Illuminate\Database\QueryException;
  29. use Illuminate\Database\Schema\Blueprint;
  30. use Illuminate\Events\Dispatcher;
  31. use Illuminate\Foundation\Application;
  32. use Illuminate\Hashing\BcryptHasher;
  33. use Illuminate\Http\JsonResponse;
  34. use Illuminate\Http\Request;
  35. use Illuminate\Support\Facades\DB;
  36. use Illuminate\Support\Facades\Facade;
  37. use Illuminate\Support\Facades\Schema;
  38. use Illuminate\Translation\ArrayLoader;
  39. use Illuminate\Translation\Translator;
  40. use Illuminate\Validation\Factory;
  41. use PHPUnit\Framework\TestCase;
  42. class AgentApiRegressionTest extends TestCase
  43. {
  44. private Application $app;
  45. private AgentService $agents;
  46. protected function setUp(): void
  47. {
  48. parent::setUp();
  49. Carbon::setTestNow(Carbon::parse('2026-09-07 12:00:00'));
  50. Facade::clearResolvedInstances();
  51. $this->app = new Application(dirname(__DIR__, 2));
  52. $this->app->instance('config', new Repository(['app' => ['locale' => 'zh'], 'agent' => []]));
  53. Facade::setFacadeApplication($this->app);
  54. $db = new Manager($this->app);
  55. $db->addConnection(['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => 'bot_']);
  56. $db->setEventDispatcher(new Dispatcher($this->app));
  57. $db->setAsGlobal();
  58. $db->bootEloquent();
  59. $this->app->instance('db', $db->getDatabaseManager());
  60. $this->app->bind('db.schema', fn () => $db->schema());
  61. $this->app->instance('hash', new BcryptHasher(['rounds' => 4]));
  62. $translator = new Translator(new ArrayLoader(), 'zh');
  63. $this->app->instance('translator', $translator);
  64. $this->app->instance('validator', new Factory($translator, $this->app));
  65. $this->app->instance('request', Request::create('/'));
  66. Request::macro('validate', function (array $rules) {
  67. return app('validator')->make($this->all(), $rules)->validate();
  68. });
  69. $responses = new class {
  70. public function json($data, $status = 200, $headers = [], $options = 0) {
  71. return new JsonResponse($data, $status, $headers, $options);
  72. }
  73. };
  74. $this->app->instance(\Illuminate\Contracts\Routing\ResponseFactory::class, $responses);
  75. Schema::create('users', function (Blueprint $t) {
  76. $t->id(); $t->string('member_id'); $t->string('username'); $t->string('first_name')->default('');
  77. $t->integer('status')->default(1); $t->string('register_ip')->default('');
  78. $t->string('register_domain')->default(''); $t->timestamps();
  79. });
  80. // The profit migration now inspects MySQL index metadata before creating
  81. // indexes. Keep the business schema real while providing empty metadata.
  82. DB::connection()->getPdo()->sqliteCreateFunction('DATABASE', static fn () => 'isolated');
  83. DB::statement("ATTACH DATABASE ':memory:' AS information_schema");
  84. DB::statement('CREATE TABLE information_schema.statistics (table_schema TEXT,table_name TEXT,index_name TEXT,column_name TEXT,non_unique INTEGER,seq_in_index INTEGER)');
  85. foreach (['2026_08_24_140000_create_agent_system_tables.php',
  86. '2026_08_28_120000_create_agent_profit_profiles_and_rebate_records.php'] as $file) {
  87. (require dirname(__DIR__, 2) . '/database/migrations/' . $file)->up();
  88. }
  89. // Flow migration repairs MySQL information_schema indexes; use equivalent SQLite test tables.
  90. Schema::create('agent_flow_commission_profiles', function (Blueprint $t) {
  91. $t->id(); $t->string('name'); $t->integer('is_default')->default(0); $t->timestamps();
  92. foreach (AgentFlowCommissionProfile::RATE_FIELDS as $field) $t->decimal($field, 8, 4)->default(0);
  93. });
  94. Schema::create('agent_flow_commission_assignments', function (Blueprint $t) {
  95. $t->id(); $t->unsignedBigInteger('agent_id'); $t->unsignedBigInteger('flow_commission_profile_id');
  96. $t->string('profile_name'); $t->date('effective_from'); $t->unsignedBigInteger('created_by')->nullable();
  97. foreach (AgentFlowCommissionProfile::RATE_FIELDS as $field) $t->decimal($field, 8, 4)->default(0);
  98. $t->timestamps(); $t->unique(['agent_id', 'effective_from']);
  99. });
  100. Schema::table('agents', fn (Blueprint $t) => $t->unsignedBigInteger('flow_commission_profile_id')->nullable());
  101. DB::table('agent_flow_commission_profiles')->insert(['name' => 'Default', 'is_default' => 1]);
  102. foreach (['agent_flow_commission_profiles', 'agent_profit_commission_profiles'] as $table) {
  103. Schema::table($table, fn (Blueprint $t) => $t->unsignedBigInteger('owner_agent_id')->nullable());
  104. }
  105. foreach (['recharges', 'withdraws', 'payment_orders'] as $table) {
  106. Schema::create($table, function (Blueprint $t) {
  107. $t->id(); $t->string('member_id'); $t->integer('status'); $t->string('type')->nullable();
  108. $t->decimal('amount', 18, 4); $t->timestamps();
  109. });
  110. }
  111. Schema::create('wallets', function (Blueprint $t) {
  112. $t->id(); $t->string('member_id'); $t->decimal('available_balance', 18, 4)->default(0);
  113. $t->decimal('frozen_balance', 18, 4)->default(0);
  114. });
  115. Schema::create('balance_logs', function (Blueprint $t) {
  116. $t->id(); $t->string('member_id'); $t->string('change_type');
  117. $t->integer('related_id')->nullable(); $t->decimal('amount', 18, 4); $t->timestamps();
  118. });
  119. $this->agents = new AgentService(new AgentFlowCommissionAssignmentService(), new AgentProfitCommissionAssignmentService());
  120. }
  121. protected function tearDown(): void
  122. {
  123. DB::disconnect();
  124. Facade::clearResolvedInstances();
  125. Carbon::setTestNow();
  126. parent::tearDown();
  127. }
  128. private function agent(?Agent $parent = null): Agent
  129. {
  130. static $number = 0;
  131. return $this->agents->create([
  132. 'username' => 'agent' . ++$number, 'password' => 'old-password',
  133. 'withdraw_password' => 'withdraw-old',
  134. 'flow_commission_profile_id' => AgentFlowCommissionProfile::officialDefaultId(),
  135. ], null, $parent);
  136. }
  137. private function token(Agent $agent): string
  138. {
  139. $plain = 'test-token-' . $agent->id;
  140. AgentToken::query()->create(['agent_id' => $agent->id, 'token_hash' => hash('sha256', $plain),
  141. 'expires_at' => now()->addDay(), 'last_used_at' => now(), 'ip' => '', 'device' => 'pc']);
  142. return $plain;
  143. }
  144. public function test_admin_password_reset_revokes_old_login(): void
  145. {
  146. $agent = $this->agent(); $token = $this->token($agent);
  147. $this->agents->update($agent, ['password' => 'new-password']);
  148. $this->assertSame(0, AgentToken::query()->where('agent_id', $agent->id)->count());
  149. $request = Request::create('/agent/profile');
  150. $request->headers->set('Authorization', 'Bearer ' . $token);
  151. $response = (new AgentAuthMiddleware())->handle($request, fn () => new JsonResponse(['code' => 0]));
  152. $this->assertSame(101011, $response->getData(true)['code']);
  153. }
  154. public function test_disable_then_enable_does_not_restore_old_login(): void
  155. {
  156. $agent = $this->agent(); $this->token($agent);
  157. $this->agents->update($agent, ['status' => 0]);
  158. $this->agents->update($agent, ['status' => 1]);
  159. $this->assertSame(0, AgentToken::query()->count());
  160. }
  161. public function test_non_sensitive_edit_preserves_login(): void
  162. {
  163. $agent = $this->agent(); $this->token($agent);
  164. $this->agents->update($agent, ['real_name' => 'new name']);
  165. $this->assertSame(1, AgentToken::query()->count());
  166. }
  167. public function test_finance_counts_include_only_successful_in_range_provider_orders(): void
  168. {
  169. $agent = $this->agent();
  170. DB::table('users')->insert(['member_id' => 'm1', 'username' => 'member', 'agent_id' => $agent->id,
  171. 'created_at' => '2026-01-01 00:00:00']);
  172. $base = ['member_id' => 'm1', 'amount' => 10, 'created_at' => now()];
  173. DB::table('recharges')->insert($base + ['status' => 1]);
  174. DB::table('withdraws')->insert($base + ['status' => 1]);
  175. foreach ([PaymentOrderService::TYPE_PAY, PaymentOrderService::TYPE_PAYOUT, PaymentOrderService::TYPE_SELF_PAYOUT] as $type) {
  176. DB::table('payment_orders')->insert($base + ['type' => $type, 'status' => PaymentOrderService::STATUS_SUCCESS]);
  177. }
  178. DB::table('payment_orders')->insert($base + ['type' => PaymentOrderService::TYPE_PAY, 'status' => -99]);
  179. DB::table('payment_orders')->insert(array_merge($base, ['type' => PaymentOrderService::TYPE_PAY,
  180. 'status' => PaymentOrderService::STATUS_SUCCESS, 'created_at' => '2026-08-01 00:00:00']));
  181. DB::table('payment_orders')->insert(array_merge($base, ['type' => PaymentOrderService::TYPE_PAY,
  182. 'status' => PaymentOrderService::STATUS_SUCCESS, 'member_id' => 'unrelated']));
  183. $result = (new AgentMemberService(new AgentReportService()))->finance($agent, []);
  184. $member = $result['list'][0];
  185. $this->assertSame(2, $member->recharge_count);
  186. $this->assertSame(3, $member->withdraw_count);
  187. $this->assertSame('20.0000', $member->recharge_amount);
  188. $this->assertSame('30.0000', $member->withdraw_amount);
  189. }
  190. public function test_login_logs_filter_login_time_not_record_creation_time(): void
  191. {
  192. $agent = $this->agent();
  193. AgentLoginLog::query()->create(['agent_id' => $agent->id, 'username' => $agent->username,
  194. 'login_at' => '2026-09-06 23:59:59', 'created_at' => '2026-09-07 00:00:01']);
  195. $controller = new AdminAgent();
  196. $request = Request::create('/admin/agent/login-logs', 'GET', ['start_date' => '2026-09-06', 'end_date' => '2026-09-06']);
  197. $this->assertSame(1, $controller->loginLogs($request)->getData(true)['data']['total']);
  198. }
  199. private function fundedAccount(Agent $agent): AgentWithdrawAccount
  200. {
  201. Agent::query()->whereKey($agent->id)->update(['balance' => 100]);
  202. return AgentWithdrawAccount::query()->create(['agent_id' => $agent->id, 'type' => 'bank', 'account' => 'test-account', 'status' => 1]);
  203. }
  204. public function test_withdrawal_uses_current_password_not_stale_request_model(): void
  205. {
  206. $agent = $this->agent(); $account = $this->fundedAccount($agent);
  207. $this->agents->update($agent, ['withdraw_password' => 'withdraw-new']);
  208. $this->expectException(\InvalidArgumentException::class);
  209. $this->expectExceptionMessage('提现密码错误');
  210. (new AgentWithdrawalService())->request($agent, $account->id, '10', 'withdraw-old', 'stale');
  211. }
  212. public function test_disabled_agent_cannot_submit_with_stale_request_model(): void
  213. {
  214. $agent = $this->agent(); $account = $this->fundedAccount($agent);
  215. $this->agents->update($agent, ['status' => 0]);
  216. $this->expectException(\RuntimeException::class);
  217. $this->expectExceptionMessage('代理账号已禁用');
  218. (new AgentWithdrawalService())->request($agent, $account->id, '10', 'withdraw-old', 'disabled');
  219. }
  220. public function test_withdrawal_retry_and_rejection_move_funds_once(): void
  221. {
  222. $agent = $this->agent(); $account = $this->fundedAccount($agent);
  223. $service = new AgentWithdrawalService();
  224. $one = $service->request($agent, $account->id, '10', 'withdraw-old', 'retry');
  225. $two = $service->request($agent, $account->id, '10', 'withdraw-old', 'retry');
  226. $this->assertSame($one->id, $two->id);
  227. $this->assertSame('90.0000', $agent->fresh()->balance);
  228. $this->assertSame('10.0000', $agent->fresh()->frozen_balance);
  229. $service->audit($one->id, 'reject', 1);
  230. $this->assertSame('100.0000', $agent->fresh()->balance);
  231. $this->assertSame('0.0000', $agent->fresh()->frozen_balance);
  232. $this->assertSame(1, AgentWithdrawal::query()->count());
  233. $this->assertSame(2, AgentBalanceLog::query()->count());
  234. }
  235. public function test_transfer_rechecks_current_parent_inside_service(): void
  236. {
  237. $root = $this->agent(); $child = $this->agent($root); $other = $this->agent();
  238. Agent::query()->whereKey($root->id)->update(['balance' => 100]);
  239. Agent::query()->whereKey($child->id)->update(['parent_id' => $other->id, 'path' => $other->path . $child->id . '/']);
  240. $this->expectException(\RuntimeException::class);
  241. $this->expectExceptionMessage('只能操作当前直属下级的额度');
  242. (new AgentWalletService())->transfer($root->id, $child->id, '10', 'test', 'moved', $root->id);
  243. }
  244. public function test_transfer_retry_and_reclaim_from_disabled_child(): void
  245. {
  246. $root = $this->agent(); $child = $this->agent($root);
  247. Agent::query()->whereKey($root->id)->update(['balance' => 100]);
  248. $wallets = new AgentWalletService();
  249. $wallets->transfer($root->id, $child->id, '10', 'test', 'once', $root->id);
  250. $wallets->transfer($root->id, $child->id, '10', 'test', 'once', $root->id);
  251. $this->assertSame('90.0000', $root->fresh()->balance);
  252. $this->agents->update($child, ['status' => 0]);
  253. $wallets->transfer($child->id, $root->id, '10', 'test', 'reclaim', $root->id);
  254. $this->assertSame('100.0000', $root->fresh()->balance);
  255. $this->assertSame('0.0000', $child->fresh()->balance);
  256. }
  257. public function test_historical_settlement_ignores_new_agents_and_new_children(): void
  258. {
  259. Carbon::setTestNow(Carbon::parse('2026-09-06 12:00:00'));
  260. $root = $this->agent();
  261. Carbon::setTestNow(Carbon::parse('2026-09-07 12:00:00'));
  262. $this->agent($root); $this->agent();
  263. $service = new AgentCommissionService(new AgentReportService(), new AgentWalletService(),
  264. new AgentFlowCommissionAssignmentService(), new AgentProfitCommissionAssignmentService());
  265. $this->assertSame(['created' => 0, 'credited' => 0], $service->settle('2026-09-06', false));
  266. }
  267. public function test_daily_game_stats_work_with_bot_table_prefix(): void
  268. {
  269. $agent = $this->agent();
  270. DB::table('users')->insert(['member_id' => 'm1', 'username' => 'member', 'agent_id' => $agent->id]);
  271. Schema::create('bets', function (Blueprint $t) {
  272. $t->id(); $t->string('member_id'); $t->integer('status');
  273. $t->decimal('amount', 18, 4); $t->decimal('profit', 18, 4); $t->timestamps();
  274. });
  275. DB::table('bets')->insert(['member_id' => 'm1', 'status' => 2, 'amount' => 10, 'profit' => 0, 'created_at' => now()]);
  276. foreach (['sport_game_order', 'jisu_game_order'] as $table) {
  277. Schema::create($table, function (Blueprint $t) {
  278. $t->id(); $t->string('member_id'); $t->integer('status');
  279. $t->decimal('amount', 18, 4); $t->decimal('profit_and_loss', 18, 4); $t->timestamps();
  280. });
  281. DB::table($table)->insert(['member_id' => 'm1', 'status' => 1, 'amount' => 10, 'profit_and_loss' => 0, 'created_at' => now()]);
  282. }
  283. Schema::create('lhc_order', function (Blueprint $t) {
  284. $t->id(); $t->string('member_id'); $t->integer('lottery_status');
  285. $t->decimal('amount', 18, 4); $t->decimal('total_amount', 18, 4)->nullable();
  286. $t->decimal('win_amount', 18, 4); $t->integer('created_at');
  287. });
  288. DB::table('lhc_order')->insert(['member_id' => 'm1', 'lottery_status' => 1, 'amount' => 10,
  289. 'total_amount' => null, 'win_amount' => 0, 'created_at' => now()->timestamp]);
  290. (require dirname(__DIR__, 2) . '/database/migrations/2026_08_21_120000_create_third_game_orders_table.php')->up();
  291. DB::table('third_game_orders')->insert(['user_id' => DB::table('users')->value('id'), 'member_id' => 'm1',
  292. 'order_key' => 'test', 'game_order_id' => 'test', 'player_id' => 'test', 'currency' => 'CNY',
  293. 'platform' => 'TEST', 'game_type' => 1, 'status' => 1,
  294. 'bet_amount' => 10, 'valid_amount' => 10, 'settled_amount' => -10, 'last_update_time' => now()]);
  295. $reports = new AgentReportService();
  296. $this->assertSame(5, $reports->refreshDailyGameStats('2026-09-07'));
  297. foreach (AgentDailyGameStat::query()->get() as $stat) {
  298. $this->assertSame('10.0000', $stat->valid_bet_amount);
  299. $this->assertSame('-10.0000', $stat->win_loss);
  300. }
  301. $summary = $reports->summaryForTree($agent, '2026-09-07', '2026-09-07');
  302. $this->assertSame(5, $summary['bet_count']);
  303. $this->assertSame('50.0000', $summary['valid_bet_amount']);
  304. $this->assertSame('-50.0000', $summary['win_loss']);
  305. }
  306. public function test_member_finance_does_not_include_other_agent_tree(): void
  307. {
  308. $root = $this->agent(); $child = $this->agent($root); $other = $this->agent();
  309. foreach ([$root, $child, $other] as $agent) {
  310. DB::table('users')->insert(['member_id' => 'm' . $agent->id, 'username' => 'member' . $agent->id,
  311. 'agent_id' => $agent->id, 'created_at' => now()]);
  312. }
  313. $service = new AgentMemberService(new AgentReportService());
  314. $this->assertSame(2, $service->finance($root, [])['total']);
  315. $this->assertSame(0, $service->finance($root, ['agent_id' => $other->id])['total']);
  316. $this->assertSame(1, $service->finance($child, [])['total']);
  317. }
  318. public function test_sub_agent_cannot_take_profile_higher_than_parent(): void
  319. {
  320. $root = $this->agent();
  321. $profile = AgentFlowCommissionProfile::query()->create(['name' => 'High rate', 'live_rate' => 90]);
  322. $this->expectException(\RuntimeException::class);
  323. $this->expectExceptionMessage('下级代理流水佣金方案不能高于上级方案');
  324. $this->agents->create(['username' => 'child-high', 'password' => 'password',
  325. 'flow_commission_profile_id' => $profile->id], null, $root);
  326. }
  327. public function test_business_errors_keep_existing_response_contract(): void
  328. {
  329. $controller = new class extends AgentApiController {
  330. public function trigger() {
  331. return $this->execute(fn () => throw new \RuntimeException('代理余额不足'));
  332. }
  333. };
  334. $data = $controller->trigger()->getData(true);
  335. $this->assertSame(['code', 'timestamp', 'msg', 'data'], array_keys($data));
  336. $this->assertSame(-3, $data['code']);
  337. $this->assertSame('代理余额不足', $data['msg']);
  338. }
  339. public function test_settlement_preview_credit_and_retry_preserve_paid_snapshot(): void
  340. {
  341. Carbon::setTestNow(Carbon::parse('2026-09-06 12:00:00'));
  342. DB::table('agent_flow_commission_profiles')->update(['live_rate' => 1]);
  343. DB::table('agent_profit_commission_profiles')->update(['live_rate' => 2]);
  344. $agent = $this->agent();
  345. DB::table('agent_daily_game_stats')->insert(['stat_date' => '2026-09-06', 'agent_id' => $agent->id,
  346. 'platform' => 'test', 'game_type' => 1, 'bet_count' => 1, 'bet_amount' => 100,
  347. 'valid_bet_amount' => 100, 'win_loss' => -50]);
  348. Carbon::setTestNow(Carbon::parse('2026-09-07 12:00:00'));
  349. $service = new AgentCommissionService(new AgentReportService(), new AgentWalletService(),
  350. new AgentFlowCommissionAssignmentService(), new AgentProfitCommissionAssignmentService());
  351. $this->assertSame(['created' => 1, 'credited' => 0], $service->settle('2026-09-06', false));
  352. $this->assertSame('0.0000', $agent->fresh()->balance);
  353. $this->assertSame(['created' => 0, 'credited' => 2], $service->settle('2026-09-06'));
  354. $this->assertSame('2.0000', $agent->fresh()->balance);
  355. DB::table('agent_daily_game_stats')->update(['valid_bet_amount' => 200]);
  356. $this->assertSame(['created' => 0, 'credited' => 0], $service->settle('2026-09-06'));
  357. $this->assertSame('2.0000', $agent->fresh()->balance);
  358. $this->assertSame('100.0000', AgentRebateRecord::query()->firstOrFail()->valid_bet_amount);
  359. $this->assertSame(2, AgentBalanceLog::query()->count());
  360. }
  361. public function test_settlement_command_rejects_invalid_and_unfinished_dates(): void
  362. {
  363. $handler = $this->createMock(ExceptionHandler::class);
  364. $handler->expects($this->exactly(3))->method('report');
  365. $this->app->instance(ExceptionHandler::class, $handler);
  366. $reports = $this->createMock(AgentReportService::class);
  367. $reports->expects($this->never())->method('refreshDaily');
  368. $this->app->instance(AgentReportService::class, $reports);
  369. $commissions = $this->createMock(AgentCommissionService::class);
  370. $commissions->expects($this->never())->method('settle');
  371. $this->app->instance(AgentCommissionService::class, $commissions);
  372. foreach (['2026-02-30', '2026-09-07', '2026-09-08'] as $date) {
  373. $command = new \App\Console\Commands\AgentDailySettle();
  374. $command->setLaravel($this->app);
  375. $tester = new \Symfony\Component\Console\Tester\CommandTester($command);
  376. $this->assertSame(1, $tester->execute(['date' => $date]));
  377. }
  378. }
  379. public function test_sql_errors_are_logged_but_not_returned_to_frontend(): void
  380. {
  381. $handler = $this->createMock(ExceptionHandler::class);
  382. $handler->expects($this->once())->method('report');
  383. $this->app->instance(ExceptionHandler::class, $handler);
  384. $controller = new class extends AgentApiController {
  385. public function trigger() {
  386. return $this->execute(fn () => throw new QueryException('select sensitive_table', ['secret'], new \PDOException('internal database error')));
  387. }
  388. };
  389. $data = $controller->trigger()->getData(true);
  390. $this->assertSame(-3, $data['code']);
  391. $this->assertSame('系统处理失败,请稍后重试', $data['msg']);
  392. $this->assertSame([], $data['data']);
  393. }
  394. }