Update copyright
[friendica.git/.git] / tests / src / Core / Config / JitConfigTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
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\JitConfig;
25
26 class JitConfigTest extends ConfigTest
27 {
28         public function getInstance()
29         {
30                 return new JitConfig($this->configCache, $this->configModel);
31         }
32
33         /**
34          * @dataProvider dataConfigLoad
35          */
36         public function testSetUp(array $data)
37         {
38                 $this->configModel->shouldReceive('load')
39                                   ->with('config')
40                                   ->andReturn(['config' => $data['config']])
41                                   ->once();
42
43                 parent::testSetUp($data);
44         }
45
46         /**
47          * @dataProvider dataConfigLoad
48          *
49          * @param array $data
50          * @param array $load
51          */
52         public function testLoad(array $data, array $load)
53         {
54                 $this->configModel->shouldReceive('isConnected')
55                                   ->andReturn(true)
56                                   ->times(count($load) + 1);
57
58                 $this->configModel->shouldReceive('load')
59                                   ->with('config')
60                                   ->andReturn(['config' => $data['config']])
61                                   ->once();
62
63                 foreach ($load as $loadCat) {
64                         $this->configModel->shouldReceive('load')
65                                           ->with($loadCat)
66                                           ->andReturn([$loadCat => $data[$loadCat]])
67                                           ->once();
68                 }
69
70                 parent::testLoad($data, $load);
71         }
72
73         /**
74          * @dataProvider dataDoubleLoad
75          */
76         public function testCacheLoadDouble(array $data1, array $data2, array $expect = [])
77         {
78                 $this->configModel->shouldReceive('isConnected')
79                                   ->andReturn(true)
80                                   ->times(count($data1) + count($data2) + 1);
81
82                 $this->configModel->shouldReceive('load')
83                                   ->with('config')
84                                   ->andReturn(['config' => $data1['config']])
85                                   ->once();
86
87                 foreach ($data1 as $cat => $data) {
88                         $this->configModel->shouldReceive('load')
89                                           ->with($cat)
90                                           ->andReturn([$cat => $data])
91                                           ->once();
92                 }
93
94
95                 foreach ($data2 as $cat => $data) {
96                         $this->configModel->shouldReceive('load')
97                                           ->with($cat)
98                                           ->andReturn([$cat => $data])
99                                           ->once();
100                 }
101
102                 parent::testCacheLoadDouble($data1, $data2);
103
104                 // Assert the expected categories
105                 foreach ($data2 as $cat => $data) {
106                         self::assertConfig($cat, $expect[$cat]);
107                 }
108         }
109
110         /**
111          * @dataProvider dataTests
112          */
113         public function testSetGetWithDB($data)
114         {
115                 $this->configModel->shouldReceive('isConnected')
116                                   ->andReturn(true)
117                                   ->times(3);
118
119                 $this->configModel->shouldReceive('load')->with('config')->andReturn(['config' => []])->once();
120
121                 parent::testSetGetWithDB($data);
122         }
123
124         /**
125          * @dataProvider dataTests
126          */
127         public function testGetWithRefresh($data)
128         {
129                 $this->configModel->shouldReceive('isConnected')
130                                   ->andReturn(true)
131                                   ->times(4);
132
133                 // constructor loading
134                 $this->configModel->shouldReceive('load')
135                                   ->with('config')
136                                   ->andReturn(['config' => []])
137                                   ->once();
138
139                 // mocking one get without result
140                 $this->configModel->shouldReceive('get')
141                                   ->with('test', 'it')
142                                   ->andReturn(null)
143                                   ->once();
144
145                 // mocking the data get
146                 $this->configModel->shouldReceive('get')
147                                   ->with('test', 'it')
148                                   ->andReturn($data)
149                                   ->once();
150
151                 // mocking second get
152                 $this->configModel->shouldReceive('get')
153                                   ->with('test', 'not')
154                                   ->andReturn(null)
155                                   ->once();
156
157                 parent::testGetWithRefresh($data);
158         }
159
160         public function testGetWrongWithoutDB()
161         {
162                 $this->configModel->shouldReceive('isConnected')
163                                   ->andReturn(false)
164                                   ->times(4);
165
166                 parent::testGetWrongWithoutDB();
167         }
168
169         /**
170          * @dataProvider dataTests
171          */
172         public function testDeleteWithoutDB($data)
173         {
174                 $this->configModel->shouldReceive('isConnected')
175                                   ->andReturn(false)
176                                   ->times(4);
177
178                 parent::testDeleteWithoutDB($data);
179         }
180
181         public function testDeleteWithDB()
182         {
183                 $this->configModel->shouldReceive('isConnected')
184                                   ->andReturn(true)
185                                   ->times(6);
186
187                 // constructor loading
188                 $this->configModel->shouldReceive('load')
189                                   ->with('config')
190                                   ->andReturn(['config' => []])
191                                   ->once();
192
193                 // mocking one get without result
194                 $this->configModel->shouldReceive('get')
195                                   ->with('test', 'it')
196                                   ->andReturn(null)
197                                   ->once();
198
199                 parent::testDeleteWithDB();
200         }
201 }