Fix JIT Config Adapter caching
[friendica.git/.git] / tests / src / Core / Config / PreloadConfigTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Core\Config;
23
24 use Friendica\Core\Config\PreloadConfig;
25
26 class PreloadConfigTest extends ConfigTest
27 {
28         public function getInstance()
29         {
30                 return new PreloadConfig($this->configCache, $this->configModel);
31         }
32
33         /**
34          * @dataProvider dataConfigLoad
35          */
36         public function testSetUp(array $data)
37         {
38                 $this->configModel->shouldReceive('load')
39                                   ->andReturn($data)
40                                   ->once();
41
42                 parent::testSetUp($data);
43         }
44
45         /**
46          * @dataProvider dataConfigLoad
47          *
48          * @param array $data
49          * @param array $load
50          */
51         public function testLoad(array $data, array $load)
52         {
53                 $this->configModel->shouldReceive('isConnected')
54                                   ->andReturn(true)
55                                   ->once();
56
57                 $this->configModel->shouldReceive('load')
58                                   ->andReturn($data)
59                                   ->once();
60
61                 parent::testLoad($data, $load);
62
63                 // Assert that every category is loaded everytime
64                 foreach ($data as $cat => $values) {
65                         self::assertConfig($cat, $values);
66                 }
67         }
68
69         /**
70          * @dataProvider dataDoubleLoad
71          *
72          * @param array $data1
73          * @param array $data2
74          */
75         public function testCacheLoadDouble(array $data1, array $data2, array $expect = [])
76         {
77                 $this->configModel->shouldReceive('isConnected')
78                                   ->andReturn(true)
79                                   ->once();
80
81                 $this->configModel->shouldReceive('load')
82                                   ->andReturn($data1)
83                                   ->once();
84
85                 parent::testCacheLoadDouble($data1, $data2);
86
87                 // Assert that every category is loaded everytime and is NOT overwritten
88                 foreach ($data1 as $cat => $values) {
89                         self::assertConfig($cat, $values);
90                 }
91         }
92
93         /**
94          * @dataProvider dataTests
95          */
96         public function testSetGetWithDB($data)
97         {
98                 $this->configModel->shouldReceive('isConnected')
99                                   ->andReturn(true)
100                                   ->times(2);
101
102                 $this->configModel->shouldReceive('load')->andReturn(['config' => []])->once();
103
104                 parent::testSetGetWithDB($data);
105         }
106
107         /**
108          * @dataProvider dataTests
109          */
110         public function testGetWithRefresh($data)
111         {
112                 $this->configModel->shouldReceive('isConnected')
113                                   ->andReturn(true)
114                                   ->times(2);
115
116                 // constructor loading
117                 $this->configModel->shouldReceive('load')
118                                   ->andReturn(['config' => []])
119                                   ->once();
120
121                 // mocking one get
122                 $this->configModel->shouldReceive('get')
123                                   ->with('test', 'it')
124                                   ->andReturn($data)
125                                   ->once();
126
127                 parent::testGetWithRefresh($data);
128         }
129
130
131         public function testGetWrongWithoutDB()
132         {
133                 $this->configModel->shouldReceive('isConnected')
134                                   ->andReturn(false)
135                                   ->times(2);
136
137                 parent::testGetWrongWithoutDB();
138         }
139
140         /**
141          * @dataProvider dataTests
142          */
143         public function testDeleteWithoutDB($data)
144         {
145                 $this->configModel->shouldReceive('isConnected')
146                                   ->andReturn(false)
147                                   ->times(2);
148
149                 parent::testDeleteWithoutDB($data);
150         }
151
152         public function testDeleteWithDB()
153         {
154                 $this->configModel->shouldReceive('isConnected')
155                                   ->andReturn(true)
156                                   ->times(5);
157
158                 // constructor loading
159                 $this->configModel->shouldReceive('load')
160                                   ->andReturn(['config' => []])
161                                   ->once();
162
163                 parent::testDeleteWithDB();
164         }
165
166
167         public function testSetGetHighPrio()
168         {
169                 $this->configModel->shouldReceive('isConnected')
170                                                   ->andReturn(true);
171
172                 // constructor loading
173                 $this->configModel->shouldReceive('load')
174                                                   ->andReturn(['config' => []])
175                                                   ->once();
176
177                 $this->configModel->shouldReceive('set')
178                                                   ->with('config', 'test', '123')
179                                                   ->andReturn(true)
180                                                   ->once();
181
182                 $this->configModel->shouldReceive('get')
183                                                   ->with('config', 'test')
184                                                   ->andReturn('123')
185                                                   ->once();
186
187                 parent::testSetGetHighPrio();
188         }
189
190         public function testSetGetLowPrio()
191         {
192                 $this->configModel->shouldReceive('isConnected')
193                                                   ->andReturn(true);
194
195                 // constructor loading
196                 $this->configModel->shouldReceive('load')
197                                                   ->andReturn(['config' => ['test' => 'it']])
198                                                   ->once();
199
200                 $this->configModel->shouldReceive('set')
201                                                   ->with('config', 'test', '123')
202                                                   ->andReturn(true)
203                                                   ->once();
204
205                 // mocking one get without result
206                 $this->configModel->shouldReceive('get')
207                                                   ->with('config', 'test')
208                                                   ->andReturn('it')
209                                                   ->once();
210
211                 parent::testSetGetLowPrio();
212         }
213 }