3 * @copyright Copyright (C) 2010-2021, the Friendica project
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\Test\Util\Database\StaticDatabase;
28 * Abstract class used by tests that need a database.
30 trait DatabaseTestTrait
32 protected function setUpDb()
34 StaticDatabase::statConnect($_SERVER);
35 // Rollbacks every DB usage (in case the test couldn't call tearDown)
36 StaticDatabase::statRollback();
37 // Rollback the first, outer transaction just 2 be sure
38 StaticDatabase::getGlobConnection()->rollBack();
39 // Start the first, outer transaction
40 StaticDatabase::getGlobConnection()->beginTransaction();
43 protected function tearDownDb()
46 // Rollbacks every DB usage so we don't commit anything into the DB
47 StaticDatabase::statRollback();
48 } catch (\PDOException $exception) {
49 print_r("Found already rolled back transaction");
54 * Loads a given DB fixture for this DB test
56 * @param string $fixture The path to the fixture
57 * @param Database $dba The DB connection
61 protected function loadFixture(string $fixture, Database $dba)
63 $data = include $fixture;
65 foreach ($data as $tableName => $rows) {
66 if (is_numeric($tableName)) {
70 if (!is_array($rows)) {
71 $dba->p('TRUNCATE TABLE `' . $tableName . '``');
75 foreach ($rows as $row) {
76 $dba->insert($tableName, $row, true);