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
23 * @var App The Friendica App
27 protected function setUp()
29 // Reusable App object
30 $this->app = BaseObject::getApp();
32 Config::set('system', 'url', 'http://localhost');
33 Config::set('system', 'hostname', 'localhost');
34 Config::set('system', 'worker_dont_fork', true);
38 * Get database connection.
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.
43 * If it could not connect to the database, the test is skipped.
45 * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
46 * @see https://phpunit.de/manual/5.7/en/database.html
48 protected function getConnection()
50 if (!getenv('MYSQL_DATABASE')) {
51 $this->markTestSkipped('Please set the MYSQL_* environment variables to your test database credentials.');
54 if (!DBA::connected()) {
55 $this->markTestSkipped('Could not connect to the database.');
58 return $this->createDefaultDBConnection(DBA::getConnection(), getenv('MYSQL_DATABASE'));
62 * Get dataset to populate the database with.
64 * @see https://phpunit.de/manual/5.7/en/database.html
66 protected function getDataSet()
68 return new YamlDataSet(__DIR__ . '/datasets/api.yml');