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