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