1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use PHPUnit\Framework\TestCase; |
4
|
|
|
use tinymeng\tools\UrlTool; |
5
|
|
|
|
6
|
|
|
class UrlToolTest extends TestCase |
7
|
|
|
{ |
8
|
|
|
public function testGetMainDomain_HostExists_ReturnsMainDomain() |
9
|
|
|
{ |
10
|
|
|
$url = "http://sub.example.com"; |
11
|
|
|
$expected = "example.com"; |
12
|
|
|
$this->assertEquals($expected, UrlTool::getMainDomain($url)); |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
public function testGetMainDomain_HostMissing_ReturnsMainDomain() |
16
|
|
|
{ |
17
|
|
|
$url = "sub.example.com"; |
18
|
|
|
$expected = "example.com"; |
19
|
|
|
$this->assertEquals($expected, UrlTool::getMainDomain($url)); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function testGetMainDomain_Localhost_ReturnsEmpty() |
23
|
|
|
{ |
24
|
|
|
$url = "http://localhost"; |
25
|
|
|
$expected = ""; |
26
|
|
|
$this->assertEquals($expected, UrlTool::getMainDomain($url)); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testGetMainDomain_127_0_0_1_ReturnsEmpty() |
30
|
|
|
{ |
31
|
|
|
$url = "http://127.0.0.1"; |
32
|
|
|
$expected = ""; |
33
|
|
|
$this->assertEquals($expected, UrlTool::getMainDomain($url)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testGetMainDomain_DoubleSuffix_ReturnsMainDomain() |
37
|
|
|
{ |
38
|
|
|
$url = "http://sub.example.com.cn"; |
39
|
|
|
$expected = "example.com.cn"; |
40
|
|
|
$this->assertEquals($expected, UrlTool::getMainDomain($url)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testGetMainDomain_TwoParts_ReturnsMainDomain() |
44
|
|
|
{ |
45
|
|
|
$url = "http://example.com"; |
46
|
|
|
$expected = "example.com"; |
47
|
|
|
$this->assertEquals($expected, UrlTool::getMainDomain($url)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testIsURL_ValidURL_ReturnsTrue() |
51
|
|
|
{ |
52
|
|
|
$url = "http://example.com"; |
53
|
|
|
$this->assertTrue((new UrlTool)->isURL($url)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testIsURL_InvalidURL_ReturnsFalse() |
57
|
|
|
{ |
58
|
|
|
$url = "example.com"; |
59
|
|
|
$this->assertFalse((new UrlTool)->isURL($url)); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|