]> git.mxchange.org Git - friendica.git/blob - tests/DatabaseTest.php
Merge branch 'develop' of https://github.com/friendica/friendica into develop
[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\BaseObject;
10 use Friendica\Core\Config;
11 use Friendica\Database\DBA;
12 use PHPUnit\DbUnit\DataSet\YamlDataSet;
13 use PHPUnit\DbUnit\TestCaseTrait;
14 use PHPUnit\Framework\TestCase;
15 use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
16
17 /**
18  * Abstract class used by tests that need a database.
19  */
20 abstract class DatabaseTest extends TestCase
21 {
22         use TestCaseTrait;
23
24         /**
25          * @var App The Friendica App
26          */
27         protected $app;
28
29         protected function setUp()
30         {
31                 // Reusable App object
32                 $this->app = BaseObject::getApp();
33
34                 Config::set('system', 'url', 'http://localhost');
35                 Config::set('system', 'hostname', 'localhost');
36                 Config::set('system', 'worker_dont_fork', true);
37         }
38
39         /**
40          * Get database connection.
41          *
42          * This function is executed before each test in order to get a database connection that can be used by tests.
43          * If no prior connection is available, it tries to create one using the USER, PASS and DB environment variables.
44          *
45          * If it could not connect to the database, the test is skipped.
46          *
47          * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
48          * @see https://phpunit.de/manual/5.7/en/database.html
49          */
50         protected function getConnection()
51         {
52                 if (!getenv('MYSQL_DATABASE')) {
53                         $this->markTestSkipped('Please set the MYSQL_* environment variables to your test database credentials.');
54                 }
55
56                 if (!DBA::connected()) {
57                         $this->markTestSkipped('Could not connect to the database.');
58                 }
59
60                 return $this->createDefaultDBConnection(DBA::getConnection(), getenv('MYSQL_DATABASE'));
61         }
62
63         /**
64          * Get dataset to populate the database with.
65          * @return YamlDataSet
66          * @see https://phpunit.de/manual/5.7/en/database.html
67          */
68         protected function getDataSet()
69         {
70                 return new YamlDataSet(__DIR__ . '/datasets/api.yml');
71         }
72 }