up(); $this->domains = new AgentDomainService(); $this->pool = new AgentDomainPoolService($this->domains); } private function call(string $method, array $params, string $http = 'POST'): array { $request = Request::create('/admin/agent/domain-pool', $http, $params); $request->user = (object) ['id' => 9]; $this->app->instance('request', $request); return (new PoolController())->{$method}($request, $this->pool)->getData(true); } public function test_pool_lifecycle_with_bindings(): void { $this->boot(); $created = $this->call('create', ['domains' => "a.example.com\nhttps://A.example.com/#/reg\nftp://bad.example.com\nhttps://b.example.com/#/user/register", 'remark' => '第一批']); $this->assertSame(0, $created['code'], (string) $created['msg']); $this->assertSame(2, $created['data']['created']); $this->assertSame(['本次输入重复', '域名格式错误'], array_column($created['data']['skipped'], 'reason')); $this->assertSame('域名已存在', $this->pool->create(['a.example.com'], '', 1)['skipped'][0]['reason']); $agent = $this->agent(); $this->domains->save($agent, ['https://a.example.com/#/user/register', 'https://c.example.com']); $list = $this->call('index', [], 'GET')['data']; $this->assertSame(['total' => 3, 'in_use' => 2, 'available' => 1, 'disabled' => 0], $list['summary'], 'c.example.com auto-registered on bind'); $rows = array_column($list['list'], null, 'host'); $this->assertTrue($rows['a.example.com']['in_use']); $this->assertSame('a.example.com', $rows['a.example.com']['domain'], 'pool keeps what was added'); $this->assertSame([$agent->username, 'https://a.example.com/#/user/register'], [$rows['a.example.com']['bindings'][0]['agent_username'], $rows['a.example.com']['bindings'][0]['bound_domain']]); $this->assertSame('代理列表绑定时自动登记', $rows['c.example.com']['remark']); $this->assertArrayNotHasKey('type', $rows['a.example.com']); $this->assertSame(['可用', '使用中'], [$rows['b.example.com']['usage_text'], $rows['a.example.com']['usage_text']]); $this->assertSame(['b.example.com'], array_column($this->call('index', ['usage' => 'available'], 'GET')['data']['list'], 'host')); $this->assertSame(2, $this->call('index', ['agent_username' => $agent->username], 'GET')['data']['total']); $this->assertSame(['https://b.example.com/#/user/register'], array_column($this->pool->options(), 'domain'), 'options exclude domains in use'); $b = $rows['b.example.com']['id']; $this->call('update', ['id' => $b, 'status' => 0]); try { $this->domains->save($this->agent(), ['b.example.com']); $this->fail('disabled'); } catch (\RuntimeException $e) { $this->assertSame('该域名已停用,不能绑定', $e->getMessage()); } $a = $rows['a.example.com']['id']; $this->assertSame('域名使用中,不能删除,请先到代理列表解绑', $this->call('delete', ['id' => $a])['msg']); $this->assertSame('域名使用中,不能修改为其他域名,请先到代理列表解绑', $this->call('update', ['id' => $a, 'domain' => 'z.example.com'])['msg']); $this->domains->save($agent, ['https://c.example.com']); $this->assertSame(0, $this->call('delete', ['id' => $a])['code'], 'free after unbinding'); $this->assertSame(2, $this->call('index', [], 'GET')['data']['total']); } }