AgentDomainPoolTest.php 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace Tests\Integration;
  3. use App\Http\Controllers\admin\AgentDomainPool as PoolController;
  4. use App\Services\Agent\AgentDomainPoolService;
  5. use App\Services\Agent\AgentDomainService;
  6. use Illuminate\Http\Request;
  7. use PHPUnit\Framework\TestCase;
  8. use Tests\Support\AgentTestEnvironment;
  9. /** 总后台代理域名管理:添加可用域名、使用情况、绑定到哪个代理、停用和删除规则。 */
  10. class AgentDomainPoolTest extends TestCase
  11. {
  12. use AgentTestEnvironment;
  13. private AgentDomainPoolService $pool;
  14. private AgentDomainService $domains;
  15. private function boot(): void
  16. {
  17. (require dirname(__DIR__, 2) . '/database/migrations/2026_11_09_120000_create_agent_domain_pool.php')->up();
  18. $this->domains = new AgentDomainService();
  19. $this->pool = new AgentDomainPoolService($this->domains);
  20. }
  21. private function call(string $method, array $params, string $http = 'POST'): array
  22. {
  23. $request = Request::create('/admin/agent/domain-pool', $http, $params);
  24. $request->user = (object) ['id' => 9];
  25. $this->app->instance('request', $request);
  26. return (new PoolController())->{$method}($request, $this->pool)->getData(true);
  27. }
  28. public function test_pool_lifecycle_with_bindings(): void
  29. {
  30. $this->boot();
  31. $created = $this->call('create', ['domains' => "a.example.com\nhttps://A.example.com/#/reg\nftp://bad.example.com\nhttps://b.example.com/#/user/register", 'remark' => '第一批']);
  32. $this->assertSame(0, $created['code'], (string) $created['msg']);
  33. $this->assertSame(2, $created['data']['created']);
  34. $this->assertSame(['本次输入重复', '域名格式错误'], array_column($created['data']['skipped'], 'reason'));
  35. $this->assertSame('域名已存在', $this->pool->create(['a.example.com'], '', 1)['skipped'][0]['reason']);
  36. $agent = $this->agent();
  37. $this->domains->save($agent, ['https://a.example.com/#/user/register', 'https://c.example.com']);
  38. $list = $this->call('index', [], 'GET')['data'];
  39. $this->assertSame(['total' => 3, 'in_use' => 2, 'available' => 1, 'disabled' => 0], $list['summary'], 'c.example.com auto-registered on bind');
  40. $rows = array_column($list['list'], null, 'host');
  41. $this->assertTrue($rows['a.example.com']['in_use']);
  42. $this->assertSame('a.example.com', $rows['a.example.com']['domain'], 'pool keeps what was added');
  43. $this->assertSame([$agent->username, 'https://a.example.com/#/user/register'],
  44. [$rows['a.example.com']['bindings'][0]['agent_username'], $rows['a.example.com']['bindings'][0]['bound_domain']]);
  45. $this->assertSame('代理列表绑定时自动登记', $rows['c.example.com']['remark']);
  46. $this->assertArrayNotHasKey('type', $rows['a.example.com']);
  47. $this->assertSame(['可用', '使用中'], [$rows['b.example.com']['usage_text'], $rows['a.example.com']['usage_text']]);
  48. $this->assertSame(['b.example.com'], array_column($this->call('index', ['usage' => 'available'], 'GET')['data']['list'], 'host'));
  49. $this->assertSame(2, $this->call('index', ['agent_username' => $agent->username], 'GET')['data']['total']);
  50. $this->assertSame(['https://b.example.com/#/user/register'], array_column($this->pool->options(), 'domain'), 'options exclude domains in use');
  51. $b = $rows['b.example.com']['id'];
  52. $this->call('update', ['id' => $b, 'status' => 0]);
  53. try { $this->domains->save($this->agent(), ['b.example.com']); $this->fail('disabled'); }
  54. catch (\RuntimeException $e) { $this->assertSame('该域名已停用,不能绑定', $e->getMessage()); }
  55. $a = $rows['a.example.com']['id'];
  56. $this->assertSame('域名使用中,不能删除,请先到代理列表解绑', $this->call('delete', ['id' => $a])['msg']);
  57. $this->assertSame('域名使用中,不能修改为其他域名,请先到代理列表解绑', $this->call('update', ['id' => $a, 'domain' => 'z.example.com'])['msg']);
  58. $this->domains->save($agent, ['https://c.example.com']);
  59. $this->assertSame(0, $this->call('delete', ['id' => $a])['code'], 'free after unbinding');
  60. $this->assertSame(2, $this->call('index', [], 'GET')['data']['total']);
  61. }
  62. }