| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- <?php
- namespace Tests\Integration;
- use App\Http\Controllers\admin\Agent as AdminAgent;
- use App\Services\Agent\AgentDomainService;
- use Illuminate\Http\Request;
- use PHPUnit\Framework\TestCase;
- use Tests\Support\AgentTestEnvironment;
- /** 代理可以绑定多个 PC / H5 域名;字符串写法兼容;一个主机名只能属于一个代理。 */
- class AgentMultipleDomainsTest extends TestCase
- {
- use AgentTestEnvironment;
- private function bind(int $agentId, array $params): array
- {
- $request = Request::create('/admin/agent/domains', 'POST', ['id' => $agentId] + $params);
- $request->user = (object) ['id' => 1];
- $this->app->instance('request', $request);
- return (new AdminAgent())->domains($request, new AgentDomainService())->getData(true);
- }
- public function test_agent_binds_several_domains_per_type(): void
- {
- $agent = $this->agent();
- $other = $this->agent();
- $data = $this->bind($agent->id, ['pc' => ['https://a.com/#/reg', 'b.com', 'https://A.com/other'], 'h5' => 'https://m.a.com']);
- $this->assertSame(0, $data['code'], (string) $data['msg']);
- $this->assertSame(['https://a.com/#/reg', 'b.com'], $data['data']['pc_domains'], 'same host kept once, first one wins');
- $this->assertSame(['https://m.a.com'], $data['data']['h5_domains']);
- $this->assertSame(['https://a.com/#/reg', 'https://a.com/#/reg', 'https://m.a.com'],
- [$data['data']['pc_domain'], $data['data']['pc_register_url'], $data['data']['h5_domain']], 'single fields keep the first domain');
- $resolver = new AgentDomainService();
- $this->assertSame($agent->id, $resolver->resolve(null, 'https://b.com/register')->id, 'every bound domain links the agent');
- $taken = $this->bind($other->id, ['pc' => ['c.com', 'b.com']]);
- $this->assertSame('该域名已绑定其他代理:b.com', $taken['msg']);
- $this->assertSame([], $this->bind($other->id, ['h5' => []])['data']['pc_domains'], 'failed bind changed nothing');
- $data = $this->bind($agent->id, ['pc' => ['b.com', 'd.com']]);
- $this->assertSame(['b.com', 'd.com'], $data['data']['pc_domains'], 'list replaced; a.com released');
- $this->assertSame(['https://m.a.com'], $data['data']['h5_domains'], 'type not sent stays');
- $this->assertSame(0, $this->bind($other->id, ['pc' => 'a.com'])['code'], 'released domain can be bound by another agent');
- $this->assertSame([], $this->bind($agent->id, ['pc' => ''])['data']['pc_domains'], 'empty string clears');
- }
- }
|