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