]> git.mxchange.org Git - friendica.git/blob - tests/DatabaseTest.php
Merge remote-tracking branch 'upstream/develop' into fetch-item
[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\Factory\DBFactory;
13 use Friendica\Factory\ProfilerFactory;
14 use Friendica\Util\BasePath;
15 use Friendica\Util\ConfigFileLoader;
16 use Friendica\Util\Profiler;
17 use PHPUnit\DbUnit\DataSet\YamlDataSet;
18 use PHPUnit\DbUnit\TestCaseTrait;
19 use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
20
21 require_once __DIR__ . '/../boot.php';
22
23 /**
24  * Abstract class used by tests that need a database.
25  */
26 abstract class DatabaseTest extends MockedTest
27 {
28         use TestCaseTrait;
29
30         /** @var Database */
31         protected static $dba;
32
33         /** @var BasePath */
34         protected static $basePath;
35
36         /** @var Mode */
37         protected static $mode;
38
39         /** @var ConfigCache */
40         protected static $configCache;
41
42         /** @var Profiler */
43         protected static $profiler;
44
45         public static function setUpBeforeClass()
46         {
47                 parent::setUpBeforeClass();
48
49                 self::$basePath = BasePath::create(dirname(__DIR__));
50                 self::$mode = new Mode(self::$basePath);
51                 $configLoader = new ConfigFileLoader(self::$basePath, self::$mode);
52                 self::$configCache = ConfigFactory::createCache($configLoader);
53                 self::$profiler = ProfilerFactory::create(self::$configCache);
54                 self::$dba = DBFactory::init(self::$configCache, self::$profiler, $_SERVER);
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 }