]> git.mxchange.org Git - friendica.git/blob - tests/DatabaseTest.php
Merge pull request #7451 from annando/ap-contact-id
[friendica.git] / tests / DatabaseTest.php
1 <?php
2 /**
3  * DatabaseTest class.
4  */
5
6 namespace Friendica\Test;
7
8 use Friendica\Database\Database;
9 use Friendica\Test\Util\Database\StaticDatabase;
10
11 /**
12  * Abstract class used by tests that need a database.
13  */
14 abstract class DatabaseTest extends MockedTest
15 {
16         protected function setUp()
17         {
18                 parent::setUp();
19
20                 StaticDatabase::statConnect($_SERVER);
21                 // Rollbacks every DB usage (in case the test couldn't call tearDown)
22                 StaticDatabase::statRollback();
23                 // Start the first, outer transaction
24                 StaticDatabase::getGlobConnection()->beginTransaction();
25         }
26
27         protected function tearDown()
28         {
29                 // Rollbacks every DB usage so we don't commit anything into the DB
30                 StaticDatabase::statRollback();
31
32                 parent::tearDown();
33         }
34
35         /**
36          * Loads a given DB fixture for this DB test
37          *
38          * @param string   $fixture The path to the fixture
39          * @param Database $dba     The DB connection
40          *
41          * @throws \Exception
42          */
43         protected function loadFixture(string $fixture, Database $dba)
44         {
45                 $this->assertFileExists($fixture);
46
47                 $data = include $fixture;
48
49                 foreach ($data as $tableName => $rows) {
50                         if (!is_array($rows)) {
51                                 $dba->p('TRUNCATE TABLE `' . $tableName . '``');
52                                 continue;
53                         }
54
55                         foreach ($rows as $row) {
56                                 $dba->insert($tableName, $row);
57                         }
58                 }
59         }
60 }