3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Test;
24 use Friendica\Database\Database;
25 use Friendica\Database\DBStructure;
26 use Friendica\Test\Util\Database\StaticDatabase;
29 * Abstract class used by tests that need a database.
31 trait DatabaseTestTrait
33 protected function setUp()
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();
41 StaticDatabase::setTestmode(true);
43 DBStructure::checkInitialValues();
48 protected function tearDown()
50 // Rollbacks every DB usage so we don't commit anything into the DB
51 StaticDatabase::statRollback();
57 * Loads a given DB fixture for this DB test
59 * @param string $fixture The path to the fixture
60 * @param Database $dba The DB connection
64 protected function loadFixture(string $fixture, Database $dba)
66 $data = include $fixture;
68 foreach ($data as $tableName => $rows) {
69 if (is_numeric($tableName)) {
73 if (!is_array($rows)) {
74 $dba->p('TRUNCATE TABLE `' . $tableName . '``');
78 foreach ($rows as $row) {
79 $dba->insert($tableName, $row, true);