2019-08-03 20:48:56 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2024-01-02 21:57:26 +01:00
|
|
|
* @copyright Copyright (C) 2010-2024, the Friendica project
|
2020-02-09 16:34:23 +01: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 20:48:56 +02: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 23:04:30 +02:00
|
|
|
protected function setUpDb()
|
2019-08-03 20:48:56 +02:00
|
|
|
{
|
|
|
|
StaticDatabase::statConnect($_SERVER);
|
|
|
|
// Rollbacks every DB usage (in case the test couldn't call tearDown)
|
|
|
|
StaticDatabase::statRollback();
|
2021-05-24 20:30:56 +02:00
|
|
|
// Rollback the first, outer transaction just 2 be sure
|
|
|
|
StaticDatabase::getGlobConnection()->rollBack();
|
2019-08-03 20:48:56 +02:00
|
|
|
// Start the first, outer transaction
|
|
|
|
StaticDatabase::getGlobConnection()->beginTransaction();
|
|
|
|
}
|
|
|
|
|
2021-04-01 23:04:30 +02:00
|
|
|
protected function tearDownDb()
|
2019-08-03 20:48:56 +02:00
|
|
|
{
|
2021-04-01 23:04:30 +02: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 20:48:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads a given DB fixture for this DB test
|
|
|
|
*
|
2023-02-12 13:09:14 +01:00
|
|
|
* @param string[][] $fixture The fixture array
|
2019-08-03 20:48:56 +02:00
|
|
|
* @param Database $dba The DB connection
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2023-02-12 13:09:14 +01:00
|
|
|
protected function loadDirectFixture(array $fixture, Database $dba)
|
2019-08-03 20:48:56 +02:00
|
|
|
{
|
2023-02-12 13:09:14 +01:00
|
|
|
foreach ($fixture as $tableName => $rows) {
|
2020-05-15 17:30:43 +02:00
|
|
|
if (is_numeric($tableName)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_array($rows)) {
|
2021-10-12 07:53:29 +02:00
|
|
|
$dba->e('TRUNCATE TABLE `' . $tableName . '``');
|
2019-08-03 20:48:56 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($rows as $row) {
|
2023-02-13 20:52:24 +01:00
|
|
|
if (is_array($row)) {
|
|
|
|
$dba->insert($tableName, $row, true);
|
|
|
|
} else {
|
|
|
|
throw new \Exception('row isn\'t an array');
|
|
|
|
}
|
2019-08-03 20:48:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-12 13:09:14 +01: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 20:48:56 +02:00
|
|
|
}
|