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