. */ namespace SP\Tests\Domain\Plugin\Services; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\MockObject\Exception; use SP\Domain\Core\Exceptions\ConstraintException; use SP\Domain\Core\Exceptions\QueryException; use SP\Domain\Plugin\Ports\Plugin; use SP\Domain\Plugin\Ports\PluginManagerService; use SP\Domain\Plugin\Services\PluginUpgrader; use SP\Infrastructure\Common\Repositories\NoSuchItemException; use SP\Tests\Generators\PluginGenerator; use SP\Tests\UnitaryTestCase; /** * Class PluginUpgraderTest */ #[Group('unitary')] class PluginUpgraderTest extends UnitaryTestCase { /** * @throws Exception * @throws ConstraintException * @throws QueryException */ public function testUpgradeForWithCompatibleVersion() { $pluginModel = PluginGenerator::factory()->buildPlugin(); $pluginManagerService = $this->createMock(PluginManagerService::class); $pluginUpgrader = new PluginUpgrader($this->application, $pluginManagerService); $plugin = self::createMock(Plugin::class); $plugin->method('getName')->willReturn('test_plugin'); $pluginManagerService->expects($this->once()) ->method('getByName') ->willReturn($pluginModel); [$version, $build] = explode('.', $pluginModel->getVersionLevel()); $upgradeVersion = sprintf('%d.%d', (int)$version + 100, (int)$build); $plugin->expects($this->once()) ->method('onUpgrade') ->with($upgradeVersion); $pluginManagerService->expects($this->once()) ->method('update') ->with($pluginModel->mutate(['data' => null, 'versionLevel' => $upgradeVersion])); $pluginUpgrader->upgradeFor($plugin, $upgradeVersion); } /** * @throws Exception * @throws ConstraintException * @throws QueryException */ public function testUpgradeForWithNoCompatibleVersion() { $pluginModel = PluginGenerator::factory()->buildPlugin(); $pluginManagerService = $this->createMock(PluginManagerService::class); $pluginUpgrader = new PluginUpgrader($this->application, $pluginManagerService); $plugin = self::createMock(Plugin::class); $plugin->method('getName')->willReturn('test_plugin'); $pluginManagerService->expects($this->once()) ->method('getByName') ->willReturn($pluginModel); [$version, $build] = explode('.', $pluginModel->getVersionLevel()); $upgradeVersion = sprintf('%d.%d', (int)$version - 100, (int)$build); $plugin->expects($this->never()) ->method('onUpgrade'); $pluginManagerService->expects($this->never()) ->method('update'); $pluginUpgrader->upgradeFor($plugin, $upgradeVersion); } /** * @throws Exception * @throws ConstraintException * @throws QueryException */ public function testUpgradeForWithNullVersion() { $pluginModel = PluginGenerator::factory()->buildPlugin()->mutate(['versionLevel' => null]); $pluginManagerService = $this->createMock(PluginManagerService::class); $pluginUpgrader = new PluginUpgrader($this->application, $pluginManagerService); $plugin = self::createMock(Plugin::class); $plugin->method('getName')->willReturn('test_plugin'); $pluginManagerService->expects($this->once()) ->method('getByName') ->willReturn($pluginModel); $plugin->expects($this->once()) ->method('onUpgrade') ->with('1.1.1'); $pluginManagerService->expects($this->once()) ->method('update') ->with($pluginModel->mutate(['data' => null, 'versionLevel' => '1.1.1'])); $pluginUpgrader->upgradeFor($plugin, '1.1.1'); } /** * @throws Exception * @throws ConstraintException * @throws QueryException */ public function testUpgradeForWithException() { $pluginManagerService = $this->createMock(PluginManagerService::class); $pluginUpgrader = new PluginUpgrader($this->application, $pluginManagerService); $plugin = self::createMock(Plugin::class); $plugin->method('getName')->willReturn('test_plugin'); $pluginManagerService->expects($this->once()) ->method('getByName') ->willThrowException(NoSuchItemException::error('test')); $plugin->expects($this->never()) ->method('onUpgrade'); $pluginManagerService->expects($this->never()) ->method('update'); $pluginUpgrader->upgradeFor($plugin, '1.1.1'); } }