]> git.mxchange.org Git - friendica.git/blob - tests/DatabaseTest.php
98f79e351b5b9f008a24c86d2b357a4a7f8d7960
[friendica.git] / tests / DatabaseTest.php
1 <?php
2 /**
3  * DatabaseTest class.
4  */
5
6 namespace Friendica\Test;
7
8 use PDO;
9 use PHPUnit\DbUnit\DataSet\YamlDataSet;
10 use PHPUnit\DbUnit\TestCaseTrait;
11 use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
12
13 /**
14  * Abstract class used by tests that need a database.
15  */
16 abstract class DatabaseTest extends MockedTest
17 {
18         use TestCaseTrait;
19
20         // only instantiate pdo once for test clean-up/fixture load
21         static private $pdo = null;
22
23         // only instantiate PHPUnit_Extensions_Database_DB_IDatabaseConnection once per test
24         private $conn = null;
25
26         /**
27          * Get database connection.
28          *
29          * This function is executed before each test in order to get a database connection that can be used by tests.
30          * If no prior connection is available, it tries to create one using the USER, PASS and DB environment variables.
31          *
32          * If it could not connect to the database, the test is skipped.
33          *
34          * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
35          * @see https://phpunit.de/manual/5.7/en/database.html
36          */
37         protected function getConnection()
38         {
39                 $server = $_SERVER;
40
41                 if ($this->conn === null) {
42                         if (self::$pdo == null) {
43
44                                 if (!empty($server['MYSQL_HOST'])
45                                     && !empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER']))
46                                     && $server['MYSQL_PASSWORD'] !== false
47                                     && !empty($server['MYSQL_DATABASE'])) {
48
49                                         $connect = "mysql:host=" . $server['MYSQL_HOST'] . ";dbname=" . $server['MYSQL_DATABASE'];
50
51                                         if (!empty($server['MYSQL_PORT'])) {
52                                                 $connect .= ";port=" . $server['MYSQL_PORT'];
53                                         }
54
55                                         if (!empty($server['MYSQL_USERNAME'])) {
56                                                 $db_user = $server['MYSQL_USERNAME'];
57                                         } else {
58                                                 $db_user = $server['MYSQL_USER'];
59                                         }
60
61                                         $db_pass = (string)$server['MYSQL_PASSWORD'];
62
63                                         self::$pdo = @new PDO($connect, $db_user, $db_pass);
64                                         self::$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
65                                 }
66                         }
67                         $this->conn = $this->createDefaultDBConnection(self::$pdo, getenv('MYSQL_DATABASE'));
68                 }
69
70                 return $this->conn;
71         }
72
73         /**
74          * Get dataset to populate the database with.
75          *
76          * @return YamlDataSet
77          * @see https://phtablepunit.de/manual/5.7/en/database.html
78          */
79         protected function getDataSet()
80         {
81                 return new YamlDataSet(__DIR__ . '/datasets/api.yml');
82         }
83 }