Passed
Branch merge-configs (a25828)
by Alan
03:09
created

ConfigTest::testMergedConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 56
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 38
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 56
rs 9.312

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
	Contracts\ConfigRepositoryInterface,
15
	AbstractConfig,
16
	AbstractConfigRepository,
17
	Config,
18
	ConfigFactory,
19
	ConfigRepository,
20
};
21
22
class ConfigTest extends AbstractTestCase
23
{
24
	/**
25
	 * @small
26
	 */
27
	public function testConfigRepository()
28
	{
29
		$factory = new ConfigFactory();
30
31
		$this->assertInstanceOf(ConfigFactoryInterface::class, $factory);
32
33
		$repo = new ConfigRepository($factory);
34
35
		$this->assertInstanceOf(ConfigRepositoryInterface::class, $repo);
36
		$this->assertInstanceOf(AbstractConfigRepository::class, $repo);
37
38
		$repo->addDirectory(__DIR__ . '/Data/Config/Alpha');
39
		$repo->addDirectory(__DIR__ . '/Data/Config/Beta');
40
41
		$directories = $repo->getDirectories();
42
43
		$this->assertIsArray($directories);
44
		$this->assertCount(2, $directories);
45
46
		$this->assertEquals($this->path(__DIR__, 'Data', 'Config', 'Alpha'), $directories[0]);
47
		$this->assertEquals($this->path(__DIR__, 'Data', 'Config', 'Beta'), $directories[1]);
48
	}
49
50
	/**
51
	 * @small
52
	 */
53
	public function testConfigRepositoryInvalidDirectory()
54
	{
55
		$factory = new ConfigFactory();
56
57
		$repo = new ConfigRepository($factory);
58
59
		$this->expectException(InvalidDirectoryException::class);
60
61
		$repo->addDirectory(__FILE__);
62
	}
63
64
	/**
65
	 * @small
66
	 */
67
	public function testConfigFactoryInvalidPath()
68
	{
69
		$factory = new ConfigFactory();
70
71
		$repo = new ConfigRepository($factory);
72
73
		$this->expectException(InvalidPathException::class);
74
75
		$repo->addDirectory('foo');
76
	}
77
78
	/**
79
	 * @small
80
	 */
81
	public function testConfigInvalid()
82
	{
83
		$factory = new ConfigFactory();
84
85
		$repo = new ConfigRepository($factory);
86
87
		$repo->addDirectory(__DIR__ . '/Data/Config/Alpha');
88
89
		$this->expectException(InvalidConfigFileException::class);
90
91
		$repo->get('invalid');
92
	}
93
94
	/**
95
	 * @small
96
	 */
97
	public function testConfigOutOfBounds()
98
	{
99
		$factory = new ConfigFactory();
100
101
		$repo = new ConfigRepository($factory);
102
103
		$repo->addDirectory(__DIR__ . '/Data/Config/Alpha');
104
105
		$this->expectException(InvalidConfigFilePathException::class);
106
107
		$repo->get('../oob');
108
	}
109
110
	/**
111
	 * @small
112
	 */
113
	public function testConfigFactoryCreate()
114
	{
115
		$factory = new ConfigFactory();
116
117
		$config = $factory->create([
118
			__DIR__ . '/Data/Config/Alpha/foo.php'
119
		]);
120
121
		$this->assertInstanceOf(ConfigInterface::class, $config);
122
		$this->assertInstanceOf(AbstractConfig::class, $config);
123
		$this->assertInstanceOf(Config::class, $config);
124
125
		$paths = $config->getPaths();
126
127
		$this->assertCount(1, $paths);
128
		$this->assertEquals($this->path(__DIR__, 'Data', 'Config', 'Alpha', 'foo.php'), $paths[0]);
129
	}
130
131
	/**
132
	 * @small
133
	 */
134
	public function testConfigRepositoryGet()
135
	{
136
		$factory = new ConfigFactory();
137
138
		$repo = new ConfigRepository($factory);
139
140
		$repo->addDirectory(__DIR__ . '/Data/Config/Alpha');
141
		$repo->addDirectory(__DIR__ . '/Data/Config/Beta');
142
143
		$config = $repo->get('oof');
144
145
		$this->assertInstanceOf(ConfigInterface::class, $config);
146
		$this->assertInstanceOf(AbstractConfig::class, $config);
147
		$this->assertInstanceOf(Config::class, $config);
148
149
		$paths = $config->getPaths();
150
151
		$this->assertEquals($this->path(__DIR__, 'Data', 'Config', 'Beta', 'oof.php'), $paths[0]);
152
153
		$this->assertEquals('Service Name', $config['name']);
154
		$this->assertEquals('main', $config['server']);
155
156
		$servers = $config['servers'];
157
158
		$this->assertIsArray($servers);
159
		$this->assertArrayHasKey('main', $servers);
160
		$this->assertArrayHasKey('alt', $servers);
161
162
		$this->assertArrayHasKey($config['server'], $servers);
163
164
		$main = $servers['main'];
165
166
		$this->assertIsArray($main);
167
168
		$this->assertArrayHasKey('host', $main);
169
		$this->assertIsString($main['host']);
170
		$this->assertEquals('http://www.service.com', $main['host']);
171
172
		$this->assertArrayHasKey('port', $main);
173
		$this->assertIsInt($main['port']);
174
		$this->assertEquals(80, $main['port']);
175
176
		$this->assertArrayHasKey('secure', $main);
177
		$this->assertIsBool($main['secure']);
178
		$this->assertEquals(false, $main['secure']);
179
180
		$alt = $servers['alt'];
181
182
		$this->assertIsArray($alt);
183
184
		$this->assertArrayHasKey('host', $alt);
185
		$this->assertIsString($alt['host']);
186
		$this->assertEquals('https://www.service.com', $alt['host']);
187
188
		$this->assertArrayHasKey('port', $alt);
189
		$this->assertIsInt($alt['port']);
190
		$this->assertEquals(443, $alt['port']);
191
192
		$this->assertArrayHasKey('secure', $alt);
193
		$this->assertIsBool($alt['secure']);
194
		$this->assertEquals(true, $alt['secure']);
195
	}
196
197
	/**
198
	 * @small
199
	 */
200
	public function testMergedConfig()
201
	{
202
		$factory = new ConfigFactory();
203
204
		$repo = new ConfigRepository($factory);
205
206
		$repo->addDirectory(__DIR__ . '/Data/Config/Alpha');
207
		$repo->addDirectory(__DIR__ . '/Data/Config/Beta');
208
209
		$config = $repo->get('bar');
210
211
		$this->assertEquals('new', $config['name']);
212
213
		$this->assertArrayHasKey('alpha', $config);
214
215
		$alpha = $config['alpha'];
216
		$this->assertArrayHasKey('colors', $alpha);
217
		$this->assertIsArray($alpha['colors']);
218
219
		$this->assertArrayHasKey('beta', $config);
220
221
		$beta = $config['beta'];
222
		$this->assertArrayHasKey('shapes', $beta);
223
		$this->assertIsArray($beta['shapes']);
224
225
		$this->assertArrayHasKey('servers', $config);
226
227
		$servers = $config['servers'];
228
		$this->assertCount(3, $servers);
229
230
		$server = $servers[0];
231
232
		$this->assertEquals('https://service.net', $server['host']);
233
		$this->assertEquals(443, $server['port']);
234
		$this->assertEquals('/api', $server['path']);
235
		$this->assertEquals('username', $server['username']);
236
		$this->assertEquals('This is my password, actually.', $server['password']);
237
		$this->assertEquals(1, $server['version']);
238
239
		$server = $servers[1];
240
241
		$this->assertEquals('https://service.net', $server['host']);
242
		$this->assertEquals(443, $server['port']);
243
		$this->assertEquals('/api', $server['path']);
244
		$this->assertEquals('username', $server['username']);
245
		$this->assertEquals('This is my password, actually.', $server['password']);
246
		$this->assertEquals(2, $server['version']);
247
248
		$server = $servers[2];
249
250
		$this->assertEquals('https://service.org', $server['host']);
251
		$this->assertEquals(443, $server['port']);
252
		$this->assertEquals('/api', $server['path']);
253
		$this->assertEquals('slj', $server['username']);
254
		$this->assertEquals('sn@kes on a Plane!', $server['password']);
255
		$this->assertEquals(2, $server['version']);
256
	}
257
}
258