]> git.mxchange.org Git - friendica.git/blob - tests/DatabaseTestTrait.php
spelling: occurred
[friendica.git] / tests / DatabaseTestTrait.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
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\Test\Util\Database\StaticDatabase;
26
27 /**
28  * Abstract class used by tests that need a database.
29  */
30 trait DatabaseTestTrait
31 {
32         protected function setUpDb()
33         {
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();
41         }
42
43         protected function tearDownDb()
44         {
45                 try {
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");
50                 }
51         }
52
53         /**
54          * Loads a given DB fixture for this DB test
55          *
56          * @param string[][] $fixture The fixture array
57          * @param Database $dba     The DB connection
58          *
59          * @throws \Exception
60          */
61         protected function loadDirectFixture(array $fixture, Database $dba)
62         {
63                 foreach ($fixture as $tableName => $rows) {
64                         if (is_numeric($tableName)) {
65                                 continue;
66                         }
67
68                         if (!is_array($rows)) {
69                                 $dba->e('TRUNCATE TABLE `' . $tableName . '``');
70                                 continue;
71                         }
72
73                         foreach ($rows as $row) {
74                                 if (is_array($row)) {
75                                         $dba->insert($tableName, $row, true);
76                                 } else {
77                                         throw new \Exception('row isn\'t an array');
78                                 }
79                         }
80                 }
81         }
82
83         /**
84          * Loads a given DB fixture-file for this DB test
85          *
86          * @param string   $fixture The path to the fixture
87          * @param Database $dba     The DB connection
88          *
89          * @throws \Exception
90          */
91         protected function loadFixture(string $fixture, Database $dba)
92         {
93                 $data = include $fixture;
94
95                 $this->loadDirectFixture($data, $dba);
96         }
97 }