]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Config/ConfigurationTest.php
6ce81a41b0b49e43b264abf8923d2f954add7849
[friendica.git] / tests / src / Core / Config / ConfigurationTest.php
1 <?php
2
3 namespace Friendica\Test\Core\Config;
4
5 use Friendica\Core\Config\Cache\ConfigCache;
6 use Friendica\Core\Config\Cache\IConfigCache;
7 use Friendica\Core\Config\Configuration;
8 use Friendica\Test\MockedTest;
9
10 class ConfigurationTest extends MockedTest
11 {
12         /**
13          * Test the configuration initialization
14          */
15         public function testSetUp()
16         {
17                 $configCache = new ConfigCache();
18                 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
19                 $configAdapter->shouldReceive('isConnected')->andReturn(false)->once();
20
21                 $configuration = new Configuration($configCache, $configAdapter);
22
23                 $this->assertInstanceOf(IConfigCache::class, $configuration->getCache());
24         }
25
26         /**
27          * Test if the configuration load() method
28          */
29         public function testCacheLoad()
30         {
31                 $configCache = new ConfigCache();
32                 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
33                 $configAdapter->shouldReceive('isConnected')->andReturn(true)->twice();
34                 // constructor loading
35                 $configAdapter->shouldReceive('load')->andReturn([])->once();
36                 // expected loading
37                 $configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'it']])->once();
38
39                 $configuration = new Configuration($configCache, $configAdapter);
40                 $configuration->load('testing');
41
42                 $this->assertEquals('it', $configuration->get('testing', 'test'));
43                 $this->assertEquals('it', $configuration->getCache()->get('testing', 'test'));
44         }
45
46         /**
47          * Test if the configuration load() method with overwrite
48          */
49         public function testCacheLoadDouble()
50         {
51                 $configCache = new ConfigCache();
52                 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
53                 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(3);
54                 // constructor loading
55                 $configAdapter->shouldReceive('load')->andReturn([])->once();
56                 // expected loading
57                 $configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'it']])->once();
58                 // expected next loading
59                 $configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'again']])->once();
60
61                 $configuration = new Configuration($configCache, $configAdapter);
62                 $configuration->load('testing');
63
64                 $this->assertEquals('it', $configuration->get('testing', 'test'));
65                 $this->assertEquals('it', $configuration->getCache()->get('testing', 'test'));
66
67                 $configuration->load('testing');
68
69                 $this->assertEquals('again', $configuration->get('testing', 'test'));
70                 $this->assertEquals('again', $configuration->getCache()->get('testing', 'test'));
71         }
72
73         /**
74          * Test if the configuration get() and set() methods without adapter
75          */
76         public function testSetGetWithoutDB()
77         {
78                 $configCache = new ConfigCache();
79                 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
80                 $configAdapter->shouldReceive('isConnected')->andReturn(false)->twice();
81
82                 $configuration = new Configuration($configCache, $configAdapter);
83
84                 $this->assertTrue($configuration->set('test', 'it', 'now'));
85
86                 $this->assertEquals('now', $configuration->get('test', 'it'));
87                 $this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
88         }
89
90         /**
91          * Test if the configuration get() and set() methods with adapter
92          */
93         public function testSetGetWithDB()
94         {
95                 $configCache = new ConfigCache();
96                 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
97                 $configAdapter->shouldReceive('isConnected')->andReturn(true)->twice();
98                 // constructor loading
99                 $configAdapter->shouldReceive('load')->andReturn([])->once();
100                 $configAdapter->shouldReceive('set')->with('test', 'it', 'now')->andReturn(true)->once();
101
102                 $configuration = new Configuration($configCache, $configAdapter);
103
104                 $this->assertTrue($configuration->set('test', 'it', 'now'));
105
106                 $this->assertEquals('now', $configuration->get('test', 'it'));
107                 $this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
108         }
109
110         /**
111          * Test the configuration get() method with wrong value and no db
112          */
113         public function testGetWrongWithoutDB()
114         {
115                 $configCache = new ConfigCache();
116                 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
117                 $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(4);
118
119                 $configuration = new Configuration($configCache, $configAdapter);
120
121                 // without refresh
122                 $this->assertNull($configuration->get('test', 'it'));
123
124                 /// beware that the cache returns '!<unset>!' and not null for a non existing value
125                 $this->assertEquals('!<unset>!', $configuration->getCache()->get('test', 'it'));
126
127                 // with default value
128                 $this->assertEquals('default', $configuration->get('test', 'it', 'default'));
129
130                 // with default value and refresh
131                 $this->assertEquals('default', $configuration->get('test', 'it', 'default', true));
132         }
133
134         /**
135          * Test the configuration get() method with refresh
136          */
137         public function testGetWithRefresh()
138         {
139                 $configCache = new ConfigCache(['test' => ['it' => 'now']]);
140                 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
141                 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(3);
142                 // constructor loading
143                 $configAdapter->shouldReceive('load')->andReturn([])->once();
144                 $configAdapter->shouldReceive('get')->with('test', 'it')->andReturn('again')->once();
145                 $configAdapter->shouldReceive('get')->with('test', 'not')->andReturn('!<unset>!')->once();
146
147                 $configuration = new Configuration($configCache, $configAdapter);
148
149                 // without refresh
150                 $this->assertEquals('now', $configuration->get('test', 'it'));
151                 $this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
152
153                 // with refresh
154                 $this->assertEquals('again', $configuration->get('test', 'it', null, true));
155                 $this->assertEquals('again', $configuration->getCache()->get('test', 'it'));
156
157                 // without refresh and wrong value and default
158                 $this->assertEquals('default', $configuration->get('test', 'not', 'default'));
159                 $this->assertEquals('!<unset>!', $configuration->getCache()->get('test', 'not'));
160         }
161
162         /**
163          * Test the configuration delete() method without adapter
164          */
165         public function testDeleteWithoutDB()
166         {
167                 $configCache = new ConfigCache(['test' => ['it' => 'now']]);
168                 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
169                 $configAdapter->shouldReceive('isConnected')->andReturn(false)->times(3);
170
171                 $configuration = new Configuration($configCache, $configAdapter);
172
173                 $this->assertEquals('now', $configuration->get('test', 'it'));
174                 $this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
175
176                 $this->assertTrue($configuration->delete('test', 'it'));
177                 $this->assertNull($configuration->get('test', 'it'));
178                 $this->assertEquals('!<unset>!', $configuration->getCache()->get('test', 'it'));
179
180                 $this->assertEmpty($configuration->getCache()->getAll());
181         }
182
183         /**
184          * Test the configuration delete() method with adapter
185          */
186         public function testDeleteWithDB()
187         {
188                 $configCache = new ConfigCache(['test' => ['it' => 'now', 'quarter' => 'true']]);
189                 $configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
190                 $configAdapter->shouldReceive('isConnected')->andReturn(true)->times(5);
191                 // constructor loading
192                 $configAdapter->shouldReceive('load')->andReturn([])->once();
193                 $configAdapter->shouldReceive('delete')->with('test', 'it')->andReturn(false)->once();
194
195                 $configAdapter->shouldReceive('delete')->with('test', 'second')->andReturn(true)->once();
196                 $configAdapter->shouldReceive('delete')->with('test', 'third')->andReturn(false)->once();
197                 $configAdapter->shouldReceive('delete')->with('test', 'quarter')->andReturn(true)->once();
198
199                 $configuration = new Configuration($configCache, $configAdapter);
200
201                 $this->assertEquals('now', $configuration->get('test', 'it'));
202                 $this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
203
204                 // delete from cache only
205                 $this->assertTrue($configuration->delete('test', 'it'));
206                 // delete from db only
207                 $this->assertTrue($configuration->delete('test', 'second'));
208                 // no delete
209                 $this->assertFalse($configuration->delete('test', 'third'));
210                 // delete both
211                 $this->assertTrue($configuration->delete('test', 'quarter'));
212
213                 $this->assertEmpty($configuration->getCache()->getAll());
214         }
215 }