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
12 * Test if the configuration load() method
14 public function testCacheLoad()
17 $configCache = new ConfigCache();
18 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
19 $configAdapter->shouldReceive('isConnected')->andReturn(true)->once();
21 $configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'it']])->once();
23 $configuration = new PConfiguration($configCache, $configAdapter);
24 $configuration->load($uid, 'testing');
26 $this->assertEquals('it', $configuration->get($uid, 'testing', 'test'));
30 * Test if the configuration load() method with overwrite
32 public function testCacheLoadDouble()
35 $configCache = new ConfigCache();
36 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
37 $configAdapter->shouldReceive('isConnected')->andReturn(true)->twice();
39 $configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'it']])->once();
40 // expected next loading
41 $configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'again']])->once();
43 $configuration = new PConfiguration($configCache, $configAdapter);
44 $configuration->load($uid, 'testing');
46 $this->assertEquals('it', $configuration->get($uid, 'testing', 'test'));
48 $configuration->load($uid, 'testing');
50 $this->assertEquals('again', $configuration->get($uid, 'testing', 'test'));
54 * Test if the configuration get() and set() methods without adapter
56 public function testSetGetWithoutDB()
59 $configCache = new ConfigCache();
60 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
61 $configAdapter->shouldReceive('isConnected')->andReturn(false)->once();
63 $configuration = new PConfiguration($configCache, $configAdapter);
65 $this->assertTrue($configuration->set($uid, 'test', 'it', 'now'));
67 $this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
71 * Test if the configuration get() and set() methods with adapter
73 public function testSetGetWithDB()
76 $configCache = new ConfigCache();
77 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
78 $configAdapter->shouldReceive('isConnected')->andReturn(true)->once();
79 $configAdapter->shouldReceive('set')->with($uid, 'test', 'it', 'now')->andReturn(true)->once();
81 $configuration = new PConfiguration($configCache, $configAdapter);
83 $this->assertTrue($configuration->set($uid, 'test', 'it', 'now'));
85 $this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
89 * Test the configuration get() method with wrong value and no db
91 public function testGetWrongWithoutDB()
94 $configCache = new ConfigCache();
95 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
96 $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(3);
98 $configuration = new PConfiguration($configCache, $configAdapter);
101 $this->assertNull($configuration->get($uid, 'test', 'it'));
103 // with default value
104 $this->assertEquals('default', $configuration->get($uid, 'test', 'it', 'default'));
106 // with default value and refresh
107 $this->assertEquals('default', $configuration->get($uid, 'test', 'it', 'default', true));
111 * Test the configuration get() method with refresh
113 public function testGetWithRefresh()
116 $configCache = new ConfigCache();
117 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
118 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(3);
119 $configAdapter->shouldReceive('get')->with($uid, 'test', 'it')->andReturn('now')->once();
120 $configAdapter->shouldReceive('get')->with($uid, 'test', 'it')->andReturn('again')->once();
121 $configAdapter->shouldReceive('get')->with($uid, 'test', 'not')->andReturn('!<unset>!')->once();
123 $configuration = new PConfiguration($configCache, $configAdapter);
126 $this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
127 // use the cache again
128 $this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
130 // with refresh (and load the second value out of the db)
131 $this->assertEquals('again', $configuration->get($uid, 'test', 'it', null, true));
133 // without refresh and wrong value and default
134 $this->assertEquals('default', $configuration->get($uid, 'test', 'not', 'default'));
138 * Test the configuration delete() method without adapter
140 public function testDeleteWithoutDB()
143 $configCache = new ConfigCache();
144 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
145 $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(3);
147 $configuration = new PConfiguration($configCache, $configAdapter);
149 $this->assertTrue($configuration->set($uid, 'test', 'it', 'now'));
150 $this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
152 $this->assertTrue($configuration->delete($uid, 'test', 'it'));
153 $this->assertNull($configuration->get($uid, 'test', 'it'));
157 * Test the configuration delete() method with adapter
159 public function testDeleteWithDB()
162 $configCache = new ConfigCache();
163 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
164 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(5);
165 $configAdapter->shouldReceive('set')->with($uid, 'test', 'it', 'now')->andReturn(false)->once();
166 $configAdapter->shouldReceive('delete')->with($uid, 'test', 'it')->andReturn(false)->once();
168 $configAdapter->shouldReceive('delete')->with($uid, 'test', 'second')->andReturn(true)->once();
169 $configAdapter->shouldReceive('delete')->with($uid, 'test', 'third')->andReturn(false)->once();
170 $configAdapter->shouldReceive('delete')->with($uid, 'test', 'quarter')->andReturn(true)->once();
172 $configuration = new PConfiguration($configCache, $configAdapter);
174 $this->assertFalse($configuration->set($uid, 'test', 'it', 'now'));
175 $this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
178 $this->assertTrue($configuration->delete($uid, 'test', 'it'));
179 // delete from db only
180 $this->assertTrue($configuration->delete($uid, 'test', 'second'));
182 $this->assertFalse($configuration->delete($uid, 'test', 'third'));
184 $this->assertTrue($configuration->delete($uid, 'test', 'quarter'));