| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- <?php
- namespace Tests\Integration;
- use App\Services\RegisterStatService;
- use Illuminate\Database\Capsule\Manager;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Foundation\Application;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Facade;
- use PHPUnit\Framework\TestCase;
- class RegisterDomainStatTest extends TestCase
- {
- protected function setUp(): void
- {
- parent::setUp();
- Facade::clearResolvedInstances();
- $app = new Application(dirname(__DIR__, 2));
- Facade::setFacadeApplication($app);
- $db = new Manager($app);
- $db->addConnection(['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => 'bot_']);
- $db->setAsGlobal();
- $app->instance('db', $db->getDatabaseManager());
- $db->schema()->create('user_login', function (Blueprint $t) {
- $t->id();
- $t->string('user_id')->nullable();
- $t->string('login_domain')->nullable();
- $t->integer('status');
- $t->dateTime('created_at');
- });
- foreach ([
- ['1', 'a.test', 1, '2026-11-05 00:00:00'],
- ['1', 'a.test', 1, '2026-11-05 12:00:00'],
- ['2', 'a.test', 1, '2026-11-05 23:59:59'],
- ['1', 'b.test', 1, '2026-11-05 12:00:00'],
- ['3', 'b.test', 0, '2026-11-05 12:00:00'],
- ['', 'failed.test', 1, '2026-11-05 12:00:00'],
- [null, 'failed.test', 1, '2026-11-05 12:00:00'],
- ['4', '', 1, '2026-11-05 12:00:00'],
- ['4', null, 1, '2026-11-05 12:00:00'],
- ['5', 'old.test', 1, '2026-11-04 23:59:59'],
- ['6', 'next.test', 1, '2026-11-06 00:00:00'],
- ] as [$user, $domain, $status, $time]) {
- DB::table('user_login')->insert([
- 'user_id' => $user, 'login_domain' => $domain,
- 'status' => $status, 'created_at' => $time,
- ]);
- }
- }
- public function test_counts_distinct_successful_users_by_login_domain_and_time(): void
- {
- $this->assertSame(['total' => 3, 'data' => [
- ['login_url' => 'a.test', 'login_count' => 2],
- ['login_url' => 'b.test', 'login_count' => 1],
- ['login_url' => '未知', 'login_count' => 1],
- ]], RegisterStatService::domain('2026-11-05', '2026-11-05', 1, 50));
- }
- public function test_pagination_counts_groups_and_supports_optional_dates(): void
- {
- $this->assertSame(['total' => 3, 'data' => [
- ['login_url' => '未知', 'login_count' => 1],
- ]], RegisterStatService::domain('2026-11-05', '2026-11-05', 2, 2));
- $this->assertSame(5, RegisterStatService::domain(null, null, 1, 50)['total']);
- $this->assertSame(4, RegisterStatService::domain('2026-11-05', null, 1, 50)['total']);
- $this->assertSame(4, RegisterStatService::domain(null, '2026-11-05', 1, 50)['total']);
- $this->assertSame(['total' => 0, 'data' => []], RegisterStatService::domain('2026-11-07', null, 1, 50));
- }
- }
|