ManualDepositFinanceTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace Tests\Integration;
  3. use App\Models\Agent\Agent;
  4. use App\Services\Agent\AgentMemberService;
  5. use App\Services\Agent\AgentReportService;
  6. use App\Services\PaymentOrderService;
  7. use Illuminate\Config\Repository;
  8. use Illuminate\Database\Capsule\Manager;
  9. use Illuminate\Database\Schema\Blueprint;
  10. use Illuminate\Foundation\Application;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Support\Facades\Facade;
  13. use PHPUnit\Framework\TestCase;
  14. /** Finance queries against SQLite fixtures, without running MySQL-only index migrations. */
  15. class ManualDepositFinanceTest extends TestCase
  16. {
  17. protected function setUp(): void
  18. {
  19. parent::setUp();
  20. Facade::clearResolvedInstances();
  21. $app = new Application(dirname(__DIR__, 2));
  22. $app->instance('config', new Repository(['app' => ['timezone' => 'Asia/Shanghai']]));
  23. Facade::setFacadeApplication($app);
  24. $db = new Manager($app);
  25. $db->addConnection(['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => 'bot_']);
  26. $db->setAsGlobal();
  27. $db->bootEloquent();
  28. $app->instance('db', $db->getDatabaseManager());
  29. $app->bind('db.schema', fn () => $db->schema());
  30. $db->schema()->create('agents', function (Blueprint $t) {
  31. $t->id(); $t->string('username'); $t->string('path');
  32. });
  33. $db->schema()->create('users', function (Blueprint $t) {
  34. $t->id(); $t->string('member_id'); $t->integer('agent_id'); $t->string('username');
  35. $t->string('first_name')->default(''); $t->integer('status')->default(1);
  36. $t->string('register_ip')->default(''); $t->string('register_domain')->default(''); $t->timestamps();
  37. });
  38. $db->schema()->create('wallets', function (Blueprint $t) {
  39. $t->id(); $t->string('member_id'); $t->decimal('available_balance', 24, 10)->default(0);
  40. $t->decimal('frozen_balance', 24, 10)->default(0);
  41. });
  42. foreach (['recharges', 'withdraws', 'payment_orders'] as $table) {
  43. $db->schema()->create($table, function (Blueprint $t) {
  44. $t->id(); $t->string('member_id'); $t->integer('status'); $t->integer('type')->nullable();
  45. $t->decimal('amount', 24, 10); $t->timestamps();
  46. });
  47. }
  48. $db->schema()->create('balance_logs', function (Blueprint $t) {
  49. $t->id(); $t->string('member_id'); $t->string('change_type'); $t->integer('related_id')->nullable();
  50. $t->decimal('amount', 24, 10); $t->timestamps();
  51. });
  52. DB::table('agents')->insert(['id' => 1, 'username' => 'agent', 'path' => '1']);
  53. DB::table('users')->insert(['member_id' => 'm1', 'username' => 'member', 'agent_id' => 1]);
  54. DB::table('wallets')->insert(['member_id' => 'm1']);
  55. }
  56. protected function tearDown(): void
  57. {
  58. DB::disconnect();
  59. Facade::clearResolvedInstances();
  60. parent::tearDown();
  61. }
  62. public function test_successful_frontend_manual_orders_count_as_deposits_and_admin_credits_remain_separate(): void
  63. {
  64. $base = ['member_id' => 'm1', 'amount' => 10, 'created_at' => '2026-10-25 12:00:00'];
  65. DB::table('recharges')->insert($base + ['status' => 1]);
  66. DB::table('withdraws')->insert($base + ['status' => 1]);
  67. foreach ([PaymentOrderService::TYPE_PAY, PaymentOrderService::TYPE_SELF_PAY,
  68. PaymentOrderService::TYPE_PAYOUT, PaymentOrderService::TYPE_SELF_PAYOUT] as $type) {
  69. DB::table('payment_orders')->insert($base + ['type' => $type, 'status' => PaymentOrderService::STATUS_SUCCESS]);
  70. }
  71. foreach ([PaymentOrderService::TYPE_PAY, PaymentOrderService::TYPE_SELF_PAY] as $type) {
  72. foreach ([0, 1, 3, 4, 5, 6] as $status) {
  73. DB::table('payment_orders')->insert($base + ['type' => $type, 'status' => $status]);
  74. }
  75. DB::table('payment_orders')->insert(array_merge($base, ['type' => $type,
  76. 'status' => PaymentOrderService::STATUS_SUCCESS, 'created_at' => '2026-10-24 12:00:00']));
  77. DB::table('payment_orders')->insert(array_merge($base, ['type' => $type,
  78. 'status' => PaymentOrderService::STATUS_SUCCESS, 'member_id' => 'unrelated']));
  79. }
  80. DB::table('balance_logs')->insert($base + ['change_type' => '人工充值']);
  81. $result = (new AgentMemberService(new AgentReportService()))->finance(Agent::findOrFail(1), [
  82. 'start_date' => '2026-10-25', 'end_date' => '2026-10-25',
  83. ]);
  84. $member = $result['list'][0];
  85. $this->assertSame(3, $member->recharge_count);
  86. $this->assertSame('30.0000', $member->recharge_amount);
  87. $this->assertSame('10.0000', $member->manual_credit);
  88. $this->assertSame(3, $member->withdraw_count);
  89. $this->assertSame('30.0000', $member->withdraw_amount);
  90. }
  91. }