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\Factory\DBFactory;
13 use Friendica\Factory\ProfilerFactory;
14 use Friendica\Util\BasePath;
15 use Friendica\Util\ConfigFileLoader;
16 use Friendica\Util\Profiler;
17 use PHPUnit\DbUnit\DataSet\YamlDataSet;
18 use PHPUnit\DbUnit\TestCaseTrait;
19 use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
21 require_once __DIR__ . '/../boot.php';
24 * Abstract class used by tests that need a database.
26 abstract class DatabaseTest extends MockedTest
31 protected static $dba;
34 protected static $basePath;
37 protected static $mode;
39 /** @var ConfigCache */
40 protected static $configCache;
43 protected static $profiler;
45 public static function setUpBeforeClass()
47 parent::setUpBeforeClass();
49 self::$basePath = BasePath::create(dirname(__DIR__));
50 self::$mode = new Mode(self::$basePath);
51 $configLoader = new ConfigFileLoader(self::$basePath, self::$mode);
52 self::$configCache = ConfigFactory::createCache($configLoader);
53 self::$profiler = ProfilerFactory::create(self::$configCache);
54 self::$dba = DBFactory::init(self::$configCache, self::$profiler, $_SERVER);
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');