3 namespace Friendica\Test\src\Core\Config;
5 use Friendica\Core\Config\Adapter\IConfigAdapter;
6 use Friendica\Core\Config\Cache\ConfigCache;
7 use Friendica\Core\Config\Cache\IConfigCache;
8 use Friendica\Core\Config\Configuration;
9 use Friendica\Test\MockedTest;
11 class ConfigurationTest extends MockedTest
13 public function dataTests()
16 'string' => ['data' => 'it'],
17 'boolTrue' => ['data' => true],
18 'boolFalse' => ['data' => false],
19 'integer' => ['data' => 235],
20 'decimal' => ['data' => 2.456],
21 'array' => ['data' => ['1', 2, '3', true, false]],
22 'boolIntTrue' => ['data' => 1],
23 'boolIntFalse' => ['Data' => 0],
28 * Test the configuration initialization
30 public function testSetUp()
32 $configCache = new ConfigCache();
33 $configAdapter = \Mockery::mock(IConfigAdapter::class);
34 $configAdapter->shouldReceive('isConnected')->andReturn(false)->once();
36 $configuration = new Configuration($configCache, $configAdapter);
38 $this->assertInstanceOf(IConfigCache::class, $configuration->getCache());
42 * Test the configuration load() method
44 public function testCacheLoad()
46 $configCache = new ConfigCache();
47 $configAdapter = \Mockery::mock(IConfigAdapter::class);
48 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(3);
49 // constructor loading
50 $configAdapter->shouldReceive('load')->andReturn([])->once();
52 $configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'it']])->once();
53 $configAdapter->shouldReceive('isLoaded')->with('testing', 'test')->andReturn(true)->once();
55 $configuration = new Configuration($configCache, $configAdapter);
56 $configuration->load('testing');
58 $this->assertEquals('it', $configuration->get('testing', 'test'));
59 $this->assertEquals('it', $configuration->getCache()->get('testing', 'test'));
63 * Test the configuration load() method with overwrite
65 public function testCacheLoadDouble()
67 $configCache = new ConfigCache();
68 $configAdapter = \Mockery::mock(IConfigAdapter::class);
69 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(5);
70 // constructor loading
71 $configAdapter->shouldReceive('load')->andReturn([])->once();
73 $configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'it']])->once();
74 $configAdapter->shouldReceive('isLoaded')->with('testing', 'test')->andReturn(true)->twice();
75 // expected next loading
76 $configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'again']])->once();
78 $configuration = new Configuration($configCache, $configAdapter);
79 $configuration->load('testing');
81 $this->assertEquals('it', $configuration->get('testing', 'test'));
82 $this->assertEquals('it', $configuration->getCache()->get('testing', 'test'));
84 $configuration->load('testing');
86 $this->assertEquals('again', $configuration->get('testing', 'test'));
87 $this->assertEquals('again', $configuration->getCache()->get('testing', 'test'));
91 * Test the configuration get() and set() methods without adapter
92 * @dataProvider dataTests
94 public function testSetGetWithoutDB($data)
96 $configCache = new ConfigCache();
97 $configAdapter = \Mockery::mock(IConfigAdapter::class);
98 $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(3);
100 $configuration = new Configuration($configCache, $configAdapter);
102 $this->assertTrue($configuration->set('test', 'it', $data));
104 $this->assertEquals($data, $configuration->get('test', 'it'));
105 $this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
109 * Test the configuration get() and set() methods with adapter
110 * @dataProvider dataTests
112 public function testSetGetWithDB($data)
114 $configCache = new ConfigCache();
115 $configAdapter = \Mockery::mock(IConfigAdapter::class);
116 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(3);
117 // constructor loading
118 $configAdapter->shouldReceive('load')->andReturn([])->once();
119 $configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(true)->once();
120 $configAdapter->shouldReceive('set')->with('test', 'it', $data)->andReturn(true)->once();
122 $configuration = new Configuration($configCache, $configAdapter);
124 $this->assertTrue($configuration->set('test', 'it', $data));
126 $this->assertEquals($data, $configuration->get('test', 'it'));
127 $this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
131 * Test the configuration get() method with wrong value and no db
133 public function testGetWrongWithoutDB()
135 $configCache = new ConfigCache();
136 $configAdapter = \Mockery::mock(IConfigAdapter::class);
137 $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(4);
139 $configuration = new Configuration($configCache, $configAdapter);
142 $this->assertNull($configuration->get('test', 'it'));
144 /// beware that the cache returns '!<unset>!' and not null for a non existing value
145 $this->assertNull($configuration->getCache()->get('test', 'it'));
147 // with default value
148 $this->assertEquals('default', $configuration->get('test', 'it', 'default'));
150 // with default value and refresh
151 $this->assertEquals('default', $configuration->get('test', 'it', 'default', true));
155 * Test the configuration get() method with refresh
156 * @dataProvider dataTests
158 public function testGetWithRefresh($data)
160 $configCache = new ConfigCache(['test' => ['it' => 'now']]);
161 $configAdapter = \Mockery::mock(IConfigAdapter::class);
162 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(4);
163 // constructor loading
164 $configAdapter->shouldReceive('load')->andReturn([])->once();
165 $configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(true)->twice();
166 $configAdapter->shouldReceive('get')->with('test', 'it')->andReturn($data)->once();
167 $configAdapter->shouldReceive('isLoaded')->with('test', 'not')->andReturn(false)->once();
168 $configAdapter->shouldReceive('get')->with('test', 'not')->andReturn(null)->once();
170 $configuration = new Configuration($configCache, $configAdapter);
173 $this->assertEquals('now', $configuration->get('test', 'it'));
174 $this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
177 $this->assertEquals($data, $configuration->get('test', 'it', null, true));
178 $this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
180 // without refresh and wrong value and default
181 $this->assertEquals('default', $configuration->get('test', 'not', 'default'));
182 $this->assertNull($configuration->getCache()->get('test', 'not'));
186 * Test the configuration get() method with different isLoaded settings
187 * @dataProvider dataTests
189 public function testGetWithoutLoaded($data)
191 $configCache = new ConfigCache(['test' => ['it' => 'now']]);
192 $configAdapter = \Mockery::mock(IConfigAdapter::class);
193 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(4);
194 // constructor loading
195 $configAdapter->shouldReceive('load')->andReturn([])->once();
197 $configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(false)->once();
198 $configAdapter->shouldReceive('get')->with('test', 'it')->andReturn(null)->once();
200 $configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(false)->once();
201 $configAdapter->shouldReceive('get')->with('test', 'it')->andReturn($data)->once();
203 $configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(true)->once();
205 $configuration = new Configuration($configCache, $configAdapter);
207 // first run is not loaded and no data is found in the DB
208 $this->assertEquals('now', $configuration->get('test', 'it'));
209 $this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
211 // second run is not loaded, but now data is found in the db (overwrote cache)
212 $this->assertEquals($data, $configuration->get('test', 'it'));
213 $this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
215 // third run is loaded and therefore cache is used
216 $this->assertEquals($data, $configuration->get('test', 'it'));
217 $this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
221 * Test the configuration delete() method without adapter
222 * @dataProvider dataTests
224 public function testDeleteWithoutDB($data)
226 $configCache = new ConfigCache(['test' => ['it' => $data]]);
227 $configAdapter = \Mockery::mock(IConfigAdapter::class);
228 $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(4);
230 $configuration = new Configuration($configCache, $configAdapter);
232 $this->assertEquals($data, $configuration->get('test', 'it'));
233 $this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
235 $this->assertTrue($configuration->delete('test', 'it'));
236 $this->assertNull($configuration->get('test', 'it'));
237 $this->assertNull($configuration->getCache()->get('test', 'it'));
239 $this->assertEmpty($configuration->getCache()->getAll());
243 * Test the configuration delete() method with adapter
245 public function testDeleteWithDB()
247 $configCache = new ConfigCache(['test' => ['it' => 'now', 'quarter' => 'true']]);
248 $configAdapter = \Mockery::mock(IConfigAdapter::class);
249 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(6);
250 // constructor loading
251 $configAdapter->shouldReceive('load')->andReturn([])->once();
252 $configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(true)->once();
254 $configAdapter->shouldReceive('delete')->with('test', 'it')->andReturn(false)->once();
256 $configAdapter->shouldReceive('delete')->with('test', 'second')->andReturn(true)->once();
257 $configAdapter->shouldReceive('delete')->with('test', 'third')->andReturn(false)->once();
258 $configAdapter->shouldReceive('delete')->with('test', 'quarter')->andReturn(true)->once();
260 $configuration = new Configuration($configCache, $configAdapter);
262 $this->assertEquals('now', $configuration->get('test', 'it'));
263 $this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
265 // delete from cache only
266 $this->assertTrue($configuration->delete('test', 'it'));
267 // delete from db only
268 $this->assertTrue($configuration->delete('test', 'second'));
270 $this->assertFalse($configuration->delete('test', 'third'));
272 $this->assertTrue($configuration->delete('test', 'quarter'));
274 $this->assertEmpty($configuration->getCache()->getAll());