]> git.mxchange.org Git - friendica.git/blob - tests/DatabaseTestTrait.php
9922d7313f9fda7d2ca16486fb59d83fd47c009c
[friendica.git] / tests / DatabaseTestTrait.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Test;
23
24 use Friendica\Database\Database;
25 use Friendica\Database\DBStructure;
26 use Friendica\Test\Util\Database\StaticDatabase;
27
28 /**
29  * Abstract class used by tests that need a database.
30  */
31 trait DatabaseTestTrait
32 {
33         protected function setUp()
34         {
35                 StaticDatabase::statConnect($_SERVER);
36                 // Rollbacks every DB usage (in case the test couldn't call tearDown)
37                 StaticDatabase::statRollback();
38                 // Start the first, outer transaction
39                 StaticDatabase::getGlobConnection()->beginTransaction();
40
41                 $this->dba->setTestmode(true);
42
43                 DBStructure::checkInitialValues();
44
45                 parent::setUp();
46         }
47
48         protected function tearDown()
49         {
50                 // Rollbacks every DB usage so we don't commit anything into the DB
51                 StaticDatabase::statRollback();
52
53                 parent::tearDown();
54         }
55
56         /**
57          * Loads a given DB fixture for this DB test
58          *
59          * @param string   $fixture The path to the fixture
60          * @param Database $dba     The DB connection
61          *
62          * @throws \Exception
63          */
64         protected function loadFixture(string $fixture, Database $dba)
65         {
66                 $data = include $fixture;
67
68                 foreach ($data as $tableName => $rows) {
69                         if (is_numeric($tableName)) {
70                                 continue;
71                         }
72
73                         if (!is_array($rows)) {
74                                 $dba->p('TRUNCATE TABLE `' . $tableName . '``');
75                                 continue;
76                         }
77
78                         foreach ($rows as $row) {
79                                 $dba->insert($tableName, $row, true);
80                         }
81                 }
82         }
83 }