3 namespace Friendica\Test\src\Core\Config;
5 use Friendica\Core\Config\Adapter\IPConfigAdapter;
6 use Friendica\Core\Config\Cache\PConfigCache;
7 use Friendica\Core\Config\PConfiguration;
8 use Friendica\Test\MockedTest;
10 class PConfigurationTest 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 load() method
29 public function testCacheLoad()
32 $configCache = new PConfigCache();
33 $configAdapter = \Mockery::mock(IPConfigAdapter::class);
34 $configAdapter->shouldReceive('isConnected')->andReturn(true)->twice();
36 $configAdapter->shouldReceive('load')
37 ->with($uid, 'testing')
38 ->andReturn(['testing' => ['test' => 'it']])
40 $configAdapter->shouldReceive('isLoaded')->with($uid, 'testing', 'test')->andReturn(true)->once();
42 $configuration = new PConfiguration($configCache, $configAdapter);
43 $configuration->load($uid, 'testing');
45 $this->assertEquals('it', $configuration->get($uid, 'testing', 'test'));
49 * Test the configuration load() method with overwrite
51 public function testCacheLoadDouble()
54 $configCache = new PConfigCache();
55 $configAdapter = \Mockery::mock(IPConfigAdapter::class);
56 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(4);
58 $configAdapter->shouldReceive('load')->with($uid, 'testing')->andReturn(['testing' => ['test' => 'it']])->once();
59 $configAdapter->shouldReceive('isLoaded')->with($uid, 'testing', 'test')->andReturn(true)->twice();
60 // expected next loading
61 $configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'again']])->once();
63 $configuration = new PConfiguration($configCache, $configAdapter);
64 $configuration->load($uid, 'testing');
66 $this->assertEquals('it', $configuration->get($uid, 'testing', 'test'));
68 $configuration->load($uid, 'testing');
70 $this->assertEquals('again', $configuration->get($uid, 'testing', 'test'));
74 * Test the configuration get() and set() methods without adapter
75 * @dataProvider dataTests
77 public function testSetGetWithoutDB($data)
80 $configCache = new PConfigCache();
81 $configAdapter = \Mockery::mock(IPConfigAdapter::class);
82 $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(2);
84 $configuration = new PConfiguration($configCache, $configAdapter);
86 $this->assertTrue($configuration->set($uid, 'test', 'it', $data));
88 $this->assertEquals($data, $configuration->get($uid, 'test', 'it'));
92 * Test the configuration get() and set() methods with adapter
93 * @dataProvider dataTests
95 public function testSetGetWithDB($data)
98 $configCache = new PConfigCache();
99 $configAdapter = \Mockery::mock(IPConfigAdapter::class);
100 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(2);
101 $configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(true)->once();
102 $configAdapter->shouldReceive('set')->with($uid, 'test', 'it', $data)->andReturn(true)->once();
104 $configuration = new PConfiguration($configCache, $configAdapter);
106 $this->assertTrue($configuration->set($uid, 'test', 'it', $data));
108 $this->assertEquals($data, $configuration->get($uid, 'test', 'it'));
112 * Test the configuration get() method with wrong value and no db
114 public function testGetWrongWithoutDB()
117 $configCache = new PConfigCache();
118 $configAdapter = \Mockery::mock(IPConfigAdapter::class);
119 $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(3);
121 $configuration = new PConfiguration($configCache, $configAdapter);
124 $this->assertNull($configuration->get($uid, 'test', 'it'));
126 // with default value
127 $this->assertEquals('default', $configuration->get($uid, 'test', 'it', 'default'));
129 // with default value and refresh
130 $this->assertEquals('default', $configuration->get($uid, 'test', 'it', 'default', true));
134 * Test the configuration get() method with refresh
135 * @dataProvider dataTests
137 public function testGetWithRefresh($data)
140 $configCache = new PConfigCache();
141 $configAdapter = \Mockery::mock(IPConfigAdapter::class);
142 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(4);
143 $configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(false)->once();
144 $configAdapter->shouldReceive('get')->with($uid, 'test', 'it')->andReturn('now')->once();
145 $configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(true)->twice();
146 $configAdapter->shouldReceive('get')->with($uid, 'test', 'it')->andReturn($data)->once();
147 $configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'not')->andReturn(false)->once();
148 $configAdapter->shouldReceive('get')->with($uid, 'test', 'not')->andReturn(null)->once();
150 $configuration = new PConfiguration($configCache, $configAdapter);
153 $this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
154 // use the cache again
155 $this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
157 // with refresh (and load the second value out of the db)
158 $this->assertEquals($data, $configuration->get($uid, 'test', 'it', null, true));
160 // without refresh and wrong value and default
161 $this->assertEquals('default', $configuration->get($uid, 'test', 'not', 'default'));
165 * Test the configuration get() method with different isLoaded settings
166 * @dataProvider dataTests
168 public function testGetWithoutLoaded($data)
171 $configCache = new PConfigCache();
172 $configAdapter = \Mockery::mock(IPConfigAdapter::class);
173 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(3);
175 $configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(false)->once();
176 $configAdapter->shouldReceive('get')->with($uid, 'test', 'it')->andReturn(null)->once();
178 $configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(false)->once();
179 $configAdapter->shouldReceive('get')->with($uid, 'test', 'it')->andReturn($data)->once();
181 $configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(true)->once();
183 $configuration = new PConfiguration($configCache, $configAdapter);
185 // first run is not loaded and no data is found in the DB
186 $this->assertNull($configuration->get($uid, 'test', 'it'));
188 // second run is not loaded, but now data is found in the db (overwrote cache)
189 $this->assertEquals($data, $configuration->get($uid,'test', 'it'));
191 // third run is loaded and therefore cache is used
192 $this->assertEquals($data, $configuration->get($uid,'test', 'it'));
196 * Test the configuration delete() method without adapter
197 * @dataProvider dataTests
199 public function testDeleteWithoutDB($data)
202 $configCache = new PConfigCache();
203 $configAdapter = \Mockery::mock(IPConfigAdapter::class);
204 $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(4);
206 $configuration = new PConfiguration($configCache, $configAdapter);
208 $this->assertTrue($configuration->set($uid, 'test', 'it', $data));
209 $this->assertEquals($data, $configuration->get($uid, 'test', 'it'));
211 $this->assertTrue($configuration->delete($uid, 'test', 'it'));
212 $this->assertNull($configuration->get($uid, 'test', 'it'));
216 * Test the configuration delete() method with adapter
218 public function testDeleteWithDB()
221 $configCache = new PConfigCache();
222 $configAdapter = \Mockery::mock(IPConfigAdapter::class);
223 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(6);
224 $configAdapter->shouldReceive('set')->with($uid, 'test', 'it', 'now')->andReturn(false)->once();
225 $configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(true)->once();
227 $configAdapter->shouldReceive('delete')->with($uid, 'test', 'it')->andReturn(false)->once();
229 $configAdapter->shouldReceive('delete')->with($uid, 'test', 'second')->andReturn(true)->once();
230 $configAdapter->shouldReceive('delete')->with($uid, 'test', 'third')->andReturn(false)->once();
231 $configAdapter->shouldReceive('delete')->with($uid, 'test', 'quarter')->andReturn(true)->once();
233 $configuration = new PConfiguration($configCache, $configAdapter);
235 $this->assertFalse($configuration->set($uid, 'test', 'it', 'now'));
236 $this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
239 $this->assertTrue($configuration->delete($uid, 'test', 'it'));
240 // delete from db only
241 $this->assertTrue($configuration->delete($uid, 'test', 'second'));
243 $this->assertFalse($configuration->delete($uid, 'test', 'third'));
245 $this->assertTrue($configuration->delete($uid, 'test', 'quarter'));