]> git.mxchange.org Git - friendica.git/blob - tests/DatabaseTest.php
Add .htconfig.php in test setUp
[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                 $config_string = file_get_contents($base_config_file_path);
37
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'));
43
44                 file_put_contents($config_file_path, $config_string);
45         }
46
47         /**
48          * Get database connection.
49          *
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.
52          *
53          * If it could not connect to the database, the test is skipped.
54          *
55          * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
56          * @see https://phpunit.de/manual/5.7/en/database.html
57          */
58         protected function getConnection()
59         {
60                 if (!dba::$connected) {
61                         dba::connect('localhost', getenv('USER'), getenv('PASS'), getenv('DB'));
62
63                         if (dba::$connected) {
64                                 $app = get_app();
65                                 // We need to do this in order to disable logging
66                                 $app->module = 'install';
67
68                                 // Create database structure
69                                 DBStructure::update(false, true, true);
70                         } else {
71                                 $this->markTestSkipped('Could not connect to the database.');
72                         }
73                 }
74
75                 return $this->createDefaultDBConnection(dba::get_db(), getenv('DB'));
76         }
77
78         /**
79          * Get dataset to populate the database with.
80          * @return YamlDataSet
81          * @see https://phpunit.de/manual/5.7/en/database.html
82          */
83         protected function getDataSet()
84         {
85                 return new YamlDataSet(__DIR__ . '/datasets/api.yml');
86         }
87 }