1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\MultiDomain\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Config\Config; |
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
7
|
|
|
use SilverStripe\MultiDomain\MultiDomain; |
8
|
|
|
use SilverStripe\MultiDomain\MultiDomainDomain; |
9
|
|
|
|
10
|
|
|
class MultiDomainTest extends SapphireTest |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Set up some test domain configuration |
14
|
|
|
* |
15
|
|
|
* {@inheritDoc} |
16
|
|
|
*/ |
17
|
|
|
public function setUp() |
18
|
|
|
{ |
19
|
|
|
parent::setUp(); |
20
|
|
|
Config::nest(); |
21
|
|
|
|
22
|
|
|
Config::inst()->remove(MultiDomain::class, 'domains'); |
|
|
|
|
23
|
|
|
Config::inst()->update(MultiDomain::class, 'domains', array( |
|
|
|
|
24
|
|
|
'primary' => array( |
25
|
|
|
'hostname' => 'foo.bar', |
26
|
|
|
'resolves_to' => 'bar.baz' |
27
|
|
|
), |
28
|
|
|
'secondary' => array( |
29
|
|
|
'hostname' => 'localhost', |
30
|
|
|
'resolves_to' => 'local.dev' |
31
|
|
|
) |
32
|
|
|
)); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Test that a MultiDomainDomain can be returned from the configured domains |
37
|
|
|
*/ |
38
|
|
|
public function testGetDomain() |
39
|
|
|
{ |
40
|
|
|
$this->assertNull(MultiDomain::get_domain('does-not-exist')); |
41
|
|
|
$this->assertInstanceOf(MultiDomainDomain::class, MultiDomain::get_domain('primary')); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Test that all domains can be returned, with or without the primary domain |
46
|
|
|
* |
47
|
|
|
* @dataProvider getAllDomainsProvider |
48
|
|
|
* @param bool $withPrimary |
49
|
|
|
*/ |
50
|
|
|
public function testGetAllDomains($withPrimary) |
51
|
|
|
{ |
52
|
|
|
$result = MultiDomain::get_all_domains($withPrimary); |
53
|
|
|
$this->assertInternalType('array', $result); |
54
|
|
|
$this->assertNotEmpty($result); |
55
|
|
|
|
56
|
|
|
$expectedCount = $withPrimary ? 2 : 1; |
57
|
|
|
$this->assertCount($expectedCount, $result); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return array[] |
62
|
|
|
*/ |
63
|
|
|
public function getAllDomainsProvider() |
64
|
|
|
{ |
65
|
|
|
return array( |
66
|
|
|
array(true), |
67
|
|
|
array(false) |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Test that the primary domain can be returned |
73
|
|
|
*/ |
74
|
|
|
public function testGetPrimaryDomain() |
75
|
|
|
{ |
76
|
|
|
$result = MultiDomain::get_primary_domain(); |
77
|
|
|
$this->assertInstanceOf(MultiDomainDomain::class, $result); |
78
|
|
|
$this->assertTrue($result->isPrimary()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Test that the correct domain can be returned by a provided URL |
83
|
|
|
*/ |
84
|
|
|
public function testDomainForUrl() |
85
|
|
|
{ |
86
|
|
|
$result = MultiDomain::domain_for_url('foo.bar/my-page'); |
87
|
|
|
$this->assertInstanceOf(MultiDomainDomain::class, $result); |
88
|
|
|
$this->assertSame('primary', $result->getKey()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Test that if a URL doesn't match any domain then the primary domain is returned |
93
|
|
|
*/ |
94
|
|
|
public function testDomainForUrlDefaultsToPrimaryDomain() |
95
|
|
|
{ |
96
|
|
|
$this->assertTrue(MultiDomain::domain_for_url('does-not-exist.com')->isPrimary()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function tearDown() |
100
|
|
|
{ |
101
|
|
|
Config::unnest(); |
102
|
|
|
parent::tearDown(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: