]> git.mxchange.org Git - friendica.git/blob - tests/DatabaseTest.php
Merge pull request #6581 from nupplaphil/config_refact
[friendica.git] / tests / DatabaseTest.php
1 <?php
2 /**
3  * DatabaseTest class.
4  */
5
6 namespace Friendica\Test;
7
8 use Friendica\Core\Config;
9 use Friendica\Database\DBA;
10 use Friendica\Factory;
11 use Friendica\Util\BasePath;
12 use PHPUnit\DbUnit\DataSet\YamlDataSet;
13 use PHPUnit\DbUnit\TestCaseTrait;
14 use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
15
16 require_once __DIR__ . '/../boot.php';
17
18 /**
19  * Abstract class used by tests that need a database.
20  */
21 abstract class DatabaseTest extends MockedTest
22 {
23         use TestCaseTrait;
24
25         /**
26          * Get database connection.
27          *
28          * This function is executed before each test in order to get a database connection that can be used by tests.
29          * If no prior connection is available, it tries to create one using the USER, PASS and DB environment variables.
30          *
31          * If it could not connect to the database, the test is skipped.
32          *
33          * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
34          * @see https://phpunit.de/manual/5.7/en/database.html
35          */
36         protected function getConnection()
37         {
38                 if (!getenv('MYSQL_DATABASE')) {
39                         $this->markTestSkipped('Please set the MYSQL_* environment variables to your test database credentials.');
40                 }
41
42                 $basedir = BasePath::create(dirname(__DIR__));
43                 $configLoader = new Config\ConfigCacheLoader($basedir);
44                 $config = Factory\ConfigFactory::createCache($configLoader);
45
46                 DBA::connect(
47                         $config,
48                         getenv('MYSQL_HOST'),
49                         getenv('MYSQL_USERNAME'),
50                         getenv('MYSQL_PASSWORD'),
51                         getenv('MYSQL_DATABASE'));
52
53                 if (!DBA::connected()) {
54                         $this->markTestSkipped('Could not connect to the database.');
55                 }
56
57                 return $this->createDefaultDBConnection(DBA::getConnection(), getenv('MYSQL_DATABASE'));
58         }
59
60         /**
61          * Get dataset to populate the database with.
62          * @return YamlDataSet
63          * @see https://phpunit.de/manual/5.7/en/database.html
64          */
65         protected function getDataSet()
66         {
67                 return new YamlDataSet(__DIR__ . '/datasets/api.yml');
68         }
69 }