AgentDomainServiceTest.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. namespace Tests\Unit;
  3. use App\Services\Agent\AgentDomainService;
  4. use PHPUnit\Framework\TestCase;
  5. use ReflectionMethod;
  6. /** 推广域名绑定什么就保存、返回什么;主机名用于唯一性和注册识别。 */
  7. class AgentDomainServiceTest extends TestCase
  8. {
  9. private function call(string $method, string $value)
  10. {
  11. $reflection = new ReflectionMethod(AgentDomainService::class, $method);
  12. $reflection->setAccessible(true);
  13. return $reflection->invoke(new AgentDomainService(), $value);
  14. }
  15. public function test_bound_value_is_kept_exactly_and_host_is_extracted(): void
  16. {
  17. foreach ([
  18. 'https://pc.example.com' => 'pc.example.com',
  19. ' https://pc.example.com/#/user/register?ref=A1 ' => 'pc.example.com',
  20. 'h5.Example.com/reg' => 'h5.example.com',
  21. 'http://pc.example.com:8080/x' => 'pc.example.com',
  22. ] as $input => $host) {
  23. $this->assertSame(trim($input), $this->call('normalize', $input), 'saved exactly as entered (only trimmed)');
  24. $this->assertSame($host, $this->call('host', trim($input)));
  25. }
  26. $this->assertSame('', $this->call('normalize', ' '));
  27. foreach (['ftp://pc.example.com', 'https://', 'pc example.com'] as $bad) {
  28. try { $this->call('normalize', $bad); $this->fail($bad); }
  29. catch (\InvalidArgumentException $e) { $this->assertSame('域名格式错误', $e->getMessage()); }
  30. }
  31. }
  32. }