]> git.mxchange.org Git - friendica.git/blob - tests/functional/DependencyCheckTest.php
Replace Config-Cache dependency with Config-Model (no more DB-waiting necessary)
[friendica.git] / tests / functional / DependencyCheckTest.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\functional;
23
24 use Dice\Dice;
25 use Friendica\App;
26 use Friendica\Core\Cache\Capability\ICanCache;
27 use Friendica\Core\Cache\Capability\ICanCacheInMemory;
28 use Friendica\Core\Config\Model\Config;
29 use Friendica\Core\Config\ValueObject\Cache;
30 use Friendica\Core\Config\Capability\IManageConfigValues;
31 use Friendica\Core\Lock\Capability\ICanLock;
32 use Friendica\Database\Database;
33 use Friendica\Test\Util\VFSTrait;
34 use Friendica\Util\BasePath;
35 use Friendica\Core\Config\Util\ConfigFileManager;
36 use Friendica\Util\Profiler;
37 use PHPUnit\Framework\TestCase;
38 use Psr\Log\LoggerInterface;
39
40 class DependencyCheckTest extends TestCase
41 {
42         use VFSTrait;
43
44         /**
45          * @var Dice
46          */
47         private $dice;
48
49         protected function setUp() : void
50         {
51                 parent::setUp();
52
53                 $this->setUpVfsDir();
54
55                 $this->dice = (new Dice())
56                         ->addRules(include __DIR__ . '/../../static/dependencies.config.php');
57         }
58
59         /**
60          * Test the creation of the BasePath
61          */
62         public function testBasePath()
63         {
64                 /** @var BasePath $basePath */
65                 $basePath = $this->dice->create(BasePath::class, [$this->root->url()]);
66
67                 self::assertInstanceOf(BasePath::class, $basePath);
68                 self::assertEquals($this->root->url(), $basePath->getPath());
69         }
70
71         /**
72          * Test the initial config cache
73          * Should not need any other files
74          */
75         public function testConfigFileLoader()
76         {
77                 /** @var ConfigFileManager $configFileManager */
78                 $configFileManager = $this->dice->create(ConfigFileManager::class);
79
80                 self::assertInstanceOf(ConfigFileManager::class, $configFileManager);
81
82                 $configCache = new Cache();
83                 $configFileManager->setupCache($configCache);
84
85                 self::assertNotEmpty($configCache->getAll());
86                 self::assertArrayHasKey('database', $configCache->getAll());
87                 self::assertArrayHasKey('system', $configCache->getAll());
88         }
89
90         public function testDatabase()
91         {
92                 // PDO needs to be disabled for PHP 7.2, see https://jira.mariadb.org/browse/MDEV-24121
93                 if (version_compare(PHP_VERSION, '7.3') < 0) {
94                         $configCache = $this->dice->create(Cache::class);
95                         $configCache->set('database', 'disable_pdo', true);
96                 }
97
98                 /** @var Database $database */
99                 $database = $this->dice->create(Database::class);
100
101                 self::assertInstanceOf(Database::class, $database);
102                 self::assertContains($database->getDriver(), [Database::PDO, Database::MYSQLI], 'The driver returns an unexpected value');
103                 self::assertNotNull($database->getConnection(), 'There is no database connection');
104
105                 $result = $database->p("SELECT 1");
106                 self::assertEquals('', $database->errorMessage(), 'There had been a database error message');
107                 self::assertEquals(0, $database->errorNo(), 'There had been a database error number');
108
109                 self::assertTrue($database->connected(), 'The database is not connected');
110         }
111
112         public function testAppMode()
113         {
114                 // PDO needs to be disabled for PHP 7.2, see https://jira.mariadb.org/browse/MDEV-24121
115                 if (version_compare(PHP_VERSION, '7.3') < 0) {
116                         $configCache = $this->dice->create(Cache::class);
117                         $configCache->set('database', 'disable_pdo', true);
118                 }
119
120                 /** @var App\Mode $mode */
121                 $mode = $this->dice->create(App\Mode::class);
122
123                 self::assertInstanceOf(App\Mode::class, $mode);
124
125                 self::assertTrue($mode->has(App\Mode::LOCALCONFIGPRESENT), 'No local config present');
126                 self::assertTrue($mode->has(App\Mode::DBAVAILABLE), 'Database is not available');
127                 self::assertTrue($mode->has(App\Mode::MAINTENANCEDISABLED), 'In maintenance mode');
128
129                 self::assertTrue($mode->isNormal(), 'Not in normal mode');
130         }
131
132         public function testConfiguration()
133         {
134                 /** @var IManageConfigValues $config */
135                 $config = $this->dice->create(IManageConfigValues::class);
136
137                 self::assertInstanceOf(IManageConfigValues::class, $config);
138
139                 self::assertNotEmpty($config->get('database', 'username'));
140         }
141
142         public function testLogger()
143         {
144                 /** @var LoggerInterface $logger */
145                 $logger = $this->dice->create(LoggerInterface::class, ['test']);
146
147                 self::assertInstanceOf(LoggerInterface::class, $logger);
148         }
149
150         public function testDevLogger()
151         {
152                 /** @var IManageConfigValues $config */
153                 $config = $this->dice->create(IManageConfigValues::class);
154                 $config->set('system', 'dlogfile', $this->root->url() . '/friendica.log');
155
156                 /** @var LoggerInterface $logger */
157                 $logger = $this->dice->create('$devLogger', ['dev']);
158
159                 self::assertInstanceOf(LoggerInterface::class, $logger);
160         }
161
162         public function testCache()
163         {
164                 /** @var ICanCache $cache */
165                 $cache = $this->dice->create(ICanCache::class);
166
167                 self::assertInstanceOf(ICanCache::class, $cache);
168         }
169
170         public function testMemoryCache()
171         {
172                 /** @var ICanCacheInMemory $cache */
173                 $cache = $this->dice->create(ICanCacheInMemory::class);
174
175                 // We need to check "just" ICache, because the default Cache is DB-Cache, which isn't a memorycache
176                 self::assertInstanceOf(ICanCache::class, $cache);
177         }
178
179         public function testLock()
180         {
181                 /** @var ICanLock $cache */
182                 $lock = $this->dice->create(ICanLock::class);
183
184                 self::assertInstanceOf(ICanLock::class, $lock);
185         }
186 }