mirror of
https://git.friendi.ca/friendica/friendica.git
synced 2025-06-08 04:14:27 +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;
|
private $affected_rows = 0;
|
||||||
protected $in_transaction = false;
|
protected $in_transaction = false;
|
||||||
protected $in_retrial = false;
|
protected $in_retrial = false;
|
||||||
protected $testmode = false;
|
private bool $throwExceptionsOnErrors = false;
|
||||||
private $relation = [];
|
private $relation = [];
|
||||||
/** @var DbaDefinition */
|
/** @var DbaDefinition */
|
||||||
protected $dbaDefinition;
|
protected $dbaDefinition;
|
||||||
|
@ -205,9 +205,9 @@ class Database
|
||||||
return $this->connected;
|
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;
|
$error = $this->error;
|
||||||
$errorno = $this->errorno;
|
$errorno = $this->errorno;
|
||||||
|
|
||||||
if ($this->testmode) {
|
if ($this->throwExceptionsOnErrors) {
|
||||||
throw new DatabaseException($error, $errorno, $this->replaceParameters($sql, $args));
|
throw new DatabaseException($error, $errorno, $this->replaceParameters($sql, $args));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -779,7 +779,7 @@ class Database
|
||||||
$error = $this->error;
|
$error = $this->error;
|
||||||
$errorno = $this->errorno;
|
$errorno = $this->errorno;
|
||||||
|
|
||||||
if ($this->testmode) {
|
if ($this->throwExceptionsOnErrors) {
|
||||||
throw new DatabaseException($error, $errorno, $this->replaceParameters($sql, $params));
|
throw new DatabaseException($error, $errorno, $this->replaceParameters($sql, $params));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,14 +28,16 @@ final class UserDeletedRepository
|
||||||
/**
|
/**
|
||||||
* Insert a deleted user by username.
|
* 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
|
public function insertByUsername(string $username): void
|
||||||
{
|
{
|
||||||
$result = $this->database->insert('userd', ['username' => $username]);
|
$this->database->throwExceptionsOnErrors(true);
|
||||||
|
|
||||||
if ($result === false) {
|
try {
|
||||||
throw new Exception(sprintf('Error while inserting username `%s` as deleted user.', $username));
|
$this->database->insert('userd', ['username' => $username]);
|
||||||
|
} finally {
|
||||||
|
$this->database->throwExceptionsOnErrors(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -60,7 +60,7 @@ trait FixtureTestTrait
|
||||||
|
|
||||||
/** @var Database $dba */
|
/** @var Database $dba */
|
||||||
$dba = $this->dice->create(Database::class);
|
$dba = $this->dice->create(Database::class);
|
||||||
$dba->setTestmode(true);
|
$dba->throwExceptionsOnErrors(true);
|
||||||
|
|
||||||
DBStructure::checkInitialValues();
|
DBStructure::checkInitialValues();
|
||||||
|
|
||||||
|
|
|
@ -9,14 +9,14 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Friendica\Test\Unit\Database\Repository;
|
namespace Friendica\Test\Unit\Database\Repository;
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use Friendica\Database\Database;
|
use Friendica\Database\Database;
|
||||||
|
use Friendica\Database\DatabaseException;
|
||||||
use Friendica\Database\Repository\UserDeletedRepository;
|
use Friendica\Database\Repository\UserDeletedRepository;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
class UserDeletedRepositoryTest extends TestCase
|
class UserDeletedRepositoryTest extends TestCase
|
||||||
{
|
{
|
||||||
public function testInsertByUsernameReturnsFalse(): void
|
public function testInsertByUsernameCallsDatabase(): void
|
||||||
{
|
{
|
||||||
$database = $this->createMock(Database::class);
|
$database = $this->createMock(Database::class);
|
||||||
$database->expects($this->once())->method('insert')->willReturnMap([
|
$database->expects($this->once())->method('insert')->willReturnMap([
|
||||||
|
@ -31,14 +31,14 @@ class UserDeletedRepositoryTest extends TestCase
|
||||||
public function testInsertByUsernameThrowsException(): void
|
public function testInsertByUsernameThrowsException(): void
|
||||||
{
|
{
|
||||||
$database = $this->createMock(Database::class);
|
$database = $this->createMock(Database::class);
|
||||||
$database->expects($this->once())->method('insert')->willReturnMap([
|
$database->expects($this->exactly(2))->method('throwExceptionsOnErrors');
|
||||||
['userd', ['username' => 'test'], 0, false],
|
$database->expects($this->once())->method('insert')->willThrowException(
|
||||||
]);
|
new DatabaseException('An error occured.', 0, 'SQL query')
|
||||||
|
);
|
||||||
|
|
||||||
$repo = new UserDeletedRepository($database);
|
$repo = new UserDeletedRepository($database);
|
||||||
|
|
||||||
$this->expectException(Exception::class);
|
$this->expectException(DatabaseException::class);
|
||||||
$this->expectExceptionMessage('Error while inserting username `test` as deleted user.');
|
|
||||||
|
|
||||||
$repo->insertByUsername('test');
|
$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 = new StaticDatabase($config, (new DbaDefinition($this->root->url()))->load(), (new ViewDefinition($this->root->url()))->load());
|
||||||
$database->setTestmode(true);
|
$database->throwExceptionsOnErrors(true);
|
||||||
|
|
||||||
return $database;
|
return $database;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue