]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Config/PConfigurationTest.php
8ecc9bdf379f092ff38ba0a3f87103ae71cf3bf9
[friendica.git] / tests / src / Core / Config / PConfigurationTest.php
1 <?php
2
3 namespace Friendica\Test\Core\Config;
4
5 use Friendica\Core\Config\Cache\ConfigCache;
6 use Friendica\Core\Config\PConfiguration;
7 use Friendica\Test\MockedTest;
8
9 class PConfigurationTest extends MockedTest
10 {
11         public function dataTests()
12         {
13                 return [
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],
22                 ];
23         }
24
25         /**
26          * Test the configuration load() method
27          */
28         public function testCacheLoad()
29         {
30                 $uid = 234;
31                 $configCache = new ConfigCache();
32                 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
33                 $configAdapter->shouldReceive('isConnected')->andReturn(true)->twice();
34                 // expected loading
35                 $configAdapter->shouldReceive('load')
36                         ->with($uid, 'testing')
37                         ->andReturn(['testing' => ['test' => 'it']])
38                         ->once();
39                 $configAdapter->shouldReceive('isLoaded')->with($uid, 'testing', 'test')->andReturn(true)->once();
40
41                 $configuration = new PConfiguration($configCache, $configAdapter);
42                 $configuration->load($uid, 'testing');
43
44                 $this->assertEquals('it', $configuration->get($uid, 'testing', 'test'));
45         }
46
47         /**
48          * Test the configuration load() method with overwrite
49          */
50         public function testCacheLoadDouble()
51         {
52                 $uid = 234;
53                 $configCache = new ConfigCache();
54                 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
55                 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(4);
56                 // expected loading
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();
61
62                 $configuration = new PConfiguration($configCache, $configAdapter);
63                 $configuration->load($uid, 'testing');
64
65                 $this->assertEquals('it', $configuration->get($uid, 'testing', 'test'));
66
67                 $configuration->load($uid, 'testing');
68
69                 $this->assertEquals('again', $configuration->get($uid, 'testing', 'test'));
70         }
71
72         /**
73          * Test the configuration get() and set() methods without adapter
74          * @dataProvider dataTests
75          */
76         public function testSetGetWithoutDB($data)
77         {
78                 $uid = 234;
79                 $configCache = new ConfigCache();
80                 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
81                 $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(2);
82
83                 $configuration = new PConfiguration($configCache, $configAdapter);
84
85                 $this->assertTrue($configuration->set($uid, 'test', 'it', $data));
86
87                 $this->assertEquals($data, $configuration->get($uid, 'test', 'it'));
88         }
89
90         /**
91          * Test the configuration get() and set() methods with adapter
92          * @dataProvider dataTests
93          */
94         public function testSetGetWithDB($data)
95         {
96                 $uid = 234;
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();
102
103                 $configuration = new PConfiguration($configCache, $configAdapter);
104
105                 $this->assertTrue($configuration->set($uid, 'test', 'it', $data));
106
107                 $this->assertEquals($data, $configuration->get($uid, 'test', 'it'));
108         }
109
110         /**
111          * Test the configuration get() method with wrong value and no db
112          */
113         public function testGetWrongWithoutDB()
114         {
115                 $uid = 234;
116                 $configCache = new ConfigCache();
117                 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
118                 $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(3);
119
120                 $configuration = new PConfiguration($configCache, $configAdapter);
121
122                 // without refresh
123                 $this->assertNull($configuration->get($uid, 'test', 'it'));
124
125                 // with default value
126                 $this->assertEquals('default', $configuration->get($uid, 'test', 'it', 'default'));
127
128                 // with default value and refresh
129                 $this->assertEquals('default', $configuration->get($uid, 'test', 'it', 'default', true));
130         }
131
132         /**
133          * Test the configuration get() method with refresh
134          * @dataProvider dataTests
135          */
136         public function testGetWithRefresh($data)
137         {
138                 $uid = 234;
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();
148
149                 $configuration = new PConfiguration($configCache, $configAdapter);
150
151                 // without refresh
152                 $this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
153                 // use the cache again
154                 $this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
155
156                 // with refresh (and load the second value out of the db)
157                 $this->assertEquals($data, $configuration->get($uid, 'test', 'it', null, true));
158
159                 // without refresh and wrong value and default
160                 $this->assertEquals('default', $configuration->get($uid, 'test', 'not', 'default'));
161         }
162
163         /**
164          * Test the configuration get() method with different isLoaded settings
165          * @dataProvider dataTests
166          */
167         public function testGetWithoutLoaded($data)
168         {
169                 $uid = 234;
170                 $configCache = new ConfigCache();
171                 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
172                 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(3);
173
174                 $configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(false)->once();
175                 $configAdapter->shouldReceive('get')->with($uid, 'test', 'it')->andReturn('!<unset>!')->once();
176
177                 $configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(false)->once();
178                 $configAdapter->shouldReceive('get')->with($uid, 'test', 'it')->andReturn($data)->once();
179
180                 $configAdapter->shouldReceive('isLoaded')->with($uid, 'test', 'it')->andReturn(true)->once();
181
182                 $configuration = new PConfiguration($configCache, $configAdapter);
183
184                 // first run is not loaded and no data is found in the DB
185                 $this->assertNull($configuration->get($uid, 'test', 'it'));
186
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'));
189
190                 // third run is loaded and therefore cache is used
191                 $this->assertEquals($data, $configuration->get($uid,'test', 'it'));
192         }
193
194         /**
195          * Test the configuration delete() method without adapter
196          * @dataProvider dataTests
197          */
198         public function testDeleteWithoutDB($data)
199         {
200                 $uid = 234;
201                 $configCache = new ConfigCache();
202                 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
203                 $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(4);
204
205                 $configuration = new PConfiguration($configCache, $configAdapter);
206
207                 $this->assertTrue($configuration->set($uid, 'test', 'it', $data));
208                 $this->assertEquals($data, $configuration->get($uid, 'test', 'it'));
209
210                 $this->assertTrue($configuration->delete($uid, 'test', 'it'));
211                 $this->assertNull($configuration->get($uid, 'test', 'it'));
212         }
213
214         /**
215          * Test the configuration delete() method with adapter
216          */
217         public function testDeleteWithDB()
218         {
219                 $uid = 234;
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();
225
226                 $configAdapter->shouldReceive('delete')->with($uid, 'test', 'it')->andReturn(false)->once();
227
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();
231
232                 $configuration = new PConfiguration($configCache, $configAdapter);
233
234                 $this->assertFalse($configuration->set($uid, 'test', 'it', 'now'));
235                 $this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
236
237                 // delete from set
238                 $this->assertTrue($configuration->delete($uid, 'test', 'it'));
239                 // delete from db only
240                 $this->assertTrue($configuration->delete($uid, 'test', 'second'));
241                 // no delete
242                 $this->assertFalse($configuration->delete($uid, 'test', 'third'));
243                 // delete both
244                 $this->assertTrue($configuration->delete($uid, 'test', 'quarter'));
245         }
246 }