AbstractConfigManagerTest.php
5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
namespace clagiordano\weblibs\configmanager\tests;
use clagiordano\weblibs\configmanager\IConfigurable;
use PHPUnit\Framework\TestCase;
/**
* Class AbstractConfigManagerTest
* @package clagiordano\weblibs\configmanager\tests
*/
abstract class AbstractConfigManagerTest extends TestCase
{
/** @var string $configFile */
protected $configFile = null;
/** @var IConfigurable $config */
protected $config = null;
public static function setUpBeforeClass()
{
parent::setUpBeforeClass();
/**
* Remove temp dir
*/
shell_exec("rm -rf " . __DIR__ . '/../testsdata/temp');
/**
* Create temp dir
*/
$status = mkdir(__DIR__ . '/../testsdata/temp/');
self::assertTrue($status);
}
public static function tearDownAfterClass()
{
parent::tearDownAfterClass();
/**
* Remove temp dir
*/
shell_exec("rm -rf " . __DIR__ . '/../testsdata/temp');
}
public function testBasicUsage()
{
$this->assertNotNull(
$this->config->getValue('app')
);
}
public function testFastUsage()
{
$this->assertNotNull(
$this->config->getValue('app')
);
}
public function testFastInvalidKey()
{
$this->assertNull(
$this->config->getValue('invalidKey')
);
}
public function testFastInvalidKeyWithDefault()
{
$this->assertEquals(
$this->config->getValue('invalidKey', 'defaultValue'),
'defaultValue'
);
}
public function testFastNestedConfig()
{
$this->assertNotNull(
$this->config->getValue('other.multi.deep.nested')
);
}
public function testCheckExistConfig()
{
$this->assertTrue(
$this->config->existValue('other.multi.deep.nested')
);
}
public function testCheckNotExistConfig()
{
$this->assertFalse(
$this->config->existValue('invalid.config.path')
);
}
public function testSetValue()
{
$this->config->setValue('other.multi.deep.nested', __FUNCTION__);
$this->assertEquals(
$this->config->getValue('other.multi.deep.nested'),
__FUNCTION__
);
}
public function testFailedSaveConfig()
{
$this->setExpectedException('Exception');
$this->config->saveConfigFile('/invalid/path');
}
public function testSuccessSaveConfigOnTempAndReload()
{
$this->config->setValue('other.multi.deep.nested', "SUPERNESTED");
$this->config->saveConfigFile("/tmp/testconfig.sample", true);
$this->assertEquals(
$this->config->getValue('other.multi.deep.nested'),
"SUPERNESTED"
);
}
public function testOverwriteSameConfigFile()
{
$this->config->saveConfigFile();
}
public function testFailWriteConfig()
{
$this->setExpectedException('\RuntimeException');
$this->config->saveConfigFile('/invalid/path/test.sample');
}
/**
* @test
* @group permissions
*/
public function canRaiseExceptionOnUnreadableFile()
{
/**
* Create new temp file
*/
$testFile = tempnam(__DIR__ . '/../testsdata/temp/', 'phpunit_');
self::assertFileExists($testFile);
/**
* Make tempfile unreadable by everyone, but still writeable
*/
$status = chmod($testFile, 0200);
self::assertTrue($status);
/**
* Check permissions it must be 0200 ( --w------- )
*/
$filePerms = (fileperms($testFile) & 0777);
self::assertSame(0200, $filePerms);
/**
* Try to read that file, an exception must be thrown
*/
self::setExpectedException('\RuntimeException');
$this->config->loadConfig($testFile);
/**
* Remove temp file
*/
$status = chmod($testFile, 0744);
self::assertTrue($status);
$filePerms = (fileperms($testFile) & 0777);
self::assertSame(0755, $filePerms);
$status = unlink($testFile);
self::assertTrue($status);
self::assertFileNotExists($testFile);
}
/**
* @return array
*/
public function configDataProvider()
{
return [
[
__DIR__ . '/../testsdata/sample_config_data.converted.yml',
'\clagiordano\weblibs\configmanager\YamlConfigManager',
],
[
__DIR__ . '/../testsdata/sample_config_data.converted.json',
'\clagiordano\weblibs\configmanager\JsonConfigManager',
],
[
__DIR__ . '/../testsdata/sample_config_data.converted.php',
'\clagiordano\weblibs\configmanager\ArrayConfigManager',
],
];
}
/**
* @test
* @dataProvider configDataProvider
* @param mixed $targetConfig
* @param mixed $targetInstance
*/
public function canConvertOneFormatToAnother($targetConfig, $targetInstance)
{
if (file_exists($targetConfig)) {
/**
* Drop target file if already existing
*/
unlink($targetConfig);
}
self::assertFileNotExists($targetConfig);
$target = new $targetInstance($targetConfig);
self::assertInstanceOf($targetInstance, $target);
$converted = $this->config->convert($target);
self::assertInstanceOf($targetInstance, $converted);
self::assertFileNotExists($targetConfig);
}
}