6 namespace Friendica\Test;
8 use Friendica\App\Mode;
9 use Friendica\Core\Config\Cache\ConfigCache;
10 use Friendica\Database\Database;
11 use Friendica\Factory\ConfigFactory;
12 use Friendica\Util\BasePath;
13 use Friendica\Util\ConfigFileLoader;
14 use Friendica\Util\Profiler;
15 use PHPUnit\DbUnit\DataSet\YamlDataSet;
16 use PHPUnit\DbUnit\TestCaseTrait;
17 use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
18 use Psr\Log\NullLogger;
20 require_once __DIR__ . '/../boot.php';
23 * Abstract class used by tests that need a database.
25 abstract class DatabaseTest extends MockedTest
30 protected static $dba;
33 protected static $basePath;
36 protected static $mode;
38 /** @var ConfigCache */
39 protected static $configCache;
42 protected static $profiler;
44 public static function setUpBeforeClass()
46 parent::setUpBeforeClass();
48 self::$basePath = new BasePath(dirname(__DIR__));
49 $configLoader = new ConfigFileLoader(self::$basePath->getPath());
50 $configFactory = new ConfigFactory();
51 self::$configCache = $configFactory->createCache($configLoader);
52 self::$profiler = new Profiler(self::$configCache);
53 self::$dba = new Database(self::$configCache, self::$profiler, new NullLogger(), $_SERVER);
54 self::$mode = new Mode(self::$basePath, self::$dba, self::$configCache);
58 * Get database connection.
60 * This function is executed before each test in order to get a database connection that can be used by tests.
61 * If no prior connection is available, it tries to create one using the USER, PASS and DB environment variables.
63 * If it could not connect to the database, the test is skipped.
65 * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
66 * @see https://phpunit.de/manual/5.7/en/database.html
68 protected function getConnection()
70 if (!getenv('MYSQL_DATABASE')) {
71 $this->markTestSkipped('Please set the MYSQL_* environment variables to your test database credentials.');
74 if (!self::$dba->isConnected()) {
75 if (!self::$dba->connect()) {
76 $this->markTestSkipped('Could not connect to the database.');
80 return $this->createDefaultDBConnection(self::$dba->getConnection(), getenv('MYSQL_DATABASE'));
84 * Get dataset to populate the database with.
86 * @see https://phpunit.de/manual/5.7/en/database.html
88 protected function getDataSet()
90 return new YamlDataSet(__DIR__ . '/datasets/api.yml');