6 namespace Friendica\Test;
8 use Friendica\Database\DBA;
9 use PHPUnit\DbUnit\DataSet\YamlDataSet;
10 use PHPUnit\DbUnit\TestCaseTrait;
11 use PHPUnit\Framework\TestCase;
12 use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
15 * Abstract class used by tests that need a database.
17 abstract class DatabaseTest extends TestCase
23 * Get database connection.
25 * This function is executed before each test in order to get a database connection that can be used by tests.
26 * If no prior connection is available, it tries to create one using the USER, PASS and DB environment variables.
28 * If it could not connect to the database, the test is skipped.
30 * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
31 * @see https://phpunit.de/manual/5.7/en/database.html
33 protected function getConnection()
35 if (!getenv('MYSQL_DATABASE')) {
36 $this->markTestSkipped('Please set the MYSQL_* environment variables to your test database credentials.');
39 if (!DBA::connected()) {
40 $this->markTestSkipped('Could not connect to the database.');
43 return $this->createDefaultDBConnection(DBA::getConnection(), getenv('MYSQL_DATABASE'));
47 * Get dataset to populate the database with.
49 * @see https://phpunit.de/manual/5.7/en/database.html
51 protected function getDataSet()
53 return new YamlDataSet(__DIR__ . '/datasets/api.yml');