]> git.mxchange.org Git - friendica.git/blob - tests/DatabaseTest.php
moved get_guid to System::createGUID
[friendica.git] / tests / DatabaseTest.php
1 <?php
2 /**
3  * DatabaseTest class.
4  */
5
6 namespace Friendica\Test;
7
8 use dba;
9 use Friendica\App;
10 use Friendica\Core\Config;
11 use Friendica\Database\DBStructure;
12 use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
13 use PHPUnit\DbUnit\DataSet\YamlDataSet;
14 use PHPUnit\DbUnit\TestCaseTrait;
15 use PHPUnit\Framework\TestCase;
16
17 /**
18  * Abstract class used by tests that need a database.
19  */
20 abstract class DatabaseTest extends TestCase
21 {
22         /**
23          * @var \Friendica\App
24          */
25         protected $app;
26
27         use TestCaseTrait;
28
29         /**
30          * Creates basic instances for testing with databases
31          *
32          * @throws \Exception
33          */
34         protected function setUp()
35         {
36                 global $a;
37                 parent::setUp();
38
39                 // Reusable App object
40                 $this->app = new App(__DIR__.'/../');
41                 $a = $this->app;
42
43                 // Default config
44                 Config::set('config', 'hostname', 'localhost');
45                 Config::set('system', 'throttle_limit_day', 100);
46                 Config::set('system', 'throttle_limit_week', 100);
47                 Config::set('system', 'throttle_limit_month', 100);
48                 Config::set('system', 'theme', 'system_theme');
49         }
50
51         /**
52          * Renames an eventually existing .htconfig.php to .htconfig.php.tmp
53          * Creates a new .htconfig.php for bin/worker.php execution
54          */
55         public static function setUpBeforeClass()
56         {
57                 parent::setUpBeforeClass();
58
59                 $base_config_file_name = 'htconfig.php';
60                 $config_file_name = '.htconfig.php';
61
62                 $base_config_file_path = stream_resolve_include_path($base_config_file_name);
63                 $config_file_path = dirname($base_config_file_path) . DIRECTORY_SEPARATOR . $config_file_name;
64                 $config_file_path_tmp = $config_file_path . '.tmp';
65
66                 if (file_exists($config_file_path)) {
67                         rename($config_file_path, $config_file_path_tmp);
68                 }
69
70                 $config_string = file_get_contents($base_config_file_path);
71
72                 $config_string = str_replace('die(', '// die(', $config_string);
73
74                 file_put_contents($config_file_path, $config_string);
75         }
76
77         /**
78          * Delete the created .htconfig.php
79          * Renames an eventually existing .htconfig.php.tmp to .htconfig.php
80          */
81         public static function tearDownAfterClass()
82         {
83                 $base_config_file_name = 'htconfig.php';
84                 $config_file_name = '.htconfig.php';
85
86                 $base_config_file_path = stream_resolve_include_path($base_config_file_name);
87                 $config_file_path = dirname($base_config_file_path) . DIRECTORY_SEPARATOR . $config_file_name;
88                 $config_file_path_tmp = $config_file_path . '.tmp';
89
90                 if (file_exists($config_file_path)) {
91                         unlink($config_file_path);
92                 }
93
94                 if (file_exists($config_file_path_tmp)) {
95                         rename($config_file_path_tmp, $config_file_path);
96                 }
97         }
98
99         /**
100          * Get database connection.
101          *
102          * This function is executed before each test in order to get a database connection that can be used by tests.
103          * If no prior connection is available, it tries to create one using the USER, PASS and DB environment variables.
104          *
105          * If it could not connect to the database, the test is skipped.
106          *
107          * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
108          * @see https://phpunit.de/manual/5.7/en/database.html
109          */
110         protected function getConnection()
111         {
112                 if (!dba::$connected) {
113                         dba::connect(getenv('MYSQL_HOST') . ':' . getenv('MYSQL_PORT'), getenv('MYSQL_USERNAME'), getenv('MYSQL_PASSWORD'), getenv('MYSQL_DATABASE'));
114
115                         if (dba::$connected) {
116                                 $app = get_app();
117                                 // We need to do this in order to disable logging
118                                 $app->mode = \Friendica\App::MODE_INSTALL;
119
120                                 // Create database structure
121                                 DBStructure::update(false, true, true);
122
123                                 $app->mode = \Friendica\App::MODE_NORMAL;
124                         } else {
125                                 $this->markTestSkipped('Could not connect to the database. Please check the MYSQL_* environment variables.');
126                         }
127                 }
128
129                 return $this->createDefaultDBConnection(dba::get_db(), getenv('MYSQL_DATABASE'));
130         }
131
132         /**
133          * Get dataset to populate the database with.
134          * @return YamlDataSet
135          * @see https://phpunit.de/manual/5.7/en/database.html
136          */
137         protected function getDataSet()
138         {
139                 return new YamlDataSet(__DIR__ . '/datasets/api.yml');
140         }
141 }