]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Config/ConfigTransactionTest.php
Improved logging
[friendica.git] / tests / src / Core / Config / ConfigTransactionTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Capability\ISetConfigValuesTransactionally;
25 use Friendica\Core\Config\Model\DatabaseConfig;
26 use Friendica\Core\Config\Model\ReadOnlyFileConfig;
27 use Friendica\Core\Config\Model\ConfigTransaction;
28 use Friendica\Core\Config\Util\ConfigFileManager;
29 use Friendica\Core\Config\ValueObject\Cache;
30 use Friendica\Database\Database;
31 use Friendica\Test\DatabaseTest;
32 use Friendica\Test\FixtureTest;
33 use Friendica\Test\MockedTest;
34 use Friendica\Test\Util\Database\StaticDatabase;
35 use Friendica\Test\Util\VFSTrait;
36 use Mockery\Exception\InvalidCountException;
37
38 class ConfigTransactionTest extends FixtureTest
39 {
40         /** @var ConfigFileManager */
41         protected $configFileManager;
42
43         protected function setUp(): void
44         {
45                 parent::setUp();
46
47                 $this->configFileManager = new ConfigFileManager($this->root->url(), $this->root->url() . '/config/', $this->root->url() . '/static/');
48         }
49
50         public function dataTests(): array
51         {
52                 return [
53                         'default' => [
54                                 'data' => include dirname(__FILE__, 4) . '/datasets/B.node.config.php',
55                         ]
56                 ];
57         }
58
59         public function testInstance()
60         {
61                 $config            = new DatabaseConfig($this->dice->create(Database::class), new Cache());
62                 $configTransaction = new ConfigTransaction($config);
63
64                 self::assertInstanceOf(ISetConfigValuesTransactionally::class, $configTransaction);
65                 self::assertInstanceOf(ConfigTransaction::class, $configTransaction);
66         }
67
68         public function testConfigTransaction()
69         {
70                 $config = new DatabaseConfig($this->dice->create(Database::class), new Cache());
71                 $config->set('config', 'key1', 'value1');
72                 $config->set('system', 'key2', 'value2');
73                 $config->set('system', 'keyDel', 'valueDel');
74                 $config->set('delete', 'keyDel', 'catDel');
75
76                 $configTransaction = new ConfigTransaction($config);
77
78                 // new key-value
79                 $configTransaction->set('transaction', 'key3', 'value3');
80                 // overwrite key-value
81                 $configTransaction->set('config', 'key1', 'changedValue1');
82                 // delete key-value
83                 $configTransaction->delete('system', 'keyDel');
84                 // delete last key of category - so the category is gone
85                 $configTransaction->delete('delete', 'keyDel');
86
87                 // The main config still doesn't know about the change
88                 self::assertNull($config->get('transaction', 'key3'));
89                 self::assertEquals('value1', $config->get('config', 'key1'));
90                 self::assertEquals('valueDel', $config->get('system', 'keyDel'));
91                 self::assertEquals('catDel', $config->get('delete', 'keyDel'));
92                 // The config file still doesn't know it either
93
94                 // save it back!
95                 $configTransaction->commit();
96
97                 // Now every config and file knows the change
98                 self::assertEquals('changedValue1', $config->get('config', 'key1'));
99                 self::assertEquals('value3', $config->get('transaction', 'key3'));
100                 self::assertNull($config->get('system', 'keyDel'));
101                 self::assertNull($config->get('delete', 'keyDel'));
102                 // the whole category should be gone
103                 self::assertNull($tempData['delete'] ?? null);
104         }
105
106         /**
107          * This test asserts that in empty transactions, no saveData is called, thus no config file writing was performed
108          */
109         public function testNothingToDo()
110         {
111                 $this->configFileManager = \Mockery::spy(ConfigFileManager::class);
112
113                 $config = new DatabaseConfig($this->dice->create(Database::class), new Cache());
114                 $configTransaction = new ConfigTransaction($config);
115
116                 // commit empty transaction
117                 $configTransaction->commit();
118
119                 try {
120                         $this->configFileManager->shouldNotHaveReceived('saveData');
121                 } catch (InvalidCountException $exception) {
122                         self::fail($exception);
123                 }
124
125                 // If not failed, the test ends successfully :)
126                 self::assertTrue(true);
127         }
128 }