AgentMultipleDomainsTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace Tests\Integration;
  3. use App\Http\Controllers\admin\Agent as AdminAgent;
  4. use App\Services\Agent\AgentDomainService;
  5. use Illuminate\Http\Request;
  6. use PHPUnit\Framework\TestCase;
  7. use Tests\Support\AgentTestEnvironment;
  8. /** 代理绑定多个域名,不区分 PC / H5;一个主机名只能属于一个代理。 */
  9. class AgentMultipleDomainsTest extends TestCase
  10. {
  11. use AgentTestEnvironment;
  12. private function bind(int $agentId, array $params): array
  13. {
  14. $request = Request::create('/admin/agent/domains', 'POST', ['id' => $agentId] + $params);
  15. $request->user = (object) ['id' => 1];
  16. $this->app->instance('request', $request);
  17. return (new AdminAgent())->domains($request, new AgentDomainService())->getData(true);
  18. }
  19. public function test_agent_binds_a_single_list_of_domains(): void
  20. {
  21. $agent = $this->agent();
  22. $other = $this->agent();
  23. $data = $this->bind($agent->id, ['domains' => ['https://a.com/#/reg', 'b.com', 'https://A.com/other', 'https://m.a.com']]);
  24. $this->assertSame(0, $data['code'], (string) $data['msg']);
  25. $this->assertSame(['https://a.com/#/reg', 'b.com', 'https://m.a.com'], $data['data']['domains'], 'same host kept once');
  26. $this->assertSame(['domains'], array_keys($data['data']), 'no pc / h5 fields');
  27. $this->assertSame($agent->id, (new AgentDomainService())->resolve(null, 'https://m.a.com/x')->id);
  28. $taken = $this->bind($other->id, ['domains' => ['c.com', 'b.com']]);
  29. $this->assertSame('该域名已绑定其他代理:b.com', $taken['msg']);
  30. $this->assertSame(['b.com'], $this->bind($agent->id, ['domains' => 'b.com'])['data']['domains'], 'string is one domain; list replaced');
  31. $this->assertSame(['c.com', 'a.com'], $this->bind($other->id, ['domains' => ['c.com', 'a.com']])['data']['domains'], 'released a.com can move');
  32. $this->assertNotSame(0, $this->bind($other->id, ['pc' => 'x.com'])['code'], 'old pc / h5 params are not accepted');
  33. $this->assertSame(['c.com', 'a.com'], $this->bind($other->id, ['domains' => ['c.com', 'a.com']])['data']['domains']);
  34. }
  35. public function test_migration_merges_old_pc_and_h5_rows(): void
  36. {
  37. $migration = require dirname(__DIR__, 2) . '/database/migrations/2026_11_09_140000_merge_agent_domain_types.php';
  38. $migration->down();
  39. $agent = $this->agent();
  40. \Illuminate\Support\Facades\DB::table('agent_domains')->insert([
  41. ['agent_id' => $agent->id, 'type' => 'pc', 'domain' => 'https://a.com', 'host' => 'a.com', 'status' => 1],
  42. ['agent_id' => $agent->id, 'type' => 'h5', 'domain' => 'https://a.com/m', 'host' => 'a.com', 'status' => 1],
  43. ['agent_id' => $agent->id, 'type' => 'h5', 'domain' => 'https://m.b.com', 'host' => 'm.b.com', 'status' => 1],
  44. ]);
  45. $migration->up();
  46. $this->assertSame(['https://a.com', 'https://m.b.com'], (new AgentDomainService())->format($agent)['domains']);
  47. $this->assertSame([''], \Illuminate\Support\Facades\DB::table('agent_domains')->distinct()->pluck('type')->all());
  48. }
  49. }