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 * Creates .htconfig.php for bin/worker.php execution
26 protected function setUp()
30 $base_config_file_name = 'htconfig.php';
31 $config_file_name = '.htconfig.php';
33 $base_config_file_path = stream_resolve_include_path($base_config_file_name);
34 $config_file_path = dirname($base_config_file_path) . DIRECTORY_SEPARATOR . $config_file_name;
36 $config_string = file_get_contents($base_config_file_path);
38 $config_string = str_replace('die(', '// die(');
39 $config_string = str_replace('your.mysqlhost.com', 'localhost');
40 $config_string = str_replace('mysqlusername' , getenv('USER'));
41 $config_string = str_replace('mysqlpassword' , getenv('PASS'));
42 $config_string = str_replace('mysqldatabasename' , getenv('DB'));
44 file_put_contents($config_file_path, $config_string);
48 * Get database connection.
50 * This function is executed before each test in order to get a database connection that can be used by tests.
51 * If no prior connection is available, it tries to create one using the USER, PASS and DB environment variables.
53 * If it could not connect to the database, the test is skipped.
55 * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
56 * @see https://phpunit.de/manual/5.7/en/database.html
58 protected function getConnection()
60 if (!dba::$connected) {
61 dba::connect('localhost', getenv('USER'), getenv('PASS'), getenv('DB'));
63 if (dba::$connected) {
65 // We need to do this in order to disable logging
66 $app->module = 'install';
68 // Create database structure
69 DBStructure::update(false, true, true);
71 $this->markTestSkipped('Could not connect to the database.');
75 return $this->createDefaultDBConnection(dba::get_db(), getenv('DB'));
79 * Get dataset to populate the database with.
81 * @see https://phpunit.de/manual/5.7/en/database.html
83 protected function getDataSet()
85 return new YamlDataSet(__DIR__ . '/datasets/api.yml');