3 namespace Friendica\Test\Core\Config\Cache;
5 use Friendica\Core\Config\Cache\ConfigCache;
6 use Friendica\Test\MockedTest;
8 class ConfigCacheTest extends MockedTest
10 public function dataTests()
21 'array' => ['1', 2, '3', true, false],
31 private function assertConfigValues($data, ConfigCache $configCache, $uid = null)
33 foreach ($data as $cat => $values) {
34 foreach ($values as $key => $value) {
36 $this->assertEquals($data[$cat][$key], $configCache->getP($uid, $cat, $key));
38 $this->assertEquals($data[$cat][$key], $configCache->get($cat, $key));
45 * Test the loadConfigArray() method without override
46 * @dataProvider dataTests
48 public function testLoadConfigArray($data)
50 $configCache = new ConfigCache();
51 $configCache->load($data);
53 $this->assertConfigValues($data, $configCache);
57 * Test the loadConfigArray() method with overrides
58 * @dataProvider dataTests
60 public function testLoadConfigArrayOverride($data)
69 $configCache = new ConfigCache();
70 $configCache->load($data);
71 $configCache->load($override);
73 $this->assertConfigValues($data, $configCache);
76 $configCache->load($override, true);
78 $this->assertEquals($override['system']['test'], $configCache->get('system', 'test'));
79 $this->assertEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
83 * Test the loadConfigArray() method with wrong/empty datasets
85 public function testLoadConfigArrayWrong()
87 $configCache = new ConfigCache();
90 $configCache->load([]);
91 $this->assertEmpty($configCache->getAll());
94 $configCache->load(['system' => 'not_array']);
95 $this->assertEmpty($configCache->getAll());
97 // incomplete dataset (key is integer ID of the array)
98 $configCache->load(['system' => ['value']]);
99 $this->assertEquals('value', $configCache->get('system', 0));
103 * Test the getAll() method
104 * @dataProvider dataTests
106 public function testGetAll($data)
108 $configCache = new ConfigCache();
109 $configCache->load($data);
111 $all = $configCache->getAll();
113 $this->assertContains($data['system'], $all);
114 $this->assertContains($data['config'], $all);
118 * Test the set() and get() method
119 * @dataProvider dataTests
121 public function testSetGet($data)
123 $configCache = new ConfigCache();
125 foreach ($data as $cat => $values) {
126 foreach ($values as $key => $value) {
127 $configCache->set($cat, $key, $value);
131 $this->assertConfigValues($data, $configCache);
135 * Test the get() method without a value
137 public function testGetEmpty()
139 $configCache = new ConfigCache();
141 $this->assertEquals('!<unset>!', $configCache->get('something', 'value'));
145 * Test the has() method
147 public function testHas()
149 $configCache = new ConfigCache();
151 $this->assertFalse($configCache->has('system', 'test'));
153 $configCache->set('system', 'test', 'it');
154 $this->assertTrue($configCache->has('system', 'test'));
156 $this->assertFalse($configCache->has('system', null));
157 $configCache->set('system', null, 'it');
158 $this->assertTrue($configCache->has('system', null));
162 * Test the delete() method
163 * @dataProvider dataTests
165 public function testDelete($data)
167 $configCache = new ConfigCache($data);
169 foreach ($data as $cat => $values) {
170 foreach ($values as $key => $value) {
171 $configCache->delete($cat, $key);
175 $this->assertEmpty($configCache->getAll());
179 * Test the setP() and getP() methods
180 * @dataProvider dataTests
182 public function testSetGetP($data)
184 $configCache = new ConfigCache();
187 foreach ($data as $cat => $values) {
188 foreach ($values as $key => $value) {
189 $configCache->setP($uid, $cat, $key, $value);
193 $this->assertConfigValues($data, $configCache, $uid);
198 * Test the deleteP() method
199 * @dataProvider dataTests
201 public function testDeleteP($data)
203 $configCache = new ConfigCache();
206 foreach ($data as $cat => $values) {
207 foreach ($values as $key => $value) {
208 $configCache->setP($uid, $cat, $key, $value);
212 foreach ($data as $cat => $values) {
213 foreach ($values as $key => $value) {
214 $configCache->deleteP($uid, $cat, $key);
218 $this->assertEmpty($configCache->getAll());
222 * Test the hasP() method
224 public function testHasP()
226 $configCache = new ConfigCache();
229 $this->assertFalse($configCache->hasP($uid, 'system', 'test'));
231 $configCache->setP($uid, 'system', 'test', 'it');
232 $this->assertTrue($configCache->hasP($uid, 'system', 'test'));
234 $this->assertFalse($configCache->hasP($uid, 'system', null));
235 $configCache->setP($uid, 'system', null, 'it');
236 $this->assertTrue($configCache->hasP($uid, 'system', null));