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