]> git.mxchange.org Git - friendica.git/blob - tests/DatabaseTest.php
Fix Database connexion in 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          * Renames an eventually existing .htconfig.php to .htconfig.php.tmp
25          * Creates a new .htconfig.php for bin/worker.php execution
26          */
27         public static function setUpBeforeClass()
28         {
29                 parent::setUpBeforeClass();
30
31                 $base_config_file_name = 'htconfig.php';
32                 $config_file_name = '.htconfig.php';
33
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';
37
38                 if (file_exists($config_file_path)) {
39                         rename($config_file_path, $config_file_path_tmp);
40                 }
41
42                 $config_string = file_get_contents($base_config_file_path);
43
44                 $config_string = str_replace('die(', '// die(', $config_string);
45
46                 file_put_contents($config_file_path, $config_string);
47         }
48
49         /**
50          * Delete the created .htconfig.php
51          * Renames an eventually existing .htconfig.php.tmp to .htconfig.php
52          */
53         public static function tearDownAfterClass()
54         {
55                 $base_config_file_name = 'htconfig.php';
56                 $config_file_name = '.htconfig.php';
57
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';
61
62                 if (file_exists($config_file_path)) {
63                         unlink($config_file_path);
64                 }
65
66                 if (file_exists($config_file_path_tmp)) {
67                         rename($config_file_path_tmp, $config_file_path);
68                 }
69         }
70
71         /**
72          * Get database connection.
73          *
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.
76          *
77          * If it could not connect to the database, the test is skipped.
78          *
79          * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
80          * @see https://phpunit.de/manual/5.7/en/database.html
81          */
82         protected function getConnection()
83         {
84                 if (!dba::connected()) {
85                         $this->markTestSkipped('Could not connect to the database.');
86                 }
87
88                 return $this->createDefaultDBConnection(dba::get_db(), getenv('MYSQL_DATABASE'));
89         }
90
91         /**
92          * Get dataset to populate the database with.
93          * @return YamlDataSet
94          * @see https://phpunit.de/manual/5.7/en/database.html
95          */
96         protected function getDataSet()
97         {
98                 return new YamlDataSet(__DIR__ . '/datasets/api.yml');
99         }
100 }