From 99c8e4c03cd68c656e593dd15aac87dc340307dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D?= Date: Wed, 1 May 2024 18:39:28 +0200 Subject: [PATCH] chore(tests): UT for UpgradeDatabase MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rubén D --- .../Upgrade/Services/UpgradeDatabase.php | 22 ++-- lib/SP/Infrastructure/Database/Database.php | 29 +++++ schemas/40024210101.sql | 8 +- .../Upgrade/Services/UpgradeDatabaseTest.php | 121 ++++++++++++++++++ 4 files changed, 167 insertions(+), 13 deletions(-) create mode 100644 tests/SP/Domain/Upgrade/Services/UpgradeDatabaseTest.php diff --git a/lib/SP/Domain/Upgrade/Services/UpgradeDatabase.php b/lib/SP/Domain/Upgrade/Services/UpgradeDatabase.php index 6b10d884..e59d409d 100644 --- a/lib/SP/Domain/Upgrade/Services/UpgradeDatabase.php +++ b/lib/SP/Domain/Upgrade/Services/UpgradeDatabase.php @@ -1,5 +1,4 @@ . */ +declare(strict_types=1); + namespace SP\Domain\Upgrade\Services; use Exception; @@ -71,22 +72,17 @@ final class UpgradeDatabase extends UpgradeBase */ protected function applyUpgrade(string $version): bool { - $queries = $this->getQueriesFromFile($version); + $count = 0; - if (count($queries) === 0) { - logger(__('Update file does not contain data'), 'ERROR'); + foreach ($this->getQueriesFromFile($version) as $query) { + $count++; - throw UpgradeException::error(__u('Update file does not contain data'), $version); - } - - foreach ($queries as $query) { try { $this->eventDispatcher->notify( 'upgrade.db.process', new Event($this, EventMessage::factory()->addDetail(__u('Version'), $version)) ); - // Direct PDO handling $this->database->runQueryRaw($query); } catch (Exception $e) { processException($e); @@ -107,6 +103,12 @@ final class UpgradeDatabase extends UpgradeBase } } + if ($count === 0) { + logger(__('Update file does not contain data'), 'ERROR'); + + throw UpgradeException::error(__u('Update file does not contain data'), $version); + } + $this->eventDispatcher->notify( 'upgrade.db.process', new Event( @@ -123,7 +125,7 @@ final class UpgradeDatabase extends UpgradeBase */ private function getQueriesFromFile(string $filename): iterable { - $fileName = FileSystem::buildPath(SQL_PATH, str_replace('.', '', $filename), '.sql'); + $fileName = FileSystem::buildPath(SQL_PATH, str_replace('.', '', $filename) . '.sql'); try { return (new MysqlFileParser(new FileHandler($fileName)))->parse('$$'); diff --git a/lib/SP/Infrastructure/Database/Database.php b/lib/SP/Infrastructure/Database/Database.php index 4e0c2bbd..365e4091 100644 --- a/lib/SP/Infrastructure/Database/Database.php +++ b/lib/SP/Infrastructure/Database/Database.php @@ -1,4 +1,26 @@ . + */ declare(strict_types=1); /* @@ -206,6 +228,7 @@ final class Database implements DatabaseInterface * * @param string $query * @throws QueryException + * @throws DatabaseException */ public function runQueryRaw(string $query): void { @@ -216,6 +239,8 @@ final class Database implements DatabaseInterface /** * Start a transaction + * + * @throws DatabaseException */ public function beginTransaction(): bool { @@ -242,6 +267,8 @@ final class Database implements DatabaseInterface /** * Finish a transaction + * + * @throws DatabaseException */ public function endTransaction(): bool { @@ -262,6 +289,8 @@ final class Database implements DatabaseInterface /** * Rollback a transaction + * + * @throws DatabaseException */ public function rollbackTransaction(): bool { diff --git a/schemas/40024210101.sql b/schemas/40024210101.sql index ebf10ea6..ada53103 100644 --- a/schemas/40024210101.sql +++ b/schemas/40024210101.sql @@ -21,8 +21,10 @@ * along with sysPass. If not, see . */ -alter table CustomFieldData - drop column id; +DELIMITER $$ alter table CustomFieldData - add primary key (moduleId, itemId, definitionId); + drop column id$$ + +alter table CustomFieldData + add primary key (moduleId, itemId, definitionId)$$ diff --git a/tests/SP/Domain/Upgrade/Services/UpgradeDatabaseTest.php b/tests/SP/Domain/Upgrade/Services/UpgradeDatabaseTest.php new file mode 100644 index 00000000..1e7b6d78 --- /dev/null +++ b/tests/SP/Domain/Upgrade/Services/UpgradeDatabaseTest.php @@ -0,0 +1,121 @@ +. + */ + +declare(strict_types=1); + +namespace SP\Tests\Domain\Upgrade\Services; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\MockObject\Exception; +use RuntimeException; +use SP\Domain\Config\Ports\ConfigDataInterface; +use SP\Domain\Database\Ports\DatabaseInterface; +use SP\Domain\Log\Ports\FileHandlerProvider; +use SP\Domain\Upgrade\Services\UpgradeDatabase; +use SP\Domain\Upgrade\Services\UpgradeException; +use SP\Infrastructure\File\FileException; +use SP\Tests\UnitaryTestCase; + +/** + * Class UpgradeDatabaseTest + */ +#[Group('unitary')] +class UpgradeDatabaseTest extends UnitaryTestCase +{ + /** + * @throws Exception + * @throws UpgradeException + * @throws FileException + */ + public function testUpgrade() + { + $fileHandlerProvider = $this->createMock(FileHandlerProvider::class); + $database = $this->createMock(DatabaseInterface::class); + $configData = $this->createMock(ConfigDataInterface::class); + + $database->expects($this->exactly(2)) + ->method('runQueryRaw') + ->with( + ... + self::withConsecutive( + ['alter table CustomFieldData drop column id'], + ['alter table CustomFieldData add primary key (moduleId, itemId, definitionId)'] + ) + ); + + $configData->expects($this->once()) + ->method('setDatabaseVersion') + ->with('400.24210101'); + + $upgradeDatabase = new UpgradeDatabase($this->application, $fileHandlerProvider, $database); + $upgradeDatabase->upgrade('400.00000000', $configData); + } + + /** + * @throws Exception + * @throws UpgradeException + * @throws FileException + */ + public function testUpgradeWithException() + { + $fileHandlerProvider = $this->createMock(FileHandlerProvider::class); + $database = $this->createMock(DatabaseInterface::class); + $configData = $this->createMock(ConfigDataInterface::class); + + $database->expects($this->once()) + ->method('runQueryRaw') + ->willThrowException(new RuntimeException('test')); + + $configData->expects($this->never()) + ->method('setDatabaseVersion'); + + $upgradeDatabase = new UpgradeDatabase($this->application, $fileHandlerProvider, $database); + + $this->expectException(UpgradeException::class); + $this->expectExceptionMessage('Error while updating the database'); + + $upgradeDatabase->upgrade('400.00000000', $configData); + } + + /** + * @throws Exception + * @throws UpgradeException + * @throws FileException + */ + public function testUpgradeWithNoUpgrades() + { + $fileHandlerProvider = $this->createMock(FileHandlerProvider::class); + $database = $this->createMock(DatabaseInterface::class); + $configData = $this->createMock(ConfigDataInterface::class); + + $database->expects($this->never()) + ->method('runQueryRaw'); + + $configData->expects($this->never()) + ->method('setDatabaseVersion'); + + $upgradeDatabase = new UpgradeDatabase($this->application, $fileHandlerProvider, $database); + $upgradeDatabase->upgrade('400.24210101', $configData); + } +}