chore(tests): UT for UpgradeDatabase

Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
Rubén D
2024-05-01 18:39:28 +02:00
parent 40c6cb1dba
commit 99c8e4c03c
4 changed files with 167 additions and 13 deletions

View File

@@ -1,5 +1,4 @@
<?php
declare(strict_types=1);
/*
* sysPass
*
@@ -23,6 +22,8 @@ declare(strict_types=1);
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*/
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('$$');

View File

@@ -1,4 +1,26 @@
<?php
/*
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
* sysPass is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sysPass is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*/
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
{

View File

@@ -21,8 +21,10 @@
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*/
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)$$

View File

@@ -0,0 +1,121 @@
<?php
/*
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
* sysPass is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sysPass is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*/
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);
}
}