2019-08-03 14:48:56 -04:00
|
|
|
<?php
|
|
|
|
/**
|
2024-01-02 15:57:26 -05:00
|
|
|
* @copyright Copyright (C) 2010-2024, the Friendica project
|
2020-02-09 10:34:23 -05:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
2019-08-03 14:48:56 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Test;
|
|
|
|
|
|
|
|
use Friendica\Database\Database;
|
|
|
|
use Friendica\Test\Util\Database\StaticDatabase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract class used by tests that need a database.
|
|
|
|
*/
|
|
|
|
trait DatabaseTestTrait
|
|
|
|
{
|
2021-04-01 17:04:30 -04:00
|
|
|
protected function setUpDb()
|
2019-08-03 14:48:56 -04:00
|
|
|
{
|
|
|
|
StaticDatabase::statConnect($_SERVER);
|
|
|
|
// Rollbacks every DB usage (in case the test couldn't call tearDown)
|
|
|
|
StaticDatabase::statRollback();
|
2021-05-24 14:30:56 -04:00
|
|
|
// Rollback the first, outer transaction just 2 be sure
|
2024-02-24 23:46:54 -05:00
|
|
|
StaticDatabase::getGlobConnection()->rollback();
|
2019-08-03 14:48:56 -04:00
|
|
|
// Start the first, outer transaction
|
|
|
|
StaticDatabase::getGlobConnection()->beginTransaction();
|
|
|
|
}
|
|
|
|
|
2021-04-01 17:04:30 -04:00
|
|
|
protected function tearDownDb()
|
2019-08-03 14:48:56 -04:00
|
|
|
{
|
2021-04-01 17:04:30 -04:00
|
|
|
try {
|
|
|
|
// Rollbacks every DB usage so we don't commit anything into the DB
|
|
|
|
StaticDatabase::statRollback();
|
|
|
|
} catch (\PDOException $exception) {
|
|
|
|
print_r("Found already rolled back transaction");
|
|
|
|
}
|
2019-08-03 14:48:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads a given DB fixture for this DB test
|
|
|
|
*
|
2023-02-12 07:09:14 -05:00
|
|
|
* @param string[][] $fixture The fixture array
|
2019-08-03 14:48:56 -04:00
|
|
|
* @param Database $dba The DB connection
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2023-02-12 07:09:14 -05:00
|
|
|
protected function loadDirectFixture(array $fixture, Database $dba)
|
2019-08-03 14:48:56 -04:00
|
|
|
{
|
2023-02-12 07:09:14 -05:00
|
|
|
foreach ($fixture as $tableName => $rows) {
|
2020-05-15 11:30:43 -04:00
|
|
|
if (is_numeric($tableName)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_array($rows)) {
|
2021-10-12 01:53:29 -04:00
|
|
|
$dba->e('TRUNCATE TABLE `' . $tableName . '``');
|
2019-08-03 14:48:56 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($rows as $row) {
|
2023-02-13 14:52:24 -05:00
|
|
|
if (is_array($row)) {
|
|
|
|
$dba->insert($tableName, $row, true);
|
|
|
|
} else {
|
|
|
|
throw new \Exception('row isn\'t an array');
|
|
|
|
}
|
2019-08-03 14:48:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-12 07:09:14 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads a given DB fixture-file for this DB test
|
|
|
|
*
|
|
|
|
* @param string $fixture The path to the fixture
|
|
|
|
* @param Database $dba The DB connection
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
protected function loadFixture(string $fixture, Database $dba)
|
|
|
|
{
|
|
|
|
$data = include $fixture;
|
|
|
|
|
|
|
|
$this->loadDirectFixture($data, $dba);
|
|
|
|
}
|
2019-08-03 14:48:56 -04:00
|
|
|
}
|