|
1
|
|
|
<?php |
|
2
|
|
|
namespace LosDomainTest; |
|
3
|
|
|
|
|
4
|
|
|
use LosDomain\Domain; |
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
|
|
7
|
|
|
class DomainTest extends TestCase |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @param $httpHost |
|
11
|
|
|
* @param $serverName |
|
12
|
|
|
* @param $serverPort |
|
13
|
|
|
* |
|
14
|
|
|
* @dataProvider domainProvider |
|
15
|
|
|
*/ |
|
16
|
|
|
public function testDomain($httpHost, $serverName, $serverPort, $expected) |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
unset($_SERVER['HTTP_HOST']); |
|
19
|
|
|
unset($_SERVER['SERVER_NAME']); |
|
20
|
|
|
unset($_SERVER['SERVER_PORT']); |
|
21
|
|
|
if (! empty($httpHost)) { |
|
22
|
|
|
$_SERVER['HTTP_HOST'] = $httpHost; |
|
23
|
|
|
} |
|
24
|
|
|
if (! empty($serverName)) { |
|
25
|
|
|
$_SERVER['SERVER_NAME'] = $serverName; |
|
26
|
|
|
} |
|
27
|
|
|
if (! empty($serverPort)) { |
|
28
|
|
|
$_SERVER['SERVER_PORT'] = $serverPort; |
|
29
|
|
|
} |
|
30
|
|
|
$domain = new Domain(); |
|
31
|
|
|
$this->assertSame($expected, $domain->toString()); |
|
32
|
|
|
$this->assertSame($expected, $domain->domain()); |
|
33
|
|
|
$this->assertSame($expected, (string)$domain); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function domainProvider() |
|
37
|
|
|
{ |
|
38
|
|
|
return [ |
|
39
|
|
|
'simple domain' => [ 'abc.com', 'abc.com', '', 'abc.com' ], |
|
40
|
|
|
'domain with port' => [ 'abc.com:8080', 'abc.com', 8080, 'abc.com' ], |
|
41
|
|
|
'only server_name' => [ '', 'abc.com', 80, 'abc.com' ], |
|
42
|
|
|
'no domain' => [ '', '', '', Domain::DEFAULT_DOMAIN ], |
|
43
|
|
|
]; |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: