6 namespace Friendica\Test;
9 use Friendica\Database\DBA;
10 use Friendica\Factory;
11 use Friendica\Util\BasePath;
12 use Friendica\Util\Config\ConfigFileLoader;
13 use Friendica\Util\Profiler;
14 use PHPUnit\DbUnit\DataSet\YamlDataSet;
15 use PHPUnit\DbUnit\TestCaseTrait;
16 use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
18 require_once __DIR__ . '/../boot.php';
21 * Abstract class used by tests that need a database.
23 abstract class DatabaseTest extends MockedTest
28 * Get database connection.
30 * This function is executed before each test in order to get a database connection that can be used by tests.
31 * If no prior connection is available, it tries to create one using the USER, PASS and DB environment variables.
33 * If it could not connect to the database, the test is skipped.
35 * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
36 * @see https://phpunit.de/manual/5.7/en/database.html
38 protected function getConnection()
40 if (!getenv('MYSQL_DATABASE')) {
41 $this->markTestSkipped('Please set the MYSQL_* environment variables to your test database credentials.');
44 $basePath = BasePath::create(dirname(__DIR__));
45 $mode = new App\Mode($basePath);
46 $configLoader = new ConfigFileLoader($basePath, $mode);
47 $config = Factory\ConfigFactory::createCache($configLoader);
49 $profiler = \Mockery::mock(Profiler::class);
56 getenv('MYSQL_USERNAME'),
57 getenv('MYSQL_PASSWORD'),
58 getenv('MYSQL_DATABASE'));
60 if (!DBA::connected()) {
61 $this->markTestSkipped('Could not connect to the database.');
64 return $this->createDefaultDBConnection(DBA::getConnection(), getenv('MYSQL_DATABASE'));
68 * Get dataset to populate the database with.
70 * @see https://phpunit.de/manual/5.7/en/database.html
72 protected function getDataSet()
74 return new YamlDataSet(__DIR__ . '/datasets/api.yml');