]> git.mxchange.org Git - friendica.git/blob - tests/DatabaseTest.php
Code standards
[friendica.git] / tests / DatabaseTest.php
1 <?php
2 /**
3  * DatabaseTest class.
4  */
5
6 namespace Friendica\Test;
7
8 use Friendica\App;
9 use Friendica\BaseObject;
10 use Friendica\Core\Config;
11 use Friendica\Database\DBA;
12 use PHPUnit\DbUnit\DataSet\YamlDataSet;
13 use PHPUnit\DbUnit\TestCaseTrait;
14 use PHPUnit\Framework\TestCase;
15 use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
16
17 require_once(__DIR__ . '/../boot.php');
18
19 /**
20  * Abstract class used by tests that need a database.
21  */
22 abstract class DatabaseTest extends TestCase
23 {
24         use TestCaseTrait;
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                 if (!getenv('MYSQL_DATABASE')) {
40                         $this->markTestSkipped('Please set the MYSQL_* environment variables to your test database credentials.');
41                 }
42
43                 DBA::connect(getenv('MYSQL_HOST'),
44                         getenv('MYSQL_USERNAME'),
45                         getenv('MYSQL_PASSWORD'),
46                         getenv('MYSQL_DATABASE'));
47
48                 if (!DBA::connected()) {
49                         $this->markTestSkipped('Could not connect to the database.');
50                 }
51
52                 return $this->createDefaultDBConnection(DBA::getConnection(), getenv('MYSQL_DATABASE'));
53         }
54
55         /**
56          * Get dataset to populate the database with.
57          * @return YamlDataSet
58          * @see https://phpunit.de/manual/5.7/en/database.html
59          */
60         protected function getDataSet()
61         {
62                 return new YamlDataSet(__DIR__ . '/datasets/api.yml');
63         }
64 }