6 namespace Friendica\Test;
8 use Friendica\Core\Config;
9 use Friendica\Database\DBA;
10 use Friendica\Factory;
11 use Friendica\Util\BasePath;
12 use PHPUnit\DbUnit\DataSet\YamlDataSet;
13 use PHPUnit\DbUnit\TestCaseTrait;
14 use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
16 require_once __DIR__ . '/../boot.php';
19 * Abstract class used by tests that need a database.
21 abstract class DatabaseTest extends MockedTest
26 * Get database connection.
28 * This function is executed before each test in order to get a database connection that can be used by tests.
29 * If no prior connection is available, it tries to create one using the USER, PASS and DB environment variables.
31 * If it could not connect to the database, the test is skipped.
33 * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
34 * @see https://phpunit.de/manual/5.7/en/database.html
36 protected function getConnection()
38 if (!getenv('MYSQL_DATABASE')) {
39 $this->markTestSkipped('Please set the MYSQL_* environment variables to your test database credentials.');
42 $basedir = BasePath::create(dirname(__DIR__));
43 $configLoader = new Config\ConfigCacheLoader($basedir);
44 $config = Factory\ConfigFactory::createCache($configLoader);
49 getenv('MYSQL_USERNAME'),
50 getenv('MYSQL_PASSWORD'),
51 getenv('MYSQL_DATABASE'));
53 if (!DBA::connected()) {
54 $this->markTestSkipped('Could not connect to the database.');
57 return $this->createDefaultDBConnection(DBA::getConnection(), getenv('MYSQL_DATABASE'));
61 * Get dataset to populate the database with.
63 * @see https://phpunit.de/manual/5.7/en/database.html
65 protected function getDataSet()
67 return new YamlDataSet(__DIR__ . '/datasets/api.yml');