From e4a93fc9c7e84ed529ed8033fb65212c6336b325 Mon Sep 17 00:00:00 2001 From: Art4 Date: Tue, 15 Apr 2025 14:48:01 +0000 Subject: [PATCH] Work with DatabaseException in UserDeletedRepository --- src/Database/Database.php | 10 +++++----- src/Database/Repository/UserDeletedRepository.php | 10 ++++++---- tests/FixtureTestTrait.php | 2 +- .../Repository/UserDeletedRepositoryTest.php | 14 +++++++------- tests/Util/CreateDatabaseTrait.php | 2 +- 5 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 6ee4394901..cfa40663e6 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -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)); } diff --git a/src/Database/Repository/UserDeletedRepository.php b/src/Database/Repository/UserDeletedRepository.php index 72f3c1cfe8..baa1ab5f2a 100644 --- a/src/Database/Repository/UserDeletedRepository.php +++ b/src/Database/Repository/UserDeletedRepository.php @@ -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); } } diff --git a/tests/FixtureTestTrait.php b/tests/FixtureTestTrait.php index 8c4508eeb8..e8ffd69b03 100644 --- a/tests/FixtureTestTrait.php +++ b/tests/FixtureTestTrait.php @@ -60,7 +60,7 @@ trait FixtureTestTrait /** @var Database $dba */ $dba = $this->dice->create(Database::class); - $dba->setTestmode(true); + $dba->throwExceptionsOnErrors(true); DBStructure::checkInitialValues(); diff --git a/tests/Unit/Database/Repository/UserDeletedRepositoryTest.php b/tests/Unit/Database/Repository/UserDeletedRepositoryTest.php index d4d1c793e9..80b706dd46 100644 --- a/tests/Unit/Database/Repository/UserDeletedRepositoryTest.php +++ b/tests/Unit/Database/Repository/UserDeletedRepositoryTest.php @@ -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'); } diff --git a/tests/Util/CreateDatabaseTrait.php b/tests/Util/CreateDatabaseTrait.php index 05d3d6c0ca..c6ad09ef94 100644 --- a/tests/Util/CreateDatabaseTrait.php +++ b/tests/Util/CreateDatabaseTrait.php @@ -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; }