RegisterDomainStatTest.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace Tests\Integration;
  3. use App\Services\RegisterStatService;
  4. use Illuminate\Database\Capsule\Manager;
  5. use Illuminate\Database\Schema\Blueprint;
  6. use Illuminate\Foundation\Application;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Facade;
  9. use PHPUnit\Framework\TestCase;
  10. class RegisterDomainStatTest extends TestCase
  11. {
  12. protected function setUp(): void
  13. {
  14. parent::setUp();
  15. Facade::clearResolvedInstances();
  16. $app = new Application(dirname(__DIR__, 2));
  17. Facade::setFacadeApplication($app);
  18. $db = new Manager($app);
  19. $db->addConnection(['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => 'bot_']);
  20. $db->setAsGlobal();
  21. $app->instance('db', $db->getDatabaseManager());
  22. $db->schema()->create('user_login', function (Blueprint $t) {
  23. $t->id();
  24. $t->string('user_id')->nullable();
  25. $t->string('login_domain')->nullable();
  26. $t->integer('status');
  27. $t->dateTime('created_at');
  28. });
  29. foreach ([
  30. ['1', 'a.test', 1, '2026-11-05 00:00:00'],
  31. ['1', 'a.test', 1, '2026-11-05 12:00:00'],
  32. ['2', 'a.test', 1, '2026-11-05 23:59:59'],
  33. ['1', 'b.test', 1, '2026-11-05 12:00:00'],
  34. ['3', 'b.test', 0, '2026-11-05 12:00:00'],
  35. ['', 'failed.test', 1, '2026-11-05 12:00:00'],
  36. [null, 'failed.test', 1, '2026-11-05 12:00:00'],
  37. ['4', '', 1, '2026-11-05 12:00:00'],
  38. ['4', null, 1, '2026-11-05 12:00:00'],
  39. ['5', 'old.test', 1, '2026-11-04 23:59:59'],
  40. ['6', 'next.test', 1, '2026-11-06 00:00:00'],
  41. ] as [$user, $domain, $status, $time]) {
  42. DB::table('user_login')->insert([
  43. 'user_id' => $user, 'login_domain' => $domain,
  44. 'status' => $status, 'created_at' => $time,
  45. ]);
  46. }
  47. }
  48. public function test_counts_distinct_successful_users_by_login_domain_and_time(): void
  49. {
  50. $this->assertSame(['total' => 3, 'data' => [
  51. ['login_url' => 'a.test', 'login_count' => 2],
  52. ['login_url' => 'b.test', 'login_count' => 1],
  53. ['login_url' => '未知', 'login_count' => 1],
  54. ]], RegisterStatService::domain('2026-11-05', '2026-11-05', 1, 50));
  55. }
  56. public function test_pagination_counts_groups_and_supports_optional_dates(): void
  57. {
  58. $this->assertSame(['total' => 3, 'data' => [
  59. ['login_url' => '未知', 'login_count' => 1],
  60. ]], RegisterStatService::domain('2026-11-05', '2026-11-05', 2, 2));
  61. $this->assertSame(5, RegisterStatService::domain(null, null, 1, 50)['total']);
  62. $this->assertSame(4, RegisterStatService::domain('2026-11-05', null, 1, 50)['total']);
  63. $this->assertSame(4, RegisterStatService::domain(null, '2026-11-05', 1, 50)['total']);
  64. $this->assertSame(['total' => 0, 'data' => []], RegisterStatService::domain('2026-11-07', null, 1, 50));
  65. }
  66. }