ConfigTest::testMergedConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 54
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 37
nc 1
nop 0
dl 0
loc 54
rs 9.328
c 0
b 0
f 0

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