TestLoginAccountCreationCommand.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace App\Module\Fund\Commands;
  3. use App\Module\AppGame\Events\LoginSuccessEvent;
  4. use App\Module\Fund\Services\AccountService;
  5. use App\Module\User\Models\User;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\Event;
  8. use Illuminate\Support\Facades\Log;
  9. /**
  10. * 测试登录时资金账户创建功能
  11. */
  12. class TestLoginAccountCreationCommand extends Command
  13. {
  14. /**
  15. * 命令签名
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'fund:test-login-account-creation {user_id : 用户ID}';
  20. /**
  21. * 命令描述
  22. *
  23. * @var string
  24. */
  25. protected $description = '测试用户登录时资金账户自动创建功能';
  26. /**
  27. * 执行命令
  28. *
  29. * @return int
  30. */
  31. public function handle(): int
  32. {
  33. $userId = $this->argument('user_id');
  34. $this->info("开始测试用户 {$userId} 的登录资金账户创建功能...");
  35. try {
  36. // 检查用户是否存在
  37. $user = User::find($userId);
  38. if (!$user) {
  39. $this->error("用户 {$userId} 不存在");
  40. return 1;
  41. }
  42. $this->info("用户信息: ID={$user->id}, 用户名={$user->username}");
  43. // 检查用户当前的资金账户
  44. $this->info("检查用户当前的资金账户...");
  45. $accounts = \App\Module\Fund\Models\FundModel::userAccount($userId);
  46. $this->info("当前资金账户数量: " . $accounts->count());
  47. foreach ($accounts as $account) {
  48. $this->line(" - 账户ID: {$account->id}, 资金类型: {$account->fund_id->value}, 余额: {$account->balance}");
  49. }
  50. // 模拟触发登录成功事件
  51. $this->info("模拟触发登录成功事件...");
  52. Event::dispatch(new LoginSuccessEvent($user, 'test-session-' . time()));
  53. // 再次检查用户的资金账户
  54. $this->info("重新检查用户的资金账户...");
  55. $accountsAfter = \App\Module\Fund\Models\FundModel::userAccount($userId);
  56. $this->info("登录后资金账户数量: " . $accountsAfter->count());
  57. foreach ($accountsAfter as $account) {
  58. $this->line(" - 账户ID: {$account->id}, 资金类型: {$account->fund_id->value}, 余额: {$account->balance}");
  59. }
  60. // 直接测试AccountService::check4user方法
  61. $this->info("直接测试 AccountService::check4user 方法...");
  62. AccountService::check4user($userId);
  63. // 最终检查
  64. $this->info("最终检查用户的资金账户...");
  65. $accountsFinal = \App\Module\Fund\Models\FundModel::userAccount($userId);
  66. $this->info("最终资金账户数量: " . $accountsFinal->count());
  67. foreach ($accountsFinal as $account) {
  68. $this->line(" - 账户ID: {$account->id}, 资金类型: {$account->fund_id->value}, 余额: {$account->balance}");
  69. }
  70. $this->info("测试完成!");
  71. return 0;
  72. } catch (\Exception $e) {
  73. $this->error("测试失败: " . $e->getMessage());
  74. $this->error("错误详情: " . $e->getTraceAsString());
  75. return 1;
  76. }
  77. }
  78. }