]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Config/ConfigurationTest.php
Splitting ConfigCache & PConfigCache
[friendica.git] / tests / src / Core / Config / ConfigurationTest.php
1 <?php
2
3 namespace Friendica\Test\src\Core\Config;
4
5 use Friendica\Core\Config\Adapter\IConfigAdapter;
6 use Friendica\Core\Config\Cache\ConfigCache;
7 use Friendica\Core\Config\Configuration;
8 use Friendica\Test\MockedTest;
9
10 class ConfigurationTest extends MockedTest
11 {
12         public function dataTests()
13         {
14                 return [
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],
23                 ];
24         }
25
26         /**
27          * Test the configuration initialization
28          */
29         public function testSetUp()
30         {
31                 $configCache = new ConfigCache();
32                 $configAdapter = \Mockery::mock(IConfigAdapter::class);
33                 $configAdapter->shouldReceive('isConnected')->andReturn(false)->once();
34
35                 $configuration = new Configuration($configCache, $configAdapter);
36
37                 $this->assertInstanceOf(ConfigCache::class, $configuration->getCache());
38         }
39
40         /**
41          * Test the configuration load() method
42          */
43         public function testCacheLoad()
44         {
45                 $configCache = new ConfigCache();
46                 $configAdapter = \Mockery::mock(IConfigAdapter::class);
47                 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(3);
48                 // constructor loading
49                 $configAdapter->shouldReceive('load')->andReturn([])->once();
50                 // expected loading
51                 $configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'it']])->once();
52                 $configAdapter->shouldReceive('isLoaded')->with('testing', 'test')->andReturn(true)->once();
53
54                 $configuration = new Configuration($configCache, $configAdapter);
55                 $configuration->load('testing');
56
57                 $this->assertEquals('it', $configuration->get('testing', 'test'));
58                 $this->assertEquals('it', $configuration->getCache()->get('testing', 'test'));
59         }
60
61         /**
62          * Test the configuration load() method with overwrite
63          */
64         public function testCacheLoadDouble()
65         {
66                 $configCache = new ConfigCache();
67                 $configAdapter = \Mockery::mock(IConfigAdapter::class);
68                 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(5);
69                 // constructor loading
70                 $configAdapter->shouldReceive('load')->andReturn([])->once();
71                 // expected loading
72                 $configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'it']])->once();
73                 $configAdapter->shouldReceive('isLoaded')->with('testing', 'test')->andReturn(true)->twice();
74                 // expected next loading
75                 $configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'again']])->once();
76
77                 $configuration = new Configuration($configCache, $configAdapter);
78                 $configuration->load('testing');
79
80                 $this->assertEquals('it', $configuration->get('testing', 'test'));
81                 $this->assertEquals('it', $configuration->getCache()->get('testing', 'test'));
82
83                 $configuration->load('testing');
84
85                 $this->assertEquals('again', $configuration->get('testing', 'test'));
86                 $this->assertEquals('again', $configuration->getCache()->get('testing', 'test'));
87         }
88
89         /**
90          * Test the configuration get() and set() methods without adapter
91          * @dataProvider dataTests
92          */
93         public function testSetGetWithoutDB($data)
94         {
95                 $configCache = new ConfigCache();
96                 $configAdapter = \Mockery::mock(IConfigAdapter::class);
97                 $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(3);
98
99                 $configuration = new Configuration($configCache, $configAdapter);
100
101                 $this->assertTrue($configuration->set('test', 'it', $data));
102
103                 $this->assertEquals($data, $configuration->get('test', 'it'));
104                 $this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
105         }
106
107         /**
108          * Test the configuration get() and set() methods with adapter
109          * @dataProvider dataTests
110          */
111         public function testSetGetWithDB($data)
112         {
113                 $configCache = new ConfigCache();
114                 $configAdapter = \Mockery::mock(IConfigAdapter::class);
115                 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(3);
116                 // constructor loading
117                 $configAdapter->shouldReceive('load')->andReturn([])->once();
118                 $configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(true)->once();
119                 $configAdapter->shouldReceive('set')->with('test', 'it', $data)->andReturn(true)->once();
120
121                 $configuration = new Configuration($configCache, $configAdapter);
122
123                 $this->assertTrue($configuration->set('test', 'it', $data));
124
125                 $this->assertEquals($data, $configuration->get('test', 'it'));
126                 $this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
127         }
128
129         /**
130          * Test the configuration get() method with wrong value and no db
131          */
132         public function testGetWrongWithoutDB()
133         {
134                 $configCache = new ConfigCache();
135                 $configAdapter = \Mockery::mock(IConfigAdapter::class);
136                 $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(4);
137
138                 $configuration = new Configuration($configCache, $configAdapter);
139
140                 // without refresh
141                 $this->assertNull($configuration->get('test', 'it'));
142
143                 /// beware that the cache returns '!<unset>!' and not null for a non existing value
144                 $this->assertNull($configuration->getCache()->get('test', 'it'));
145
146                 // with default value
147                 $this->assertEquals('default', $configuration->get('test', 'it', 'default'));
148
149                 // with default value and refresh
150                 $this->assertEquals('default', $configuration->get('test', 'it', 'default', true));
151         }
152
153         /**
154          * Test the configuration get() method with refresh
155          * @dataProvider dataTests
156          */
157         public function testGetWithRefresh($data)
158         {
159                 $configCache = new ConfigCache(['test' => ['it' => 'now']]);
160                 $configAdapter = \Mockery::mock(IConfigAdapter::class);
161                 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(4);
162                 // constructor loading
163                 $configAdapter->shouldReceive('load')->andReturn([])->once();
164                 $configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(true)->twice();
165                 $configAdapter->shouldReceive('get')->with('test', 'it')->andReturn($data)->once();
166                 $configAdapter->shouldReceive('isLoaded')->with('test', 'not')->andReturn(false)->once();
167                 $configAdapter->shouldReceive('get')->with('test', 'not')->andReturn(null)->once();
168
169                 $configuration = new Configuration($configCache, $configAdapter);
170
171                 // without refresh
172                 $this->assertEquals('now', $configuration->get('test', 'it'));
173                 $this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
174
175                 // with refresh
176                 $this->assertEquals($data, $configuration->get('test', 'it', null, true));
177                 $this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
178
179                 // without refresh and wrong value and default
180                 $this->assertEquals('default', $configuration->get('test', 'not', 'default'));
181                 $this->assertNull($configuration->getCache()->get('test', 'not'));
182         }
183
184         /**
185          * Test the configuration get() method with different isLoaded settings
186          * @dataProvider dataTests
187          */
188         public function testGetWithoutLoaded($data)
189         {
190                 $configCache = new ConfigCache(['test' => ['it' => 'now']]);
191                 $configAdapter = \Mockery::mock(IConfigAdapter::class);
192                 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(4);
193                 // constructor loading
194                 $configAdapter->shouldReceive('load')->andReturn([])->once();
195
196                 $configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(false)->once();
197                 $configAdapter->shouldReceive('get')->with('test', 'it')->andReturn(null)->once();
198
199                 $configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(false)->once();
200                 $configAdapter->shouldReceive('get')->with('test', 'it')->andReturn($data)->once();
201
202                 $configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(true)->once();
203
204                 $configuration = new Configuration($configCache, $configAdapter);
205
206                 // first run is not loaded and no data is found in the DB
207                 $this->assertEquals('now', $configuration->get('test', 'it'));
208                 $this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
209
210                 // second run is not loaded, but now data is found in the db (overwrote cache)
211                 $this->assertEquals($data, $configuration->get('test', 'it'));
212                 $this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
213
214                 // third run is loaded and therefore cache is used
215                 $this->assertEquals($data, $configuration->get('test', 'it'));
216                 $this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
217         }
218
219         /**
220          * Test the configuration delete() method without adapter
221          * @dataProvider dataTests
222          */
223         public function testDeleteWithoutDB($data)
224         {
225                 $configCache = new ConfigCache(['test' => ['it' => $data]]);
226                 $configAdapter = \Mockery::mock(IConfigAdapter::class);
227                 $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(4);
228
229                 $configuration = new Configuration($configCache, $configAdapter);
230
231                 $this->assertEquals($data, $configuration->get('test', 'it'));
232                 $this->assertEquals($data, $configuration->getCache()->get('test', 'it'));
233
234                 $this->assertTrue($configuration->delete('test', 'it'));
235                 $this->assertNull($configuration->get('test', 'it'));
236                 $this->assertNull($configuration->getCache()->get('test', 'it'));
237
238                 $this->assertEmpty($configuration->getCache()->getAll());
239         }
240
241         /**
242          * Test the configuration delete() method with adapter
243          */
244         public function testDeleteWithDB()
245         {
246                 $configCache = new ConfigCache(['test' => ['it' => 'now', 'quarter' => 'true']]);
247                 $configAdapter = \Mockery::mock(IConfigAdapter::class);
248                 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(6);
249                 // constructor loading
250                 $configAdapter->shouldReceive('load')->andReturn([])->once();
251                 $configAdapter->shouldReceive('isLoaded')->with('test', 'it')->andReturn(true)->once();
252
253                 $configAdapter->shouldReceive('delete')->with('test', 'it')->andReturn(false)->once();
254
255                 $configAdapter->shouldReceive('delete')->with('test', 'second')->andReturn(true)->once();
256                 $configAdapter->shouldReceive('delete')->with('test', 'third')->andReturn(false)->once();
257                 $configAdapter->shouldReceive('delete')->with('test', 'quarter')->andReturn(true)->once();
258
259                 $configuration = new Configuration($configCache, $configAdapter);
260
261                 $this->assertEquals('now', $configuration->get('test', 'it'));
262                 $this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
263
264                 // delete from cache only
265                 $this->assertTrue($configuration->delete('test', 'it'));
266                 // delete from db only
267                 $this->assertTrue($configuration->delete('test', 'second'));
268                 // no delete
269                 $this->assertFalse($configuration->delete('test', 'third'));
270                 // delete both
271                 $this->assertTrue($configuration->delete('test', 'quarter'));
272
273                 $this->assertEmpty($configuration->getCache()->getAll());
274         }
275 }