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