1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace KochTest\Config; |
4
|
|
|
|
5
|
|
|
use Koch\Config\Config; |
6
|
|
|
use org\bovigo\vfs\vfsStream; |
7
|
|
|
use org\bovigo\vfs\vfsStreamDirectory; |
8
|
|
|
use org\bovigo\vfs\vfsStreamWrapper; |
9
|
|
|
|
10
|
|
|
class AbstractConfigTest extends \PHPUnit_Framework_TestCase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var AbstractConfig |
14
|
|
|
*/ |
15
|
|
|
protected $object; |
16
|
|
|
|
17
|
|
View Code Duplication |
public function setUp() |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
// we are using the config adapter native PHP here, |
20
|
|
|
// it's a class extending the abstract class |
21
|
|
|
// abstract classes cannot be instantiated |
22
|
|
|
$this->object = new Config(); |
|
|
|
|
23
|
|
|
|
24
|
|
|
vfsStreamWrapper::register(); |
25
|
|
|
$this->configFileURL = vfsStream::url('root/test.config.php'); |
|
|
|
|
26
|
|
|
$this->file = vfsStream::newFile('test.config.php', 0777)->withContent($this->getConfigFileContent()); |
|
|
|
|
27
|
|
|
|
28
|
|
|
$this->configFileURL2 = vfsStream::url('root/test2.config.php'); |
|
|
|
|
29
|
|
|
$this->file2 = vfsStream::newFile('test2.config.php', 0777)->withContent(''); |
|
|
|
|
30
|
|
|
|
31
|
|
|
$this->root = new vfsStreamDirectory('root'); |
|
|
|
|
32
|
|
|
$this->root->addChild($this->file); |
33
|
|
|
$this->root->addChild($this->file2); |
34
|
|
|
vfsStreamWrapper::setRoot($this->root); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function tearDown() |
38
|
|
|
{ |
39
|
|
|
unset($this->object); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getConfigFileContent() |
43
|
|
|
{ |
44
|
|
|
return <<<EOF |
45
|
|
|
<?php |
46
|
|
|
// Configuration File generated by Koch Framework. |
47
|
|
|
return array( |
48
|
|
|
"oldKey" => "value" |
49
|
|
|
); |
50
|
|
|
EOF; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getConfigArray() |
54
|
|
|
{ |
55
|
|
|
return [ |
56
|
|
|
'oldKey' => 'value', |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @covers Koch\Config\Adapter\PHP::read |
62
|
|
|
* @covers Koch\Config\AbstractConfig::toArray |
63
|
|
|
*/ |
64
|
|
|
public function testToArray() |
65
|
|
|
{ |
66
|
|
|
$array = $this->object->read($this->configFileURL); |
67
|
|
|
$this->assertEquals($this->getConfigArray(), $this->object->toArray()); |
68
|
|
|
|
69
|
|
|
// unset, returns the array one last time |
70
|
|
|
$this->assertEquals($array, $this->object->toArray(true)); |
71
|
|
|
$this->assertEquals([], $this->object->toArray()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @covers Koch\Config\AbstractConfig::merge |
76
|
|
|
*/ |
77
|
|
|
public function testMerge() |
78
|
|
|
{ |
79
|
|
|
// read old config |
80
|
|
|
$this->object->read($this->configFileURL); |
81
|
|
|
|
82
|
|
|
// merge new values |
83
|
|
|
$newConfig = ['newKey' => 'newKeyValue']; |
84
|
|
|
$this->object->merge($newConfig); |
85
|
|
|
|
86
|
|
|
$this->assertArrayHasKey('oldKey', $this->object->toArray()); |
87
|
|
|
$this->assertArrayHasKey('newKey', $this->object->toArray()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @covers Koch\Config\AbstractConfig::getConfigValue |
92
|
|
|
*/ |
93
|
|
|
public function testGetConfigValue() |
94
|
|
|
{ |
95
|
|
|
// read old config |
96
|
|
|
$this->object->read($this->configFileURL); |
97
|
|
|
|
98
|
|
|
$this->assertEquals('value', $this->object->getConfigValue('oldKey')); |
99
|
|
|
|
100
|
|
|
// not existing key, returns null |
101
|
|
|
$this->assertEquals(null, $this->object->getConfigValue('notExistingKey')); |
102
|
|
|
|
103
|
|
|
$this->assertEquals('defaultValue', $this->object->getConfigValue('notExistingKey', 'defaultValue')); |
104
|
|
|
|
105
|
|
|
$this->assertEquals( |
106
|
|
|
'defaultValueTwo', |
107
|
|
|
$this->object->getConfigValue('notExistingKey', null, 'defaultValueTwo') |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
$this->assertEquals( |
111
|
|
|
'defaultValue', |
112
|
|
|
$this->object->getConfigValue('notExistingKey', 'defaultValue', 'defaultValueTwo') |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @covers Koch\Config\AbstractConfig::__get |
118
|
|
|
*/ |
119
|
|
|
public function test__get() |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
// read old config |
122
|
|
|
$this->object->read($this->configFileURL); |
123
|
|
|
$this->assertEquals('value', $this->object->oldKey); |
124
|
|
|
|
125
|
|
|
$this->assertNull($this->object->notExistingKey); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @covers Koch\Config\AbstractConfig::__set |
130
|
|
|
* @covers Koch\Config\AbstractConfig::__isset |
131
|
|
|
* @covers Koch\Config\AbstractConfig::__unset |
132
|
|
|
*/ |
133
|
|
View Code Duplication |
public function test__set() |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
// __set |
136
|
|
|
$this->object->newKey = 'someValue'; |
137
|
|
|
$this->assertEquals('someValue', $this->object->newKey); |
138
|
|
|
|
139
|
|
|
// __isset |
140
|
|
|
$this->assertTrue(isset($this->object->newKey)); |
141
|
|
|
|
142
|
|
|
// __unset |
143
|
|
|
unset($this->object->newKey); |
144
|
|
|
$this->assertFalse(isset($this->object->newKey)); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @covers Koch\Config\AbstractConfig::offsetExists |
149
|
|
|
*/ |
150
|
|
|
public function testOffsetExists() |
151
|
|
|
{ |
152
|
|
|
$this->object->newKey = 'someValue'; |
153
|
|
|
|
154
|
|
|
$this->assertFalse(empty($this->object['newKey'])); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @covers Koch\Config\AbstractConfig::offsetGet |
159
|
|
|
*/ |
160
|
|
|
public function testOffsetGet() |
161
|
|
|
{ |
162
|
|
|
$this->object->newKey = 'someValue'; |
163
|
|
|
|
164
|
|
|
$this->assertEquals('someValue', $this->object['newKey']); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @covers Koch\Config\AbstractConfig::offsetSet |
169
|
|
|
*/ |
170
|
|
|
public function testOffsetSet() |
171
|
|
|
{ |
172
|
|
|
$this->object['newKey'] = 'someValue'; |
173
|
|
|
$this->assertEquals('someValue', $this->object['newKey']); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @covers Koch\Config\AbstractConfig::offsetUnset |
178
|
|
|
* |
179
|
|
|
* @todo Implement testOffsetUnset(). |
180
|
|
|
*/ |
181
|
|
|
public function testOffsetUnset() |
182
|
|
|
{ |
183
|
|
|
unset($this->object['newKey']); |
184
|
|
|
|
185
|
|
|
$this->assertFalse(isset($this->object->newKey)); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.