Work with DatabaseException in UserDeletedRepository

This commit is contained in:
Art4 2025-04-15 14:48:01 +00:00
parent 2879db12dc
commit e4a93fc9c7
5 changed files with 20 additions and 18 deletions

View file

@ -69,7 +69,7 @@ class Database
private $affected_rows = 0;
protected $in_transaction = false;
protected $in_retrial = false;
protected $testmode = false;
private bool $throwExceptionsOnErrors = false;
private $relation = [];
/** @var DbaDefinition */
protected $dbaDefinition;
@ -205,9 +205,9 @@ class Database
return $this->connected;
}
public function setTestmode(bool $test)
public function throwExceptionsOnErrors(bool $throwExceptions): void
{
$this->testmode = $test;
$this->throwExceptionsOnErrors = $throwExceptions;
}
/**
@ -672,7 +672,7 @@ class Database
$error = $this->error;
$errorno = $this->errorno;
if ($this->testmode) {
if ($this->throwExceptionsOnErrors) {
throw new DatabaseException($error, $errorno, $this->replaceParameters($sql, $args));
}
@ -779,7 +779,7 @@ class Database
$error = $this->error;
$errorno = $this->errorno;
if ($this->testmode) {
if ($this->throwExceptionsOnErrors) {
throw new DatabaseException($error, $errorno, $this->replaceParameters($sql, $params));
}

View file

@ -28,14 +28,16 @@ final class UserDeletedRepository
/**
* Insert a deleted user by username.
*
* @throws \Exception If the username could not be inserted
* @throws DatabaseException If the username could not be inserted
*/
public function insertByUsername(string $username): void
{
$result = $this->database->insert('userd', ['username' => $username]);
$this->database->throwExceptionsOnErrors(true);
if ($result === false) {
throw new Exception(sprintf('Error while inserting username `%s` as deleted user.', $username));
try {
$this->database->insert('userd', ['username' => $username]);
} finally {
$this->database->throwExceptionsOnErrors(false);
}
}

View file

@ -60,7 +60,7 @@ trait FixtureTestTrait
/** @var Database $dba */
$dba = $this->dice->create(Database::class);
$dba->setTestmode(true);
$dba->throwExceptionsOnErrors(true);
DBStructure::checkInitialValues();

View file

@ -9,14 +9,14 @@ declare(strict_types=1);
namespace Friendica\Test\Unit\Database\Repository;
use Exception;
use Friendica\Database\Database;
use Friendica\Database\DatabaseException;
use Friendica\Database\Repository\UserDeletedRepository;
use PHPUnit\Framework\TestCase;
class UserDeletedRepositoryTest extends TestCase
{
public function testInsertByUsernameReturnsFalse(): void
public function testInsertByUsernameCallsDatabase(): void
{
$database = $this->createMock(Database::class);
$database->expects($this->once())->method('insert')->willReturnMap([
@ -31,14 +31,14 @@ class UserDeletedRepositoryTest extends TestCase
public function testInsertByUsernameThrowsException(): void
{
$database = $this->createMock(Database::class);
$database->expects($this->once())->method('insert')->willReturnMap([
['userd', ['username' => 'test'], 0, false],
]);
$database->expects($this->exactly(2))->method('throwExceptionsOnErrors');
$database->expects($this->once())->method('insert')->willThrowException(
new DatabaseException('An error occured.', 0, 'SQL query')
);
$repo = new UserDeletedRepository($database);
$this->expectException(Exception::class);
$this->expectExceptionMessage('Error while inserting username `test` as deleted user.');
$this->expectException(DatabaseException::class);
$repo->insertByUsername('test');
}

View file

@ -43,7 +43,7 @@ trait CreateDatabaseTrait
]));
$database = new StaticDatabase($config, (new DbaDefinition($this->root->url()))->load(), (new ViewDefinition($this->root->url()))->load());
$database->setTestmode(true);
$database->throwExceptionsOnErrors(true);
return $database;
}