]> git.mxchange.org Git - friendica.git/blob - tests/DatabaseTest.php
Use MYSQL_* environment variables for tests
[friendica.git] / tests / DatabaseTest.php
1 <?php
2 /**
3  * DatabaseTest class.
4  */
5
6 namespace Friendica\Test;
7
8 use dba;
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;
14
15 /**
16  * Abstract class used by tests that need a database.
17  */
18 abstract class DatabaseTest extends TestCase
19 {
20
21         use TestCaseTrait;
22
23         /**
24          * Creates .htconfig.php for bin/worker.php execution
25          */
26         protected function setUp()
27         {
28                 parent::setUp();
29
30                 $base_config_file_name = 'htconfig.php';
31                 $config_file_name = '.htconfig.php';
32
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;
35
36                 if (!file_exists($config_file_path)) {
37                         $config_string = file_get_contents($base_config_file_path);
38
39                         $config_string = str_replace('die(', '// die(', $config_string);
40
41                         file_put_contents($config_file_path, $config_string);
42                 }
43         }
44
45         /**
46          * Get database connection.
47          *
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.
50          *
51          * If it could not connect to the database, the test is skipped.
52          *
53          * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
54          * @see https://phpunit.de/manual/5.7/en/database.html
55          */
56         protected function getConnection()
57         {
58                 if (!dba::$connected) {
59                         dba::connect(getenv('MYSQL_HOST') . ':' . getenv('MYSQL_PORT'), getenv('MYSQL_USERNAME'), getenv('MYSQL_PASSWORD'), getenv('MYSQL_DATABASE'));
60
61                         if (dba::$connected) {
62                                 $app = get_app();
63                                 // We need to do this in order to disable logging
64                                 $app->module = 'install';
65
66                                 // Create database structure
67                                 DBStructure::update(false, true, true);
68                         } else {
69                                 $this->markTestSkipped('Could not connect to the database.');
70                         }
71                 }
72
73                 return $this->createDefaultDBConnection(dba::get_db(), getenv('DB'));
74         }
75
76         /**
77          * Get dataset to populate the database with.
78          * @return YamlDataSet
79          * @see https://phpunit.de/manual/5.7/en/database.html
80          */
81         protected function getDataSet()
82         {
83                 return new YamlDataSet(__DIR__ . '/datasets/api.yml');
84         }
85 }