Skip to content

Commit

Permalink
OXDEV-8489: Create new ModuleDeactivationBlockedException
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelOxid committed Aug 14, 2024
1 parent 882a344 commit 06112e1
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 10 deletions.
22 changes: 22 additions & 0 deletions src/Module/Exception/ModuleDeactivationBlockedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\GraphQL\ConfigurationAccess\Module\Exception;

use OxidEsales\GraphQL\Base\Exception\NotFound;

final class ModuleDeactivationBlockedException extends NotFound
{
private const EXCEPTION_MESSAGE = 'Module "%s" is in the blocklist and cannot be deactivated.';

public function __construct(string $moduleId)
{
parent::__construct(sprintf(self::EXCEPTION_MESSAGE, $moduleId));
}
}
8 changes: 2 additions & 6 deletions src/Module/Exception/ModuleDeactivationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@
final class ModuleDeactivationException extends Error
{
private const EXCEPTION_MESSAGE = "An error occurred while deactivating the module.";
public const BLOCKED_MODULE_MESSAGE = 'Module "%s" is in the blocklist and cannot be deactivated.';

public function __construct(string $message = null)
public function __construct()
{
if (empty($message)) {
$message = self::EXCEPTION_MESSAGE;
}
parent::__construct($message);
parent::__construct(self::EXCEPTION_MESSAGE);
}
}
5 changes: 2 additions & 3 deletions src/Module/Service/ModuleActivationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OxidEsales\EshopCommunity\Internal\Framework\Module\Setup\Bridge\ModuleActivationBridgeInterface;
use OxidEsales\EshopCommunity\Internal\Transition\Utility\ContextInterface;
use OxidEsales\GraphQL\ConfigurationAccess\Module\Exception\ModuleActivationException;
use OxidEsales\GraphQL\ConfigurationAccess\Module\Exception\ModuleDeactivationBlockedException;
use OxidEsales\GraphQL\ConfigurationAccess\Module\Exception\ModuleDeactivationException;

class ModuleActivationService implements ModuleActivationServiceInterface
Expand Down Expand Up @@ -45,9 +46,7 @@ public function activateModule(string $moduleId): bool
public function deactivateModule(string $moduleId): bool
{
if ($this->moduleBlocklistService->isModuleBlocked($moduleId)) {
throw new ModuleDeactivationException(
sprintf(ModuleDeactivationException::BLOCKED_MODULE_MESSAGE, $moduleId)
);
throw new ModuleDeactivationBlockedException($moduleId);
}

$shopId = $this->context->getCurrentShopId();
Expand Down
2 changes: 2 additions & 0 deletions src/Module/Service/ModuleActivationServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use OxidEsales\GraphQL\ConfigurationAccess\Module\Exception\ModuleActivationException;
use OxidEsales\GraphQL\ConfigurationAccess\Module\Exception\ModuleDeactivationException;
use OxidEsales\GraphQL\ConfigurationAccess\Module\Exception\ModuleDeactivationBlockedException;

interface ModuleActivationServiceInterface
{
Expand All @@ -19,6 +20,7 @@ public function activateModule(string $moduleId): bool;

/**
* @throws ModuleDeactivationException
* @throws ModuleDeactivationBlockedException
*/
public function deactivateModule(string $moduleId): bool;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public function testException(): void
$exception = new ModuleBlockListException();

$this->assertInstanceOf(ModuleBlockListException::class, $exception);
$this->assertSame(ModuleBlockListException::EXCEPTION_MESSAGE, $exception->getMessage());
$this->assertSame('Failed to load module blocklist from YAML file.', $exception->getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\Module\Exception;

use OxidEsales\GraphQL\ConfigurationAccess\Module\Exception\ModuleDeactivationBlockedException;
use PHPUnit\Framework\TestCase;

/**
* @covers \OxidEsales\GraphQL\ConfigurationAccess\Module\Exception\ModuleDeactivationBlockedException
*/
final class ModuleDeactivationBlockedExceptionTest extends TestCase
{
public function testException(): void
{
$moduleId = uniqid();
$exception = new ModuleDeactivationBlockedException($moduleId);

$this->assertInstanceOf(ModuleDeactivationBlockedException::class, $exception);
$this->assertSame(
sprintf('Module "%s" is in the blocklist and cannot be deactivated.', $moduleId),
$exception->getMessage()
);
}
}
16 changes: 16 additions & 0 deletions tests/Unit/Module/Service/ModuleActivationsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OxidEsales\EshopCommunity\Internal\Transition\Utility\ContextInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Setup\Bridge\ModuleActivationBridgeInterface;
use OxidEsales\GraphQL\ConfigurationAccess\Module\Exception\ModuleActivationException;
use OxidEsales\GraphQL\ConfigurationAccess\Module\Exception\ModuleDeactivationBlockedException;
use OxidEsales\GraphQL\ConfigurationAccess\Module\Exception\ModuleDeactivationException;
use OxidEsales\GraphQL\ConfigurationAccess\Module\Service\ModuleActivationService;
use OxidEsales\GraphQL\ConfigurationAccess\Module\Service\ModuleBlocklistServiceInterface;
Expand Down Expand Up @@ -73,6 +74,21 @@ public function testModuleActivationAndDeactivationExceptions(
($method === 'activate') ? $sut->activateModule($moduleId) : $sut->deactivateModule($moduleId);
}

public function testModuleDeactivationBlockedException()
{
$moduleBlockListServiceMock = $this->createMock(ModuleBlocklistServiceInterface::class);
$moduleBlockListServiceMock
->method('isModuleBlocked')
->willReturn(true);

$sut = $this->getSut(
moduleBlocklistService: $moduleBlockListServiceMock
);

$this->expectException(ModuleDeactivationBlockedException::class);
$sut->deactivateModule(uniqid());
}

public static function activationDataProvider(): \Generator
{
yield 'test activate module' => [
Expand Down

0 comments on commit 06112e1

Please sign in to comment.