AgentMultipleDomainsTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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_several_domains_per_type(): void
  20. {
  21. $agent = $this->agent();
  22. $other = $this->agent();
  23. $data = $this->bind($agent->id, ['pc' => ['https://a.com/#/reg', 'b.com', 'https://A.com/other'], 'h5' => 'https://m.a.com']);
  24. $this->assertSame(0, $data['code'], (string) $data['msg']);
  25. $this->assertSame(['https://a.com/#/reg', 'b.com'], $data['data']['pc_domains'], 'same host kept once, first one wins');
  26. $this->assertSame(['https://m.a.com'], $data['data']['h5_domains']);
  27. $this->assertSame(['https://a.com/#/reg', 'https://a.com/#/reg', 'https://m.a.com'],
  28. [$data['data']['pc_domain'], $data['data']['pc_register_url'], $data['data']['h5_domain']], 'single fields keep the first domain');
  29. $resolver = new AgentDomainService();
  30. $this->assertSame($agent->id, $resolver->resolve(null, 'https://b.com/register')->id, 'every bound domain links the agent');
  31. $taken = $this->bind($other->id, ['pc' => ['c.com', 'b.com']]);
  32. $this->assertSame('该域名已绑定其他代理:b.com', $taken['msg']);
  33. $this->assertSame([], $this->bind($other->id, ['h5' => []])['data']['pc_domains'], 'failed bind changed nothing');
  34. $data = $this->bind($agent->id, ['pc' => ['b.com', 'd.com']]);
  35. $this->assertSame(['b.com', 'd.com'], $data['data']['pc_domains'], 'list replaced; a.com released');
  36. $this->assertSame(['https://m.a.com'], $data['data']['h5_domains'], 'type not sent stays');
  37. $this->assertSame(0, $this->bind($other->id, ['pc' => 'a.com'])['code'], 'released domain can be bound by another agent');
  38. $this->assertSame([], $this->bind($agent->id, ['pc' => ''])['data']['pc_domains'], 'empty string clears');
  39. }
  40. }