6 namespace Friendica\Test;
8 use Friendica\App\Mode;
9 use Friendica\App\Router;
10 use Friendica\Core\Config\Cache\ConfigCache;
11 use Friendica\Database\Database;
12 use Friendica\Factory\ConfigFactory;
13 use Friendica\Factory\DBFactory;
14 use Friendica\Factory\ProfilerFactory;
15 use Friendica\Util\BasePath;
16 use Friendica\Util\Config\ConfigFileLoader;
17 use Friendica\Util\Profiler;
18 use PHPUnit\DbUnit\DataSet\YamlDataSet;
19 use PHPUnit\DbUnit\TestCaseTrait;
20 use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
22 require_once __DIR__ . '/../boot.php';
25 * Abstract class used by tests that need a database.
27 abstract class DatabaseTest extends MockedTest
32 protected static $dba;
35 protected static $basePath;
38 protected static $mode;
40 /** @var ConfigCache */
41 protected static $configCache;
44 protected static $profiler;
46 public static function setUpBeforeClass()
48 parent::setUpBeforeClass();
50 self::$basePath = BasePath::create(dirname(__DIR__));
51 self::$mode = new Mode(self::$basePath);
52 $configLoader = new ConfigFileLoader(self::$basePath, self::$mode);
53 self::$configCache = ConfigFactory::createCache($configLoader);
54 self::$profiler = ProfilerFactory::create(self::$configCache);
55 self::$dba = DBFactory::init(self::$configCache, self::$profiler, $_SERVER);
59 * Get database connection.
61 * This function is executed before each test in order to get a database connection that can be used by tests.
62 * If no prior connection is available, it tries to create one using the USER, PASS and DB environment variables.
64 * If it could not connect to the database, the test is skipped.
66 * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
67 * @see https://phpunit.de/manual/5.7/en/database.html
69 protected function getConnection()
71 if (!getenv('MYSQL_DATABASE')) {
72 $this->markTestSkipped('Please set the MYSQL_* environment variables to your test database credentials.');
75 if (!self::$dba->isConnected()) {
76 if (!self::$dba->connect()) {
77 $this->markTestSkipped('Could not connect to the database.');
81 return $this->createDefaultDBConnection(self::$dba->getConnection(), getenv('MYSQL_DATABASE'));
85 * Get dataset to populate the database with.
87 * @see https://phpunit.de/manual/5.7/en/database.html
89 protected function getDataSet()
91 return new YamlDataSet(__DIR__ . '/datasets/api.yml');