MemberDailyReportBackfillTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace Tests\Integration;
  3. use App\Console\Commands\MemberDailyReport;
  4. use Illuminate\Support\Facades\DB;
  5. use PHPUnit\Framework\TestCase;
  6. use Symfony\Component\Console\Input\ArrayInput;
  7. use Symfony\Component\Console\Output\BufferedOutput;
  8. use Tests\Support\AgentTestEnvironment;
  9. /** report:member-daily 补算历史:--from 指定起点、--all 从最早有数据的一天开始;默认 --days 行为不变。今天 2026-09-07。 */
  10. class MemberDailyReportBackfillTest extends TestCase
  11. {
  12. use AgentTestEnvironment;
  13. private function artisan(array $input): array
  14. {
  15. $command = new MemberDailyReport();
  16. $command->setLaravel($this->app);
  17. $output = new BufferedOutput();
  18. $code = $command->run(new ArrayInput($input), $output);
  19. return [$code, $output->fetch()];
  20. }
  21. public function test_backfill_from_a_date_or_all_history(): void
  22. {
  23. DB::table('users')->insert(['id' => 1, 'member_id' => 'm1', 'username' => 'm1', 'created_at' => '2026-06-30 09:00:00']);
  24. foreach (['2026-07-01 10:00:00' => 100, '2026-08-15 10:00:00' => 200, '2026-09-07 10:00:00' => 300] as $at => $amount) {
  25. DB::table('recharges')->insert(['member_id' => 'm1', 'status' => 1, 'amount' => $amount, 'created_at' => $at]);
  26. }
  27. [$code, $out] = $this->artisan(['date' => '2026-08-20', '--from' => '2026-08-10']);
  28. $this->assertSame(0, $code, $out);
  29. $this->assertStringContainsString('共 11 天', $out);
  30. $this->assertSame(['2026-08-15'], DB::table('member_daily_reports')->pluck('report_date')->map(fn ($d) => substr($d, 0, 10))->all());
  31. [$code, $out] = $this->artisan(['--all' => true]);
  32. $this->assertSame(0, $code, $out);
  33. $this->assertStringContainsString('重算 2026-06-30 ~ 2026-09-07,共 70 天', $out);
  34. $this->assertSame('600.0000', bcadd((string) DB::table('member_daily_reports')->sum('deposit_amount'), '0', 4));
  35. [$code, $out] = $this->artisan(['date' => '2026-09-07', '--days' => 3]);
  36. $this->assertSame(0, $code);
  37. $this->assertSame(3, substr_count($out, '/3] '), 'scheduled --days run unchanged');
  38. $this->assertStringContainsString('重算 2026-09-05 ~ 2026-09-07', $out);
  39. [$code] = $this->artisan(['date' => '2026-09-01', '--from' => '2026-09-05']);
  40. $this->assertSame(1, $code, 'start after end is rejected');
  41. }
  42. }