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