6 namespace Friendica\Test;
10 use Friendica\Core\Config;
11 use Friendica\Database\DBStructure;
12 use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
13 use PHPUnit\DbUnit\DataSet\YamlDataSet;
14 use PHPUnit\DbUnit\TestCaseTrait;
15 use PHPUnit\Framework\TestCase;
18 * Abstract class used by tests that need a database.
20 abstract class DatabaseTest extends TestCase
30 * Creates basic instances for testing with databases
34 protected function setUp()
39 // Reusable App object
40 $this->app = new App(__DIR__.'/../');
44 Config::set('config', 'hostname', 'localhost');
45 Config::set('system', 'throttle_limit_day', 100);
46 Config::set('system', 'throttle_limit_week', 100);
47 Config::set('system', 'throttle_limit_month', 100);
48 Config::set('system', 'theme', 'system_theme');
52 * Renames an eventually existing .htconfig.php to .htconfig.php.tmp
53 * Creates a new .htconfig.php for bin/worker.php execution
55 public static function setUpBeforeClass()
57 parent::setUpBeforeClass();
59 $base_config_file_name = 'htconfig.php';
60 $config_file_name = '.htconfig.php';
62 $base_config_file_path = stream_resolve_include_path($base_config_file_name);
63 $config_file_path = dirname($base_config_file_path) . DIRECTORY_SEPARATOR . $config_file_name;
64 $config_file_path_tmp = $config_file_path . '.tmp';
66 if (file_exists($config_file_path)) {
67 rename($config_file_path, $config_file_path_tmp);
70 $config_string = file_get_contents($base_config_file_path);
72 $config_string = str_replace('die(', '// die(', $config_string);
74 file_put_contents($config_file_path, $config_string);
78 * Delete the created .htconfig.php
79 * Renames an eventually existing .htconfig.php.tmp to .htconfig.php
81 public static function tearDownAfterClass()
83 $base_config_file_name = 'htconfig.php';
84 $config_file_name = '.htconfig.php';
86 $base_config_file_path = stream_resolve_include_path($base_config_file_name);
87 $config_file_path = dirname($base_config_file_path) . DIRECTORY_SEPARATOR . $config_file_name;
88 $config_file_path_tmp = $config_file_path . '.tmp';
90 if (file_exists($config_file_path)) {
91 unlink($config_file_path);
94 if (file_exists($config_file_path_tmp)) {
95 rename($config_file_path_tmp, $config_file_path);
100 * Get database connection.
102 * This function is executed before each test in order to get a database connection that can be used by tests.
103 * If no prior connection is available, it tries to create one using the USER, PASS and DB environment variables.
105 * If it could not connect to the database, the test is skipped.
107 * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
108 * @see https://phpunit.de/manual/5.7/en/database.html
110 protected function getConnection()
112 if (!dba::$connected) {
113 dba::connect(getenv('MYSQL_HOST') . ':' . getenv('MYSQL_PORT'), getenv('MYSQL_USERNAME'), getenv('MYSQL_PASSWORD'), getenv('MYSQL_DATABASE'));
115 if (dba::$connected) {
117 // We need to do this in order to disable logging
118 $app->mode = \Friendica\App::MODE_INSTALL;
120 // Create database structure
121 DBStructure::update(false, true, true);
123 $app->mode = \Friendica\App::MODE_NORMAL;
125 $this->markTestSkipped('Could not connect to the database. Please check the MYSQL_* environment variables.');
129 return $this->createDefaultDBConnection(dba::get_db(), getenv('MYSQL_DATABASE'));
133 * Get dataset to populate the database with.
134 * @return YamlDataSet
135 * @see https://phpunit.de/manual/5.7/en/database.html
137 protected function getDataSet()
139 return new YamlDataSet(__DIR__ . '/datasets/api.yml');