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 if (!file_exists($config_file_path)) {
37 $config_string = file_get_contents($base_config_file_path);
39 $config_string = str_replace('die(', '// die(', $config_string);
41 file_put_contents($config_file_path, $config_string);
46 * Get database connection.
48 * This function is executed before each test in order to get a database connection that can be used by tests.
49 * If no prior connection is available, it tries to create one using the USER, PASS and DB environment variables.
51 * If it could not connect to the database, the test is skipped.
53 * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
54 * @see https://phpunit.de/manual/5.7/en/database.html
56 protected function getConnection()
58 if (!dba::$connected) {
59 dba::connect(getenv('MYSQL_HOST') . ':' . getenv('MYSQL_PORT'), getenv('MYSQL_USERNAME'), getenv('MYSQL_PASSWORD'), getenv('MYSQL_DATABASE'));
61 if (dba::$connected) {
63 // We need to do this in order to disable logging
64 $app->module = 'install';
66 // Create database structure
67 DBStructure::update(false, true, true);
69 $this->markTestSkipped('Could not connect to the database.');
73 return $this->createDefaultDBConnection(dba::get_db(), getenv('DB'));
77 * Get dataset to populate the database with.
79 * @see https://phpunit.de/manual/5.7/en/database.html
81 protected function getDataSet()
83 return new YamlDataSet(__DIR__ . '/datasets/api.yml');