3 namespace Friendica\Test\Core\Config;
5 use Friendica\Core\Config\Cache\ConfigCache;
6 use Friendica\Core\Config\Cache\IConfigCache;
7 use Friendica\Core\Config\Configuration;
8 use Friendica\Test\MockedTest;
10 class ConfigurationTest extends MockedTest
13 * Test the configuration initialization
15 public function testSetUp()
17 $configCache = new ConfigCache();
18 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
19 $configAdapter->shouldReceive('isConnected')->andReturn(false)->once();
21 $configuration = new Configuration($configCache, $configAdapter);
23 $this->assertInstanceOf(IConfigCache::class, $configuration->getCache());
27 * Test if the configuration load() method
29 public function testCacheLoad()
31 $configCache = new ConfigCache();
32 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
33 $configAdapter->shouldReceive('isConnected')->andReturn(true)->twice();
34 // constructor loading
35 $configAdapter->shouldReceive('load')->andReturn([])->once();
37 $configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'it']])->once();
39 $configuration = new Configuration($configCache, $configAdapter);
40 $configuration->load('testing');
42 $this->assertEquals('it', $configuration->get('testing', 'test'));
43 $this->assertEquals('it', $configuration->getCache()->get('testing', 'test'));
47 * Test if the configuration load() method with overwrite
49 public function testCacheLoadDouble()
51 $configCache = new ConfigCache();
52 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
53 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(3);
54 // constructor loading
55 $configAdapter->shouldReceive('load')->andReturn([])->once();
57 $configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'it']])->once();
58 // expected next loading
59 $configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'again']])->once();
61 $configuration = new Configuration($configCache, $configAdapter);
62 $configuration->load('testing');
64 $this->assertEquals('it', $configuration->get('testing', 'test'));
65 $this->assertEquals('it', $configuration->getCache()->get('testing', 'test'));
67 $configuration->load('testing');
69 $this->assertEquals('again', $configuration->get('testing', 'test'));
70 $this->assertEquals('again', $configuration->getCache()->get('testing', 'test'));
74 * Test if the configuration get() and set() methods without adapter
76 public function testSetGetWithoutDB()
78 $configCache = new ConfigCache();
79 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
80 $configAdapter->shouldReceive('isConnected')->andReturn(false)->twice();
82 $configuration = new Configuration($configCache, $configAdapter);
84 $this->assertTrue($configuration->set('test', 'it', 'now'));
86 $this->assertEquals('now', $configuration->get('test', 'it'));
87 $this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
91 * Test if the configuration get() and set() methods with adapter
93 public function testSetGetWithDB()
95 $configCache = new ConfigCache();
96 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
97 $configAdapter->shouldReceive('isConnected')->andReturn(true)->twice();
98 // constructor loading
99 $configAdapter->shouldReceive('load')->andReturn([])->once();
100 $configAdapter->shouldReceive('set')->with('test', 'it', 'now')->andReturn(true)->once();
102 $configuration = new Configuration($configCache, $configAdapter);
104 $this->assertTrue($configuration->set('test', 'it', 'now'));
106 $this->assertEquals('now', $configuration->get('test', 'it'));
107 $this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
111 * Test the configuration get() method with wrong value and no db
113 public function testGetWrongWithoutDB()
115 $configCache = new ConfigCache();
116 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
117 $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(4);
119 $configuration = new Configuration($configCache, $configAdapter);
122 $this->assertNull($configuration->get('test', 'it'));
124 /// beware that the cache returns '!<unset>!' and not null for a non existing value
125 $this->assertEquals('!<unset>!', $configuration->getCache()->get('test', 'it'));
127 // with default value
128 $this->assertEquals('default', $configuration->get('test', 'it', 'default'));
130 // with default value and refresh
131 $this->assertEquals('default', $configuration->get('test', 'it', 'default', true));
135 * Test the configuration get() method with refresh
137 public function testGetWithRefresh()
139 $configCache = new ConfigCache(['test' => ['it' => 'now']]);
140 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
141 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(3);
142 // constructor loading
143 $configAdapter->shouldReceive('load')->andReturn([])->once();
144 $configAdapter->shouldReceive('get')->with('test', 'it')->andReturn('again')->once();
145 $configAdapter->shouldReceive('get')->with('test', 'not')->andReturn('!<unset>!')->once();
147 $configuration = new Configuration($configCache, $configAdapter);
150 $this->assertEquals('now', $configuration->get('test', 'it'));
151 $this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
154 $this->assertEquals('again', $configuration->get('test', 'it', null, true));
155 $this->assertEquals('again', $configuration->getCache()->get('test', 'it'));
157 // without refresh and wrong value and default
158 $this->assertEquals('default', $configuration->get('test', 'not', 'default'));
159 $this->assertEquals('!<unset>!', $configuration->getCache()->get('test', 'not'));
163 * Test the configuration delete() method without adapter
165 public function testDeleteWithoutDB()
167 $configCache = new ConfigCache(['test' => ['it' => 'now']]);
168 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
169 $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(3);
171 $configuration = new Configuration($configCache, $configAdapter);
173 $this->assertEquals('now', $configuration->get('test', 'it'));
174 $this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
176 $this->assertTrue($configuration->delete('test', 'it'));
177 $this->assertNull($configuration->get('test', 'it'));
178 $this->assertEquals('!<unset>!', $configuration->getCache()->get('test', 'it'));
180 $this->assertEmpty($configuration->getCache()->getAll());
184 * Test the configuration delete() method with adapter
186 public function testDeleteWithDB()
188 $configCache = new ConfigCache(['test' => ['it' => 'now', 'quarter' => 'true']]);
189 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
190 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(5);
191 // constructor loading
192 $configAdapter->shouldReceive('load')->andReturn([])->once();
193 $configAdapter->shouldReceive('delete')->with('test', 'it')->andReturn(false)->once();
195 $configAdapter->shouldReceive('delete')->with('test', 'second')->andReturn(true)->once();
196 $configAdapter->shouldReceive('delete')->with('test', 'third')->andReturn(false)->once();
197 $configAdapter->shouldReceive('delete')->with('test', 'quarter')->andReturn(true)->once();
199 $configuration = new Configuration($configCache, $configAdapter);
201 $this->assertEquals('now', $configuration->get('test', 'it'));
202 $this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
204 // delete from cache only
205 $this->assertTrue($configuration->delete('test', 'it'));
206 // delete from db only
207 $this->assertTrue($configuration->delete('test', 'second'));
209 $this->assertFalse($configuration->delete('test', 'third'));
211 $this->assertTrue($configuration->delete('test', 'quarter'));
213 $this->assertEmpty($configuration->getCache()->getAll());