3 namespace Friendica\Test\Core\Config;
5 use Friendica\Core\Config\Cache\ConfigCache;
6 use Friendica\Core\Config\PConfiguration;
7 use Friendica\Test\MockedTest;
9 class PConfigurationTest extends MockedTest
11 public function dataTests()
14 'string' => ['data' => 'it'],
15 'boolTrue' => ['data' => true],
16 'boolFalse' => ['data' => false],
17 'integer' => ['data' => 235],
18 'decimal' => ['data' => 2.456],
19 'array' => ['data' => ['1', 2, '3', true, false]],
20 'boolIntTrue' => ['data' => 1],
21 'boolIntFalse' => ['Data' => 0],
26 * Test the configuration load() method
28 public function testCacheLoad()
31 $configCache = new ConfigCache();
32 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
33 $configAdapter->shouldReceive('isConnected')->andReturn(true)->twice();
35 $configAdapter->shouldReceive('load')
36 ->with($uid, 'testing')
37 ->andReturn(['testing' => ['test' => 'it']])
39 $configAdapter->shouldReceive('isLoaded')->with($uid, 'testing', 'test')->andReturn(true)->once();
41 $configuration = new PConfiguration($configCache, $configAdapter);
42 $configuration->load($uid, 'testing');
44 $this->assertEquals('it', $configuration->get($uid, 'testing', 'test'));
48 * Test the configuration load() method with overwrite
50 public function testCacheLoadDouble()
53 $configCache = new ConfigCache();
54 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
55 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(4);
57 $configAdapter->shouldReceive('load')->with($uid, 'testing')->andReturn(['testing' => ['test' => 'it']])->once();
58 $configAdapter->shouldReceive('isLoaded')->with($uid, 'testing', 'test')->andReturn(true)->twice();
59 // expected next loading
60 $configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'again']])->once();
62 $configuration = new PConfiguration($configCache, $configAdapter);
63 $configuration->load($uid, 'testing');
65 $this->assertEquals('it', $configuration->get($uid, 'testing', 'test'));
67 $configuration->load($uid, 'testing');
69 $this->assertEquals('again', $configuration->get($uid, 'testing', 'test'));
73 * Test the configuration get() and set() methods without adapter
74 * @dataProvider dataTests
76 public function testSetGetWithoutDB($data)
79 $configCache = new ConfigCache();
80 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
81 $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(2);
83 $configuration = new PConfiguration($configCache, $configAdapter);
85 $this->assertTrue($configuration->set($uid, 'test', 'it', $data));
87 $this->assertEquals($data, $configuration->get($uid, 'test', 'it'));
91 * Test the configuration get() and set() methods with adapter
92 * @dataProvider dataTests
94 public function testSetGetWithDB($data)
97 $configCache = new ConfigCache();
98 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
99 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(2);
100 $configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(true)->once();
101 $configAdapter->shouldReceive('set')->with($uid, 'test', 'it', $data)->andReturn(true)->once();
103 $configuration = new PConfiguration($configCache, $configAdapter);
105 $this->assertTrue($configuration->set($uid, 'test', 'it', $data));
107 $this->assertEquals($data, $configuration->get($uid, 'test', 'it'));
111 * Test the configuration get() method with wrong value and no db
113 public function testGetWrongWithoutDB()
116 $configCache = new ConfigCache();
117 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
118 $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(3);
120 $configuration = new PConfiguration($configCache, $configAdapter);
123 $this->assertNull($configuration->get($uid, 'test', 'it'));
125 // with default value
126 $this->assertEquals('default', $configuration->get($uid, 'test', 'it', 'default'));
128 // with default value and refresh
129 $this->assertEquals('default', $configuration->get($uid, 'test', 'it', 'default', true));
133 * Test the configuration get() method with refresh
134 * @dataProvider dataTests
136 public function testGetWithRefresh($data)
139 $configCache = new ConfigCache();
140 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
141 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(4);
142 $configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(false)->once();
143 $configAdapter->shouldReceive('get')->with($uid, 'test', 'it')->andReturn('now')->once();
144 $configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(true)->twice();
145 $configAdapter->shouldReceive('get')->with($uid, 'test', 'it')->andReturn($data)->once();
146 $configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'not')->andReturn(false)->once();
147 $configAdapter->shouldReceive('get')->with($uid, 'test', 'not')->andReturn('!<unset>!')->once();
149 $configuration = new PConfiguration($configCache, $configAdapter);
152 $this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
153 // use the cache again
154 $this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
156 // with refresh (and load the second value out of the db)
157 $this->assertEquals($data, $configuration->get($uid, 'test', 'it', null, true));
159 // without refresh and wrong value and default
160 $this->assertEquals('default', $configuration->get($uid, 'test', 'not', 'default'));
164 * Test the configuration get() method with different isLoaded settings
165 * @dataProvider dataTests
167 public function testGetWithoutLoaded($data)
170 $configCache = new ConfigCache();
171 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
172 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(3);
174 $configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(false)->once();
175 $configAdapter->shouldReceive('get')->with($uid, 'test', 'it')->andReturn('!<unset>!')->once();
177 $configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(false)->once();
178 $configAdapter->shouldReceive('get')->with($uid, 'test', 'it')->andReturn($data)->once();
180 $configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(true)->once();
182 $configuration = new PConfiguration($configCache, $configAdapter);
184 // first run is not loaded and no data is found in the DB
185 $this->assertNull($configuration->get($uid, 'test', 'it'));
187 // second run is not loaded, but now data is found in the db (overwrote cache)
188 $this->assertEquals($data, $configuration->get($uid,'test', 'it'));
190 // third run is loaded and therefore cache is used
191 $this->assertEquals($data, $configuration->get($uid,'test', 'it'));
195 * Test the configuration delete() method without adapter
196 * @dataProvider dataTests
198 public function testDeleteWithoutDB($data)
201 $configCache = new ConfigCache();
202 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
203 $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(4);
205 $configuration = new PConfiguration($configCache, $configAdapter);
207 $this->assertTrue($configuration->set($uid, 'test', 'it', $data));
208 $this->assertEquals($data, $configuration->get($uid, 'test', 'it'));
210 $this->assertTrue($configuration->delete($uid, 'test', 'it'));
211 $this->assertNull($configuration->get($uid, 'test', 'it'));
215 * Test the configuration delete() method with adapter
217 public function testDeleteWithDB()
220 $configCache = new ConfigCache();
221 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
222 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(6);
223 $configAdapter->shouldReceive('set')->with($uid, 'test', 'it', 'now')->andReturn(false)->once();
224 $configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(true)->once();
226 $configAdapter->shouldReceive('delete')->with($uid, 'test', 'it')->andReturn(false)->once();
228 $configAdapter->shouldReceive('delete')->with($uid, 'test', 'second')->andReturn(true)->once();
229 $configAdapter->shouldReceive('delete')->with($uid, 'test', 'third')->andReturn(false)->once();
230 $configAdapter->shouldReceive('delete')->with($uid, 'test', 'quarter')->andReturn(true)->once();
232 $configuration = new PConfiguration($configCache, $configAdapter);
234 $this->assertFalse($configuration->set($uid, 'test', 'it', 'now'));
235 $this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
238 $this->assertTrue($configuration->delete($uid, 'test', 'it'));
239 // delete from db only
240 $this->assertTrue($configuration->delete($uid, 'test', 'second'));
242 $this->assertFalse($configuration->delete($uid, 'test', 'third'));
244 $this->assertTrue($configuration->delete($uid, 'test', 'quarter'));