1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class MultiDomainTest extends SapphireTest |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
/** |
6
|
|
|
* Set up some test domain configuration |
7
|
|
|
* |
8
|
|
|
* {@inheritDoc} |
9
|
|
|
*/ |
10
|
|
|
public function setUp() |
11
|
|
|
{ |
12
|
|
|
parent::setUp(); |
13
|
|
|
Config::nest(); |
14
|
|
|
|
15
|
|
|
Config::inst()->remove('MultiDomain', 'domains'); |
16
|
|
|
Config::inst()->update('MultiDomain', 'domains', array( |
17
|
|
|
'primary' => array( |
18
|
|
|
'hostname' => 'foo.bar', |
19
|
|
|
'resolves_to' => 'bar.baz' |
20
|
|
|
), |
21
|
|
|
'secondary' => array( |
22
|
|
|
'hostname' => 'localhost', |
23
|
|
|
'resolves_to' => 'local.dev' |
24
|
|
|
) |
25
|
|
|
)); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Test that a MultiDomainDomain can be returned from the configured domains |
30
|
|
|
*/ |
31
|
|
|
public function testGetDomain() |
32
|
|
|
{ |
33
|
|
|
$this->assertNull(MultiDomain::get_domain('does-not-exist')); |
|
|
|
|
34
|
|
|
$this->assertInstanceOf(MultiDomainDomain::class, MultiDomain::get_domain('primary')); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Test that all domains can be returned, with or without the primary domain |
39
|
|
|
* |
40
|
|
|
* @dataProvider getAllDomainsProvider |
41
|
|
|
* @param bool $withPrimary |
42
|
|
|
*/ |
43
|
|
|
public function testGetAllDomains($withPrimary) |
44
|
|
|
{ |
45
|
|
|
$result = MultiDomain::get_all_domains($withPrimary); |
46
|
|
|
$this->assertInternalType('array', $result); |
|
|
|
|
47
|
|
|
$this->assertNotEmpty($result); |
|
|
|
|
48
|
|
|
|
49
|
|
|
$expectedCount = $withPrimary ? 2 : 1; |
50
|
|
|
$this->assertCount($expectedCount, $result); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return array[] |
55
|
|
|
*/ |
56
|
|
|
public function getAllDomainsProvider() |
57
|
|
|
{ |
58
|
|
|
return array( |
59
|
|
|
array(true), |
60
|
|
|
array(false) |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Test that the primary domain can be returned |
66
|
|
|
*/ |
67
|
|
|
public function testGetPrimaryDomain() |
68
|
|
|
{ |
69
|
|
|
$result = MultiDomain::get_primary_domain(); |
70
|
|
|
$this->assertInstanceOf(MultiDomainDomain::class, $result); |
|
|
|
|
71
|
|
|
$this->assertTrue($result->isPrimary()); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Test that the correct domain can be returned by a provided URL |
76
|
|
|
*/ |
77
|
|
|
public function testDomainForUrl() |
78
|
|
|
{ |
79
|
|
|
$result = MultiDomain::domain_for_url('foo.bar/my-page'); |
80
|
|
|
$this->assertInstanceOf(MultiDomainDomain::class, $result); |
|
|
|
|
81
|
|
|
$this->assertSame('primary', $result->getKey()); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Test that if a URL doesn't match any domain then the primary domain is returned |
86
|
|
|
*/ |
87
|
|
|
public function testDomainForUrlDefaultsToPrimaryDomain() |
88
|
|
|
{ |
89
|
|
|
$this->assertTrue(MultiDomain::domain_for_url('does-not-exist.com')->isPrimary()); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function tearDown() |
93
|
|
|
{ |
94
|
|
|
Config::unnest(); |
95
|
|
|
parent::tearDown(); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.