6 namespace Friendica\Test;
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;
18 * Abstract class used by tests that need a database.
20 abstract class DatabaseTest extends TestCase
25 * @var App The Friendica App
29 protected function setUp()
31 // Reusable App object
32 $this->app = BaseObject::getApp();
34 Config::set('system', 'url', 'http://localhost');
35 Config::set('system', 'hostname', 'localhost');
36 Config::set('system', 'worker_dont_fork', true);
40 * Get database connection.
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.
45 * If it could not connect to the database, the test is skipped.
47 * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
48 * @see https://phpunit.de/manual/5.7/en/database.html
50 protected function getConnection()
52 if (!getenv('MYSQL_DATABASE')) {
53 $this->markTestSkipped('Please set the MYSQL_* environment variables to your test database credentials.');
56 if (!DBA::connected()) {
57 $this->markTestSkipped('Could not connect to the database.');
60 return $this->createDefaultDBConnection(DBA::getConnection(), getenv('MYSQL_DATABASE'));
64 * Get dataset to populate the database with.
66 * @see https://phpunit.de/manual/5.7/en/database.html
68 protected function getDataSet()
70 return new YamlDataSet(__DIR__ . '/datasets/api.yml');