6 namespace Friendica\Test;
9 use PHPUnit\DbUnit\DataSet\YamlDataSet;
10 use PHPUnit\DbUnit\TestCaseTrait;
11 use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
14 * Abstract class used by tests that need a database.
16 abstract class DatabaseTest extends MockedTest
20 // only instantiate pdo once for test clean-up/fixture load
21 static private $pdo = null;
23 // only instantiate PHPUnit_Extensions_Database_DB_IDatabaseConnection once per test
27 * Get database connection.
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.
32 * If it could not connect to the database, the test is skipped.
34 * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
35 * @see https://phpunit.de/manual/5.7/en/database.html
37 protected function getConnection()
41 if ($this->conn === null) {
42 if (self::$pdo == null) {
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'])) {
49 $connect = "mysql:host=" . $server['MYSQL_HOST'] . ";dbname=" . $server['MYSQL_DATABASE'];
51 if (!empty($server['MYSQL_PORT'])) {
52 $connect .= ";port=" . $server['MYSQL_PORT'];
55 if (!empty($server['MYSQL_USERNAME'])) {
56 $db_user = $server['MYSQL_USERNAME'];
58 $db_user = $server['MYSQL_USER'];
61 $db_pass = (string)$server['MYSQL_PASSWORD'];
63 self::$pdo = @new PDO($connect, $db_user, $db_pass);
64 self::$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
67 $this->conn = $this->createDefaultDBConnection(self::$pdo, getenv('MYSQL_DATABASE'));
74 * Get dataset to populate the database with.
77 * @see https://phtablepunit.de/manual/5.7/en/database.html
79 protected function getDataSet()
81 return new YamlDataSet(__DIR__ . '/datasets/api.yml');