mirror of
https://git.friendi.ca/friendica/friendica.git
synced 2025-06-08 00:54:26 +02:00
Work with DatabaseException in UserDeletedRepository
This commit is contained in:
parent
2879db12dc
commit
e4a93fc9c7
5 changed files with 20 additions and 18 deletions
|
@ -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));
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -60,7 +60,7 @@ trait FixtureTestTrait
|
|||
|
||||
/** @var Database $dba */
|
||||
$dba = $this->dice->create(Database::class);
|
||||
$dba->setTestmode(true);
|
||||
$dba->throwExceptionsOnErrors(true);
|
||||
|
||||
DBStructure::checkInitialValues();
|
||||
|
||||
|
|
|
@ -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');
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue