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\Configuration;
8 use Friendica\Test\MockedTest;
10 class ConfigurationTest extends MockedTest
12 public function dataTests()
15 'string' => ['data' => 'it'],
16 'boolTrue' => ['data' => true],
17 'boolFalse' => ['data' => false],
18 'integer' => ['data' => 235],
19 'decimal' => ['data' => 2.456],
20 'array' => ['data' => ['1', 2, '3', true, false]],
21 'boolIntTrue' => ['data' => 1],
22 'boolIntFalse' => ['Data' => 0],
27 * Test the configuration initialization
29 public function testSetUp()
31 $configCache = new ConfigCache();
32 $configAdapter = \Mockery::mock(IConfigAdapter::class);
33 $configAdapter->shouldReceive('isConnected')->andReturn(false)->once();
35 $configuration = new Configuration($configCache, $configAdapter);
37 $this->assertInstanceOf(ConfigCache::class, $configuration->getCache());
41 * Test the configuration load() method
43 public function testCacheLoad()
45 $configCache = new ConfigCache();
46 $configAdapter = \Mockery::mock(IConfigAdapter::class);
47 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(3);
48 // constructor loading
49 $configAdapter->shouldReceive('load')->andReturn([])->once();
51 $configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'it']])->once();
52 $configAdapter->shouldReceive('isLoaded')->with('testing', 'test')->andReturn(true)->once();
54 $configuration = new Configuration($configCache, $configAdapter);
55 $configuration->load('testing');
57 $this->assertEquals('it', $configuration->get('testing', 'test'));
58 $this->assertEquals('it', $configuration->getCache()->get('testing', 'test'));
62 * Test the configuration load() method with overwrite
64 public function testCacheLoadDouble()
66 $configCache = new ConfigCache();
67 $configAdapter = \Mockery::mock(IConfigAdapter::class);
68 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(5);
69 // constructor loading
70 $configAdapter->shouldReceive('load')->andReturn([])->once();
72 $configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'it']])->once();
73 $configAdapter->shouldReceive('isLoaded')->with('testing', 'test')->andReturn(true)->twice();
74 // expected next loading
75 $configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'again']])->once();
77 $configuration = new Configuration($configCache, $configAdapter);
78 $configuration->load('testing');
80 $this->assertEquals('it', $configuration->get('testing', 'test'));
81 $this->assertEquals('it', $configuration->getCache()->get('testing', 'test'));
83 $configuration->load('testing');
85 $this->assertEquals('again', $configuration->get('testing', 'test'));
86 $this->assertEquals('again', $configuration->getCache()->get('testing', 'test'));
90 * Test the configuration get() and set() methods without adapter
91 * @dataProvider dataTests
93 public function testSetGetWithoutDB($data)
95 $configCache = new ConfigCache();
96 $configAdapter = \Mockery::mock(IConfigAdapter::class);
97 $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(3);
99 $configuration = new Configuration($configCache, $configAdapter);
101 $this->assertTrue($configuration->set('test', 'it', $data));
103 $this->assertEquals($data, $configuration->get('test', 'it'));
104 $this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
108 * Test the configuration get() and set() methods with adapter
109 * @dataProvider dataTests
111 public function testSetGetWithDB($data)
113 $configCache = new ConfigCache();
114 $configAdapter = \Mockery::mock(IConfigAdapter::class);
115 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(3);
116 // constructor loading
117 $configAdapter->shouldReceive('load')->andReturn([])->once();
118 $configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(true)->once();
119 $configAdapter->shouldReceive('set')->with('test', 'it', $data)->andReturn(true)->once();
121 $configuration = new Configuration($configCache, $configAdapter);
123 $this->assertTrue($configuration->set('test', 'it', $data));
125 $this->assertEquals($data, $configuration->get('test', 'it'));
126 $this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
130 * Test the configuration get() method with wrong value and no db
132 public function testGetWrongWithoutDB()
134 $configCache = new ConfigCache();
135 $configAdapter = \Mockery::mock(IConfigAdapter::class);
136 $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(4);
138 $configuration = new Configuration($configCache, $configAdapter);
141 $this->assertNull($configuration->get('test', 'it'));
143 /// beware that the cache returns '!<unset>!' and not null for a non existing value
144 $this->assertNull($configuration->getCache()->get('test', 'it'));
146 // with default value
147 $this->assertEquals('default', $configuration->get('test', 'it', 'default'));
149 // with default value and refresh
150 $this->assertEquals('default', $configuration->get('test', 'it', 'default', true));
154 * Test the configuration get() method with refresh
155 * @dataProvider dataTests
157 public function testGetWithRefresh($data)
159 $configCache = new ConfigCache(['test' => ['it' => 'now']]);
160 $configAdapter = \Mockery::mock(IConfigAdapter::class);
161 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(4);
162 // constructor loading
163 $configAdapter->shouldReceive('load')->andReturn([])->once();
164 $configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(true)->twice();
165 $configAdapter->shouldReceive('get')->with('test', 'it')->andReturn($data)->once();
166 $configAdapter->shouldReceive('isLoaded')->with('test', 'not')->andReturn(false)->once();
167 $configAdapter->shouldReceive('get')->with('test', 'not')->andReturn(null)->once();
169 $configuration = new Configuration($configCache, $configAdapter);
172 $this->assertEquals('now', $configuration->get('test', 'it'));
173 $this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
176 $this->assertEquals($data, $configuration->get('test', 'it', null, true));
177 $this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
179 // without refresh and wrong value and default
180 $this->assertEquals('default', $configuration->get('test', 'not', 'default'));
181 $this->assertNull($configuration->getCache()->get('test', 'not'));
185 * Test the configuration get() method with different isLoaded settings
186 * @dataProvider dataTests
188 public function testGetWithoutLoaded($data)
190 $configCache = new ConfigCache(['test' => ['it' => 'now']]);
191 $configAdapter = \Mockery::mock(IConfigAdapter::class);
192 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(4);
193 // constructor loading
194 $configAdapter->shouldReceive('load')->andReturn([])->once();
196 $configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(false)->once();
197 $configAdapter->shouldReceive('get')->with('test', 'it')->andReturn(null)->once();
199 $configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(false)->once();
200 $configAdapter->shouldReceive('get')->with('test', 'it')->andReturn($data)->once();
202 $configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(true)->once();
204 $configuration = new Configuration($configCache, $configAdapter);
206 // first run is not loaded and no data is found in the DB
207 $this->assertEquals('now', $configuration->get('test', 'it'));
208 $this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
210 // second run is not loaded, but now data is found in the db (overwrote cache)
211 $this->assertEquals($data, $configuration->get('test', 'it'));
212 $this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
214 // third run is loaded and therefore cache is used
215 $this->assertEquals($data, $configuration->get('test', 'it'));
216 $this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
220 * Test the configuration delete() method without adapter
221 * @dataProvider dataTests
223 public function testDeleteWithoutDB($data)
225 $configCache = new ConfigCache(['test' => ['it' => $data]]);
226 $configAdapter = \Mockery::mock(IConfigAdapter::class);
227 $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(4);
229 $configuration = new Configuration($configCache, $configAdapter);
231 $this->assertEquals($data, $configuration->get('test', 'it'));
232 $this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
234 $this->assertTrue($configuration->delete('test', 'it'));
235 $this->assertNull($configuration->get('test', 'it'));
236 $this->assertNull($configuration->getCache()->get('test', 'it'));
238 $this->assertEmpty($configuration->getCache()->getAll());
242 * Test the configuration delete() method with adapter
244 public function testDeleteWithDB()
246 $configCache = new ConfigCache(['test' => ['it' => 'now', 'quarter' => 'true']]);
247 $configAdapter = \Mockery::mock(IConfigAdapter::class);
248 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(6);
249 // constructor loading
250 $configAdapter->shouldReceive('load')->andReturn([])->once();
251 $configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(true)->once();
253 $configAdapter->shouldReceive('delete')->with('test', 'it')->andReturn(false)->once();
255 $configAdapter->shouldReceive('delete')->with('test', 'second')->andReturn(true)->once();
256 $configAdapter->shouldReceive('delete')->with('test', 'third')->andReturn(false)->once();
257 $configAdapter->shouldReceive('delete')->with('test', 'quarter')->andReturn(true)->once();
259 $configuration = new Configuration($configCache, $configAdapter);
261 $this->assertEquals('now', $configuration->get('test', 'it'));
262 $this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
264 // delete from cache only
265 $this->assertTrue($configuration->delete('test', 'it'));
266 // delete from db only
267 $this->assertTrue($configuration->delete('test', 'second'));
269 $this->assertFalse($configuration->delete('test', 'third'));
271 $this->assertTrue($configuration->delete('test', 'quarter'));
273 $this->assertEmpty($configuration->getCache()->getAll());