6 namespace Friendica\Test;
9 use Friendica\Database\DBStructure;
10 use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
11 use PHPUnit\DbUnit\DataSet\YamlDataSet;
12 use PHPUnit\DbUnit\TestCaseTrait;
13 use PHPUnit\Framework\TestCase;
16 * Abstract class used by tests that need a database.
18 abstract class DatabaseTest extends TestCase
24 * Get database connection.
26 * This function is executed before each test in order to get a database connection that can be used by tests.
27 * If no prior connection is available, it tries to create one using the USER, PASS and DB environment variables.
29 * If it could not connect to the database, the test is skipped.
31 * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
32 * @see https://phpunit.de/manual/5.7/en/database.html
34 protected function getConnection()
36 if (!dba::$connected) {
37 dba::connect('localhost', getenv('USER'), getenv('PASS'), getenv('DB'));
39 if (dba::$connected) {
41 // We need to do this in order to disable logging
42 $app->module = 'install';
44 // Create database structure
45 DBStructure::update(false, true, true);
47 $this->markTestSkipped('Could not connect to the database.');
51 return $this->createDefaultDBConnection(dba::get_db(), getenv('DB'));
55 * Get dataset to populate the database with.
57 * @see https://phpunit.de/manual/5.7/en/database.html
59 protected function getDataSet()
61 return new YamlDataSet(__DIR__ . '/datasets/api.yml');