AgentApiRegressionTest.php 23 KB

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