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