| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- <?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_a_single_list_of_domains(): void
- {
- $agent = $this->agent();
- $other = $this->agent();
- $data = $this->bind($agent->id, ['domains' => ['https://a.com/#/reg', 'b.com', 'https://A.com/other', 'https://m.a.com']]);
- $this->assertSame(0, $data['code'], (string) $data['msg']);
- $this->assertSame(['https://a.com/#/reg', 'b.com', 'https://m.a.com'], $data['data']['domains'], 'same host kept once');
- $this->assertSame(['domains'], array_keys($data['data']), 'no pc / h5 fields');
- $this->assertSame($agent->id, (new AgentDomainService())->resolve(null, 'https://m.a.com/x')->id);
- $taken = $this->bind($other->id, ['domains' => ['c.com', 'b.com']]);
- $this->assertSame('该域名已绑定其他代理:b.com', $taken['msg']);
- $this->assertSame(['b.com'], $this->bind($agent->id, ['domains' => 'b.com'])['data']['domains'], 'string is one domain; list replaced');
- $this->assertSame(['c.com', 'a.com'], $this->bind($other->id, ['domains' => ['c.com', 'a.com']])['data']['domains'], 'released a.com can move');
- $this->assertNotSame(0, $this->bind($other->id, ['pc' => 'x.com'])['code'], 'old pc / h5 params are not accepted');
- $this->assertSame(['c.com', 'a.com'], $this->bind($other->id, ['domains' => ['c.com', 'a.com']])['data']['domains']);
- }
- public function test_migration_merges_old_pc_and_h5_rows(): void
- {
- $migration = require dirname(__DIR__, 2) . '/database/migrations/2026_11_09_140000_merge_agent_domain_types.php';
- $migration->down();
- $agent = $this->agent();
- \Illuminate\Support\Facades\DB::table('agent_domains')->insert([
- ['agent_id' => $agent->id, 'type' => 'pc', 'domain' => 'https://a.com', 'host' => 'a.com', 'status' => 1],
- ['agent_id' => $agent->id, 'type' => 'h5', 'domain' => 'https://a.com/m', 'host' => 'a.com', 'status' => 1],
- ['agent_id' => $agent->id, 'type' => 'h5', 'domain' => 'https://m.b.com', 'host' => 'm.b.com', 'status' => 1],
- ]);
- $migration->up();
- $this->assertSame(['https://a.com', 'https://m.b.com'], (new AgentDomainService())->format($agent)['domains']);
- $this->assertSame([''], \Illuminate\Support\Facades\DB::table('agent_domains')->distinct()->pluck('type')->all());
- }
- }
|