chore: Refactor upgrade subsystem

Use attributes to detect and load upgrade handlers.

Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
Rubén D
2024-05-03 22:24:13 +02:00
parent 0304221fae
commit 10dedaa03f
20 changed files with 526 additions and 490 deletions

View File

@@ -1,101 +0,0 @@
<?php
declare(strict_types=1);
/*
* 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/>.
*/
namespace SP\Tests\Domain\Config\Services;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\MockObject\MockObject;
use SP\Core\Application;
use SP\Domain\Config\Ports\ConfigDataInterface;
use SP\Domain\Config\Ports\ConfigFileService;
use SP\Domain\Log\Ports\FileHandlerProvider;
use SP\Domain\Upgrade\Services\UpgradeConfig;
use SP\Domain\Upgrade\Services\UpgradeException;
use SP\Infrastructure\File\FileException;
use SP\Tests\UnitaryTestCase;
/**
* Class UpgradeConfigTest
*
*/
#[Group('unitary')]
class UpgradeConfigTest extends UnitaryTestCase
{
private FileHandlerProvider|MockObject $fileLogHandlerProvider;
public static function versionDataProvider(): array
{
return [
['320.20062801', false],
['340.00000000', false]
];
}
/**
* @throws Exception
* @throws FileException
* @throws UpgradeException
*/
public function testUpgrade()
{
$version = '200.00000000';
$configData = $this->createMock(ConfigDataInterface::class);
$configFileService = $this->createMock(ConfigFileService::class);
$application = new Application(
$configFileService,
$this->application->getEventDispatcher(),
$this->application->getContext()
);
$configData->expects(self::never())
->method('setConfigVersion')
->with(self::anything());
$configFileService->expects(self::never())
->method('save')
->with($configData, false);
$upgradeConfig = new UpgradeConfig($application, $this->fileLogHandlerProvider);
$upgradeConfig->upgrade($version, $configData);
}
/**
* @return void
*/
#[DataProvider('versionDataProvider')]
public function testNeedsUpgrade(string $version, bool $expected)
{
$this->assertEquals($expected, UpgradeConfig::needsUpgrade($version));
}
protected function setUp(): void
{
parent::setUp();
$this->fileLogHandlerProvider = $this->createMock(FileHandlerProvider::class);
}
}

View File

@@ -1,87 +0,0 @@
<?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);
/**
* sysPass
*
* @author nuxsmin
* @link http://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/>.
*
*/
namespace SP\Tests\Domain\Upgrade\Services;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\Exception;
use SP\Domain\Config\Ports\ConfigDataInterface;
use SP\Domain\Log\Ports\FileHandlerProvider;
use SP\Domain\Upgrade\Services\UpgradeApp;
use SP\Domain\Upgrade\Services\UpgradeException;
use SP\Infrastructure\File\FileException;
use SP\Tests\UnitaryTestCase;
/**
* Class UpgradeAppTest
*/
#[Group('unitary')]
class UpgradeAppTest extends UnitaryTestCase
{
/**
* @throws Exception
* @throws UpgradeException
* @throws FileException
*/
public function testUpgrade()
{
$fileHandlerProvider = $this->createMock(FileHandlerProvider::class);
$configData = $this->createMock(ConfigDataInterface::class);
$configData->expects($this->never())
->method('setAppVersion');
$this->config->expects($this->never())
->method('save');
$upgradeApp = new UpgradeApp($this->application, $fileHandlerProvider);
$upgradeApp->upgrade('123', $configData);
}
}

View File

@@ -1,87 +0,0 @@
<?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);
/**
* sysPass
*
* @author nuxsmin
* @link http://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/>.
*
*/
namespace SP\Tests\Domain\Upgrade\Services;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\Exception;
use SP\Domain\Config\Ports\ConfigDataInterface;
use SP\Domain\Log\Ports\FileHandlerProvider;
use SP\Domain\Upgrade\Services\UpgradeConfig;
use SP\Domain\Upgrade\Services\UpgradeException;
use SP\Infrastructure\File\FileException;
use SP\Tests\UnitaryTestCase;
/**
* Class UpgradeConfigTest
*/
#[Group('unitary')]
class UpgradeConfigTest extends UnitaryTestCase
{
/**
* @throws Exception
* @throws UpgradeException
* @throws FileException
*/
public function testUpgrade()
{
$fileHandlerProvider = $this->createMock(FileHandlerProvider::class);
$configData = $this->createMock(ConfigDataInterface::class);
$configData->expects($this->never())
->method('setConfigVersion');
$this->config->expects($this->never())
->method('save');
$upgradeConfig = new UpgradeConfig($this->application, $fileHandlerProvider);
$upgradeConfig->upgrade('123', $configData);
}
}

View File

@@ -28,13 +28,12 @@ namespace SP\Tests\Domain\Upgrade\Services;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\MockObject\MockObject;
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;
/**
@@ -43,83 +42,80 @@ use SP\Tests\UnitaryTestCase;
#[Group('unitary')]
class UpgradeDatabaseTest extends UnitaryTestCase
{
private UpgradeDatabase $upgradeDatabase;
private DatabaseInterface|MockObject $database;
/**
* @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)']
)
);
$this->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');
$this->config->expects($this->once())
->method('save')
->with($configData);
$upgradeDatabase = new UpgradeDatabase($this->application, $fileHandlerProvider, $database);
$upgradeDatabase->upgrade('400.00000000', $configData);
$this->upgradeDatabase->apply('400.24210101', $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'));
$this->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);
$this->upgradeDatabase->apply('400.24210101', $configData);
}
/**
* @throws Exception
* @throws UpgradeException
* @throws FileException
*/
public function testUpgradeWithNoUpgrades()
public function testUpgradeWithFileException()
{
$fileHandlerProvider = $this->createMock(FileHandlerProvider::class);
$database = $this->createMock(DatabaseInterface::class);
$configData = $this->createMock(ConfigDataInterface::class);
$database->expects($this->never())
->method('runQueryRaw');
$this->database->expects($this->never())
->method('runQueryRaw');
$configData->expects($this->never())
->method('setDatabaseVersion');
$upgradeDatabase = new UpgradeDatabase($this->application, $fileHandlerProvider, $database);
$upgradeDatabase->upgrade('400.24210101', $configData);
$this->expectException(UpgradeException::class);
$this->expectExceptionMessage('Failed to open stream: No such file or directory');
$this->upgradeDatabase->apply('400.00000000', $configData);
}
protected function setUp(): void
{
parent::setUp();
$this->database = $this->createMock(DatabaseInterface::class);
$this->upgradeDatabase = new UpgradeDatabase($this->application, $this->database);
}
}

View File

@@ -0,0 +1,217 @@
<?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 PHPUnit\Framework\MockObject\MockObject;
use Psr\Container\ContainerInterface;
use RuntimeException;
use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Config\Ports\ConfigDataInterface;
use SP\Domain\Core\Exceptions\InvalidClassException;
use SP\Domain\Log\Ports\FileHandlerProvider;
use SP\Domain\Upgrade\Ports\UpgradeHandlerService;
use SP\Domain\Upgrade\Services\Upgrade;
use SP\Domain\Upgrade\Services\UpgradeException;
use SP\Infrastructure\File\FileException;
use SP\Tests\Stubs\UpgradeHandlerStub;
use SP\Tests\UnitaryTestCase;
use stdClass;
/**
* Class UpgradeTest
*/
#[Group('unitary')]
class UpgradeTest extends UnitaryTestCase
{
private MockObject|ContainerInterface $container;
private Upgrade $upgrade;
/**
* @throws ServiceException
* @throws InvalidClassException
* @throws Exception
*/
public function testRegisterUpgradeHandler()
{
$handler = $this->createMock(UpgradeHandlerService::class);
$this->upgrade->registerUpgradeHandler($handler::class);
$this->expectNotToPerformAssertions();
}
/**
* @throws ServiceException
* @throws InvalidClassException
*/
public function testRegisterUpgradeHandlerWithClassException()
{
$this->expectException(InvalidClassException::class);
$this->expectExceptionMessage('Class does not either exist or implement UpgradeService class');
$this->upgrade->registerUpgradeHandler(stdClass::class);
}
/**
* @throws ServiceException
* @throws InvalidClassException
* @throws Exception
*/
public function testRegisterUpgradeHandlerWithDuplicate()
{
$handler = $this->createMock(UpgradeHandlerService::class);
$this->upgrade->registerUpgradeHandler($handler::class);
$this->expectException(ServiceException::class);
$this->expectExceptionMessage('Class already registered');
$this->upgrade->registerUpgradeHandler($handler::class);
}
/**
* @throws Exception
* @throws ServiceException
* @throws FileException
* @throws UpgradeException
*/
public function testUpgrade()
{
$configData = $this->createMock(ConfigDataInterface::class);
$this->config->expects($this->never())
->method('save');
$this->upgrade->upgrade('400.00000000', $configData);
}
/**
* @throws Exception
* @throws ServiceException
* @throws FileException
* @throws UpgradeException
* @throws InvalidClassException
*/
public function testUpgradeWithHandler()
{
$configData = $this->createMock(ConfigDataInterface::class);
$handler = $this->createMock(UpgradeHandlerService::class);
$handler->expects($this->exactly(2))
->method('apply')
->with('400.00000000', $configData)
->willReturn(true);
$this->container
->expects($this->exactly(2))
->method('get')
->with(UpgradeHandlerStub::class)
->willReturn($handler);
$this->config->expects($this->exactly(2))
->method('save')
->with($configData, false);
$this->upgrade->registerUpgradeHandler(UpgradeHandlerStub::class);
$this->upgrade->upgrade('400.00000000', $configData);
}
/**
* @throws Exception
* @throws ServiceException
* @throws FileException
* @throws UpgradeException
* @throws InvalidClassException
*/
public function testUpgradeWithHandlerWithFailedApply()
{
$configData = $this->createMock(ConfigDataInterface::class);
$handler = $this->createMock(UpgradeHandlerService::class);
$handler->expects($this->once())
->method('apply')
->with('400.00000000', $configData)
->willReturn(false);
$this->container
->expects($this->once())
->method('get')
->with(UpgradeHandlerStub::class)
->willReturn($handler);
$this->config->expects($this->never())
->method('save');
$this->upgrade->registerUpgradeHandler(UpgradeHandlerStub::class);
$this->expectException(UpgradeException::class);
$this->expectExceptionMessage('Error while applying the update');
$this->upgrade->upgrade('400.00000000', $configData);
}
/**
* @throws Exception
* @throws ServiceException
* @throws FileException
* @throws UpgradeException
* @throws InvalidClassException
*/
public function testUpgradeWithException()
{
$configData = $this->createMock(ConfigDataInterface::class);
$handler = $this->createMock(UpgradeHandlerService::class);
$handler->expects($this->never())
->method('apply');
$this->container
->expects($this->once())
->method('get')
->with(UpgradeHandlerStub::class)
->willThrowException(new RuntimeException('test'));
$this->config->expects($this->never())
->method('save');
$this->upgrade->registerUpgradeHandler(UpgradeHandlerStub::class);
$this->expectException(ServiceException::class);
$this->expectExceptionMessage('test');
$this->upgrade->upgrade('400.00000000', $configData);
}
protected function setUp(): void
{
parent::setUp();
$fileHandlerProvider = $this->createMock(FileHandlerProvider::class);
$this->container = $this->createMock(ContainerInterface::class);
$this->upgrade = new Upgrade($this->application, $fileHandlerProvider, $this->container);
}
}

View File

@@ -0,0 +1,45 @@
<?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\Stubs;
use SP\Domain\Common\Attributes\UpgradeVersion;
use SP\Domain\Config\Ports\ConfigDataInterface;
use SP\Domain\Upgrade\Ports\UpgradeHandlerService;
/**
* Class UpgradeHandlerStub
*/
#[UpgradeVersion('400.00000002')]
#[UpgradeVersion('400.00000001')]
#[UpgradeVersion('400.00000000')]
class UpgradeHandlerStub implements UpgradeHandlerService
{
public function apply(string $version, ConfigDataInterface $configData): bool
{
return true;
}
}