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