]> git.mxchange.org Git - friendica.git/blob - tests/DatabaseTest.php
Fix tests (static usage for non-static methods)
[friendica.git] / tests / DatabaseTest.php
1 <?php
2 /**
3  * DatabaseTest class.
4  */
5
6 namespace Friendica\Test;
7
8 use Friendica\App\Mode;
9 use Friendica\Core\Config\Cache\ConfigCache;
10 use Friendica\Database\Database;
11 use Friendica\Factory\ConfigFactory;
12 use Friendica\Util\BasePath;
13 use Friendica\Util\ConfigFileLoader;
14 use Friendica\Util\Profiler;
15 use PHPUnit\DbUnit\DataSet\YamlDataSet;
16 use PHPUnit\DbUnit\TestCaseTrait;
17 use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
18 use Psr\Log\NullLogger;
19
20 require_once __DIR__ . '/../boot.php';
21
22 /**
23  * Abstract class used by tests that need a database.
24  */
25 abstract class DatabaseTest extends MockedTest
26 {
27         use TestCaseTrait;
28
29         /** @var Database */
30         protected static $dba;
31
32         /** @var BasePath */
33         protected static $basePath;
34
35         /** @var Mode */
36         protected static $mode;
37
38         /** @var ConfigCache */
39         protected static $configCache;
40
41         /** @var Profiler */
42         protected static $profiler;
43
44         public static function setUpBeforeClass()
45         {
46                 parent::setUpBeforeClass();
47
48                 self::$basePath = new BasePath(dirname(__DIR__));
49                 $configLoader = new ConfigFileLoader(self::$basePath->getPath());
50                 $configFactory = new ConfigFactory();
51                 self::$configCache = $configFactory->createCache($configLoader);
52                 self::$profiler = new Profiler(self::$configCache);
53                 self::$dba = new Database(self::$configCache, self::$profiler, new NullLogger(), $_SERVER);
54                 self::$mode = new Mode(self::$basePath, self::$dba, self::$configCache);
55         }
56
57         /**
58          * Get database connection.
59          *
60          * This function is executed before each test in order to get a database connection that can be used by tests.
61          * If no prior connection is available, it tries to create one using the USER, PASS and DB environment variables.
62          *
63          * If it could not connect to the database, the test is skipped.
64          *
65          * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
66          * @see https://phpunit.de/manual/5.7/en/database.html
67          */
68         protected function getConnection()
69         {
70                 if (!getenv('MYSQL_DATABASE')) {
71                         $this->markTestSkipped('Please set the MYSQL_* environment variables to your test database credentials.');
72                 }
73
74                 if (!self::$dba->isConnected()) {
75                         if (!self::$dba->connect()) {
76                                 $this->markTestSkipped('Could not connect to the database.');
77                         }
78                 }
79
80                 return $this->createDefaultDBConnection(self::$dba->getConnection(), getenv('MYSQL_DATABASE'));
81         }
82
83         /**
84          * Get dataset to populate the database with.
85          * @return YamlDataSet
86          * @see https://phpunit.de/manual/5.7/en/database.html
87          */
88         protected function getDataSet()
89         {
90                 return new YamlDataSet(__DIR__ . '/datasets/api.yml');
91         }
92 }