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\Logger\VoidLogger;
14 use Friendica\Util\Profiler;
15 use PHPUnit\DbUnit\DataSet\YamlDataSet;
16 use PHPUnit\DbUnit\TestCaseTrait;
17 use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
19 require_once __DIR__ . '/../boot.php';
22 * Abstract class used by tests that need a database.
24 abstract class DatabaseTest extends MockedTest
29 * Get database connection.
31 * This function is executed before each test in order to get a database connection that can be used by tests.
32 * If no prior connection is available, it tries to create one using the USER, PASS and DB environment variables.
34 * If it could not connect to the database, the test is skipped.
36 * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
37 * @see https://phpunit.de/manual/5.7/en/database.html
39 protected function getConnection()
41 if (!getenv('MYSQL_DATABASE')) {
42 $this->markTestSkipped('Please set the MYSQL_* environment variables to your test database credentials.');
45 $basePath = BasePath::create(dirname(__DIR__));
46 $mode = new App\Mode($basePath);
47 $configLoader = new ConfigFileLoader($basePath, $mode);
48 $config = Factory\ConfigFactory::createCache($configLoader);
50 $profiler = \Mockery::mock(Profiler::class);
57 getenv('MYSQL_USERNAME'),
58 getenv('MYSQL_PASSWORD'),
59 getenv('MYSQL_DATABASE'));
61 if (!DBA::connected()) {
62 $this->markTestSkipped('Could not connect to the database.');
65 return $this->createDefaultDBConnection(DBA::getConnection(), getenv('MYSQL_DATABASE'));
69 * Get dataset to populate the database with.
71 * @see https://phpunit.de/manual/5.7/en/database.html
73 protected function getDataSet()
75 return new YamlDataSet(__DIR__ . '/datasets/api.yml');