6 namespace Friendica\Test;
9 use Friendica\Database\DBStructure;
10 use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
11 use PHPUnit\DbUnit\DataSet\YamlDataSet;
12 use PHPUnit\DbUnit\TestCaseTrait;
13 use PHPUnit\Framework\TestCase;
16 * Abstract class used by tests that need a database.
18 abstract class DatabaseTest extends TestCase
24 * Renames an eventually existing .htconfig.php to .htconfig.php.tmp
25 * Creates a new .htconfig.php for bin/worker.php execution
27 public static function setUpBeforeClass()
29 parent::setUpBeforeClass();
31 $base_config_file_name = 'htconfig.php';
32 $config_file_name = '.htconfig.php';
34 $base_config_file_path = stream_resolve_include_path($base_config_file_name);
35 $config_file_path = dirname($base_config_file_path) . DIRECTORY_SEPARATOR . $config_file_name;
36 $config_file_path_tmp = $config_file_path . '.tmp';
38 if (file_exists($config_file_path)) {
39 rename($config_file_path, $config_file_path_tmp);
42 $config_string = file_get_contents($base_config_file_path);
44 $config_string = str_replace('die(', '// die(', $config_string);
46 file_put_contents($config_file_path, $config_string);
50 * Delete the created .htconfig.php
51 * Renames an eventually existing .htconfig.php.tmp to .htconfig.php
53 public static function tearDownAfterClass()
55 $base_config_file_name = 'htconfig.php';
56 $config_file_name = '.htconfig.php';
58 $base_config_file_path = stream_resolve_include_path($base_config_file_name);
59 $config_file_path = dirname($base_config_file_path) . DIRECTORY_SEPARATOR . $config_file_name;
60 $config_file_path_tmp = $config_file_path . '.tmp';
62 if (file_exists($config_file_path)) {
63 unlink($config_file_path);
66 if (file_exists($config_file_path_tmp)) {
67 rename($config_file_path_tmp, $config_file_path);
72 * Get database connection.
74 * This function is executed before each test in order to get a database connection that can be used by tests.
75 * If no prior connection is available, it tries to create one using the USER, PASS and DB environment variables.
77 * If it could not connect to the database, the test is skipped.
79 * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
80 * @see https://phpunit.de/manual/5.7/en/database.html
82 protected function getConnection()
84 if (!dba::$connected) {
85 dba::connect(getenv('MYSQL_HOST') . ':' . getenv('MYSQL_PORT'), getenv('MYSQL_USERNAME'), getenv('MYSQL_PASSWORD'), getenv('MYSQL_DATABASE'));
87 if (dba::$connected) {
89 // We need to do this in order to disable logging
90 $app->module = 'install';
92 // Create database structure
93 DBStructure::update(false, true, true);
95 $this->markTestSkipped('Could not connect to the database.');
99 return $this->createDefaultDBConnection(dba::get_db(), getenv('DB'));
103 * Get dataset to populate the database with.
104 * @return YamlDataSet
105 * @see https://phpunit.de/manual/5.7/en/database.html
107 protected function getDataSet()
109 return new YamlDataSet(__DIR__ . '/datasets/api.yml');