6 namespace Friendica\Test;
8 use Friendica\BaseObject;
9 use Friendica\Core\Config;
10 use Friendica\Database\DBA;
11 use PHPUnit\DbUnit\DataSet\YamlDataSet;
12 use PHPUnit\DbUnit\TestCaseTrait;
13 use PHPUnit\Framework\TestCase;
14 use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
17 * Abstract class used by tests that need a database.
19 abstract class DatabaseTest extends TestCase
21 public function setUp()
23 // Reusable App object
24 $this->app = BaseObject::getApp();
26 Config::set('system', 'url', 'http://localhost');
27 Config::set('system', 'hostname', 'localhost');
28 Config::set('system', 'worker_dont_fork', true);
32 * Get database connection.
34 * This function is executed before each test in order to get a database connection that can be used by tests.
35 * If no prior connection is available, it tries to create one using the USER, PASS and DB environment variables.
37 * If it could not connect to the database, the test is skipped.
39 * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
40 * @see https://phpunit.de/manual/5.7/en/database.html
42 protected function getConnection()
44 if (!getenv('MYSQL_DATABASE')) {
45 $this->markTestSkipped('Please set the MYSQL_* environment variables to your test database credentials.');
48 if (!DBA::connected()) {
49 $this->markTestSkipped('Could not connect to the database.');
52 return $this->createDefaultDBConnection(DBA::getConnection(), getenv('MYSQL_DATABASE'));
56 * Get dataset to populate the database with.
58 * @see https://phpunit.de/manual/5.7/en/database.html
60 protected function getDataSet()
62 return new YamlDataSet(__DIR__ . '/datasets/api.yml');