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