1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FigTree\Config\Tests; |
4
|
|
|
|
5
|
|
|
use FigTree\Exceptions\{ |
6
|
|
|
InvalidDirectoryException, |
7
|
|
|
InvalidPathException, |
8
|
|
|
}; |
9
|
|
|
use FigTree\Config\{ |
10
|
|
|
Exceptions\InvalidConfigFileException, |
11
|
|
|
Exceptions\InvalidConfigFilePathException, |
12
|
|
|
Contracts\ConfigInterface, |
13
|
|
|
Contracts\ConfigFactoryInterface, |
14
|
|
|
AbstractConfig, |
15
|
|
|
AbstractConfigFactory, |
16
|
|
|
Config, |
17
|
|
|
ConfigFactory, |
18
|
|
|
}; |
19
|
|
|
|
20
|
|
|
class ConfigTest extends AbstractTestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @small |
24
|
|
|
*/ |
25
|
|
|
public function testConfigFactory() |
26
|
|
|
{ |
27
|
|
|
$factory = new ConfigFactory(); |
28
|
|
|
|
29
|
|
|
$this->assertInstanceOf(ConfigFactoryInterface::class, $factory); |
30
|
|
|
$this->assertInstanceOf(AbstractConfigFactory::class, $factory); |
31
|
|
|
$this->assertInstanceOf(ConfigFactory::class, $factory); |
32
|
|
|
|
33
|
|
|
$factory->addDirectory(__DIR__ . '/Data/Config/Alpha'); |
34
|
|
|
$factory->addDirectory(__DIR__ . '/Data/Config/Beta'); |
35
|
|
|
|
36
|
|
|
$directories = $factory->getDirectories(); |
37
|
|
|
|
38
|
|
|
$this->assertIsArray($directories); |
39
|
|
|
$this->assertCount(2, $directories); |
40
|
|
|
|
41
|
|
|
$this->assertEquals($this->path(__DIR__, 'Data', 'Config', 'Alpha'), $directories[0]); |
42
|
|
|
$this->assertEquals($this->path(__DIR__, 'Data', 'Config', 'Beta'), $directories[1]); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @small |
47
|
|
|
*/ |
48
|
|
|
public function testConfigFactoryInvalidDirectory() |
49
|
|
|
{ |
50
|
|
|
$factory = new ConfigFactory(); |
51
|
|
|
|
52
|
|
|
$this->expectException(InvalidDirectoryException::class); |
53
|
|
|
|
54
|
|
|
$factory->addDirectory(__FILE__); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @small |
59
|
|
|
*/ |
60
|
|
|
public function testConfigFactoryInvalidPath() |
61
|
|
|
{ |
62
|
|
|
$factory = new ConfigFactory(); |
63
|
|
|
|
64
|
|
|
$this->expectException(InvalidPathException::class); |
65
|
|
|
|
66
|
|
|
$factory->addDirectory('foo'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @small |
71
|
|
|
*/ |
72
|
|
|
public function testConfigInvalid() |
73
|
|
|
{ |
74
|
|
|
$factory = new ConfigFactory(); |
75
|
|
|
|
76
|
|
|
$factory->addDirectory(__DIR__ . '/Data/Config/Alpha'); |
77
|
|
|
|
78
|
|
|
$this->expectException(InvalidConfigFileException::class); |
79
|
|
|
|
80
|
|
|
$factory->get('invalid'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @small |
85
|
|
|
*/ |
86
|
|
|
public function testConfigOutOfBounds() |
87
|
|
|
{ |
88
|
|
|
$factory = new ConfigFactory(); |
89
|
|
|
|
90
|
|
|
$factory->addDirectory(__DIR__ . '/Data/Config/Alpha'); |
91
|
|
|
|
92
|
|
|
$this->expectException(InvalidConfigFilePathException::class); |
93
|
|
|
|
94
|
|
|
$factory->get('../oob'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @small |
99
|
|
|
*/ |
100
|
|
|
public function testConfigFactoryCreate() |
101
|
|
|
{ |
102
|
|
|
$factory = new ConfigFactory(); |
103
|
|
|
|
104
|
|
|
$config = $factory->create(__DIR__ . '/Data/Config/Alpha/foo.php'); |
105
|
|
|
|
106
|
|
|
$this->assertInstanceOf(ConfigInterface::class, $config); |
107
|
|
|
$this->assertInstanceOf(AbstractConfig::class, $config); |
108
|
|
|
$this->assertInstanceOf(Config::class, $config); |
109
|
|
|
|
110
|
|
|
$this->assertEquals($this->path(__DIR__, 'Data', 'Config', 'Alpha', 'foo.php'), $config->getFileName()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @small |
115
|
|
|
*/ |
116
|
|
|
public function testConfigFactoryGet() |
117
|
|
|
{ |
118
|
|
|
$factory = new ConfigFactory(); |
119
|
|
|
|
120
|
|
|
$factory->addDirectory(__DIR__ . '/Data/Config/Alpha'); |
121
|
|
|
$factory->addDirectory(__DIR__ . '/Data/Config/Beta'); |
122
|
|
|
|
123
|
|
|
$config = $factory->get('oof'); |
124
|
|
|
|
125
|
|
|
$this->assertInstanceOf(ConfigInterface::class, $config); |
126
|
|
|
$this->assertInstanceOf(AbstractConfig::class, $config); |
127
|
|
|
$this->assertInstanceOf(Config::class, $config); |
128
|
|
|
|
129
|
|
|
$this->assertEquals($this->path(__DIR__, 'Data', 'Config', 'Beta', 'oof.php'), $config->getFileName()); |
130
|
|
|
|
131
|
|
|
$this->assertEquals('Service Name', $config['name']); |
132
|
|
|
$this->assertEquals('main', $config['server']); |
133
|
|
|
|
134
|
|
|
$servers = $config['servers']; |
135
|
|
|
|
136
|
|
|
$this->assertIsArray($servers); |
137
|
|
|
$this->assertArrayHasKey('main', $servers); |
138
|
|
|
$this->assertArrayHasKey('alt', $servers); |
139
|
|
|
|
140
|
|
|
$this->assertArrayHasKey($config['server'], $servers); |
141
|
|
|
|
142
|
|
|
$main = $servers['main']; |
143
|
|
|
|
144
|
|
|
$this->assertIsArray($main); |
145
|
|
|
|
146
|
|
|
$this->assertArrayHasKey('host', $main); |
147
|
|
|
$this->assertIsString($main['host']); |
148
|
|
|
$this->assertEquals('http://www.service.com', $main['host']); |
149
|
|
|
|
150
|
|
|
$this->assertArrayHasKey('port', $main); |
151
|
|
|
$this->assertIsInt($main['port']); |
152
|
|
|
$this->assertEquals(80, $main['port']); |
153
|
|
|
|
154
|
|
|
$this->assertArrayHasKey('secure', $main); |
155
|
|
|
$this->assertIsBool($main['secure']); |
156
|
|
|
$this->assertEquals(false, $main['secure']); |
157
|
|
|
|
158
|
|
|
$alt = $servers['alt']; |
159
|
|
|
|
160
|
|
|
$this->assertIsArray($alt); |
161
|
|
|
|
162
|
|
|
$this->assertArrayHasKey('host', $alt); |
163
|
|
|
$this->assertIsString($alt['host']); |
164
|
|
|
$this->assertEquals('https://www.service.com', $alt['host']); |
165
|
|
|
|
166
|
|
|
$this->assertArrayHasKey('port', $alt); |
167
|
|
|
$this->assertIsInt($alt['port']); |
168
|
|
|
$this->assertEquals(443, $alt['port']); |
169
|
|
|
|
170
|
|
|
$this->assertArrayHasKey('secure', $alt); |
171
|
|
|
$this->assertIsBool($alt['secure']); |
172
|
|
|
$this->assertEquals(true, $alt['secure']); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|