From eda1e037863b5009c7c9199bc155fb56fb27bcac Mon Sep 17 00:00:00 2001 From: RahatHameed Date: Thu, 11 Jul 2024 17:59:42 +0200 Subject: [PATCH] OXDEV-8489 Prepared & Consumed ModuleBlocklistService Prepared this service ModuleBlocklistService Consumed above service in ModuleSwitchService --- module_blocklist.yaml | 3 + .../Exception/ModuleBlockListException.php | 22 +++++ .../Exception/ModuleDeactivationException.php | 2 +- src/Module/Service/ModuleBlocklistService.php | 37 +++++++++ .../ModuleBlocklistServiceInterface.php | 18 ++++ src/Module/Service/ModuleSwitchService.php | 13 ++- .../Service/ModuleSwitchServiceInterface.php | 7 ++ src/Module/services.yaml | 11 +++ .../ModuleBlockListExceptionTest.php | 27 ++++++ .../ModuleSwitchInfrastructureTest.php | 3 +- .../Service/ModuleBlocklistServiceTest.php | 82 +++++++++++++++++++ .../Service/ModuleSwitchServiceTest.php | 19 ++++- 12 files changed, 238 insertions(+), 6 deletions(-) create mode 100644 module_blocklist.yaml create mode 100644 src/Module/Exception/ModuleBlockListException.php create mode 100644 src/Module/Service/ModuleBlocklistService.php create mode 100644 src/Module/Service/ModuleBlocklistServiceInterface.php create mode 100644 tests/Unit/Module/Exception/ModuleBlockListExceptionTest.php create mode 100644 tests/Unit/Module/Service/ModuleBlocklistServiceTest.php diff --git a/module_blocklist.yaml b/module_blocklist.yaml new file mode 100644 index 0000000..80ae9ad --- /dev/null +++ b/module_blocklist.yaml @@ -0,0 +1,3 @@ +modules: + - oe_graphql_base + - oe_graphql_configuration_access diff --git a/src/Module/Exception/ModuleBlockListException.php b/src/Module/Exception/ModuleBlockListException.php new file mode 100644 index 0000000..ff77191 --- /dev/null +++ b/src/Module/Exception/ModuleBlockListException.php @@ -0,0 +1,22 @@ +moduleBlocklist; + $blocklistData = $this->yamlFileLoader->load($resolvedPath); + + return in_array($moduleId, $blocklistData['modules'], true); + } catch (\Exception $e) { + throw new ModuleBlockListException(); + } + } +} diff --git a/src/Module/Service/ModuleBlocklistServiceInterface.php b/src/Module/Service/ModuleBlocklistServiceInterface.php new file mode 100644 index 0000000..49cfaf1 --- /dev/null +++ b/src/Module/Service/ModuleBlocklistServiceInterface.php @@ -0,0 +1,18 @@ +moduleSwitchInfrastructure->activateModule(moduleId: $moduleId); } + /** + * @inheritDoc + */ public function deactivateModule(string $moduleId): bool { + if ($this->moduleBlocklistService->isModuleBlocked($moduleId)) { + throw new ModuleDeactivationException(); + } return $this->moduleSwitchInfrastructure->deactivateModule(moduleId: $moduleId); } } diff --git a/src/Module/Service/ModuleSwitchServiceInterface.php b/src/Module/Service/ModuleSwitchServiceInterface.php index 0178c57..42ddb65 100644 --- a/src/Module/Service/ModuleSwitchServiceInterface.php +++ b/src/Module/Service/ModuleSwitchServiceInterface.php @@ -7,8 +7,15 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Module\Service; +use OxidEsales\GraphQL\ConfigurationAccess\Module\Exception\ModuleActivationException; +use OxidEsales\GraphQL\ConfigurationAccess\Module\Exception\ModuleDeactivationException; + interface ModuleSwitchServiceInterface { public function activateModule(string $moduleId): bool; + + /** + * @throws ModuleDeactivationException + */ public function deactivateModule(string $moduleId): bool; } diff --git a/src/Module/services.yaml b/src/Module/services.yaml index 3a422df..e16236f 100644 --- a/src/Module/services.yaml +++ b/src/Module/services.yaml @@ -1,3 +1,6 @@ +parameters: + oxidesales.graphqlconfigurationaccess.modules_block_list: '../../../module_blocklist.yaml' + services: _defaults: public: false @@ -15,3 +18,11 @@ services: OxidEsales\GraphQL\ConfigurationAccess\Module\Service\ModuleSwitchServiceInterface: class: OxidEsales\GraphQL\ConfigurationAccess\Module\Service\ModuleSwitchService + + OxidEsales\GraphQL\ConfigurationAccess\Module\Infrastructure\YamlFileLoaderInfrastructureInterface: + class: OxidEsales\GraphQL\ConfigurationAccess\Module\Infrastructure\YamlFileLoaderInfrastructure + + OxidEsales\GraphQL\ConfigurationAccess\Module\Service\ModuleBlocklistServiceInterface: + class: OxidEsales\GraphQL\ConfigurationAccess\Module\Service\ModuleBlocklistService + arguments: + $moduleBlocklist: '%oxidesales.graphqlconfigurationaccess.modules_block_list%' diff --git a/tests/Unit/Module/Exception/ModuleBlockListExceptionTest.php b/tests/Unit/Module/Exception/ModuleBlockListExceptionTest.php new file mode 100644 index 0000000..a7177cf --- /dev/null +++ b/tests/Unit/Module/Exception/ModuleBlockListExceptionTest.php @@ -0,0 +1,27 @@ +assertInstanceOf(ModuleBlockListException::class, $exception); + $this->assertSame(ModuleBlockListException::EXCEPTION_MESSAGE, $exception->getMessage()); + } +} diff --git a/tests/Unit/Module/Infrastructure/ModuleSwitchInfrastructureTest.php b/tests/Unit/Module/Infrastructure/ModuleSwitchInfrastructureTest.php index d9b034b..06bbc0f 100644 --- a/tests/Unit/Module/Infrastructure/ModuleSwitchInfrastructureTest.php +++ b/tests/Unit/Module/Infrastructure/ModuleSwitchInfrastructureTest.php @@ -85,9 +85,10 @@ public static function activationDataProvider(): \Generator public static function exceptionDataProvider(): \Generator { + $moduleId = uniqid(); yield 'test activate module throws exception' => [ 'method' => 'activate', - 'moduleId' => uniqid(), + 'moduleId' => $moduleId, 'exceptionClass' => ModuleActivationException::class, ]; diff --git a/tests/Unit/Module/Service/ModuleBlocklistServiceTest.php b/tests/Unit/Module/Service/ModuleBlocklistServiceTest.php new file mode 100644 index 0000000..4b7c70c --- /dev/null +++ b/tests/Unit/Module/Service/ModuleBlocklistServiceTest.php @@ -0,0 +1,82 @@ + ['module1', 'module2']]; + + $yamlFileLoaderMock = $this->createMock(YamlFileLoaderInfrastructureInterface::class); + $yamlFileLoaderMock + ->method('load') + ->with($this->stringContains($filePath)) + ->willReturn($expectedData); + + $sut = $this->getSut(moduleBlockList: $filePath, yamlFileLoader: $yamlFileLoaderMock); + $actualResult = $sut->isModuleBlocked(moduleId: $moduleId); + + $this->assertSame($expectedResult, $actualResult); + } + + public static function blockListDataProvider(): \Generator + { + yield 'isModuleBlocked returns true if Module is in the BlockList' => [ + 'moduleId' => 'module1', + 'expectedResult' => true + ]; + + yield 'isModuleBlocked returns false if Module is not in the BlockList' => [ + 'moduleId' => 'unknownModuleId', + 'expectedResult' => false + ]; + } + + public function testGetModuleBlocklistThrowsExceptionOnFailure(): void + { + $moduleId = uniqid(); + $yamlFileLoaderMock = $this->createMock(YamlFileLoaderInfrastructureInterface::class); + $yamlFileLoaderMock + ->method('load') + ->will($this->throwException(new \Exception('Failed to load YAML file'))); + + $this->expectException(ModuleBlockListException::class); + + $sut = $this->getSut(yamlFileLoader: $yamlFileLoaderMock); + $sut->isModuleBlocked(moduleId: $moduleId); + } + + private function getSut( + string $moduleBlockList = null, + YamlFileLoaderInfrastructureInterface $yamlFileLoader = null + ): ModuleBlocklistService { + return new ModuleBlocklistService( + moduleBlocklist: $moduleBlockList + ?? 'testFilePath.yaml', + yamlFileLoader: $yamlFileLoader + ?? $this->createStub(YamlFileLoaderInfrastructureInterface::class) + ); + } +} diff --git a/tests/Unit/Module/Service/ModuleSwitchServiceTest.php b/tests/Unit/Module/Service/ModuleSwitchServiceTest.php index 1a87b27..bd8732c 100644 --- a/tests/Unit/Module/Service/ModuleSwitchServiceTest.php +++ b/tests/Unit/Module/Service/ModuleSwitchServiceTest.php @@ -10,6 +10,7 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\Module\Service; use OxidEsales\GraphQL\ConfigurationAccess\Module\Infrastructure\ModuleSwitchInfrastructureInterface; +use OxidEsales\GraphQL\ConfigurationAccess\Module\Service\ModuleBlocklistServiceInterface; use OxidEsales\GraphQL\ConfigurationAccess\Module\Service\ModuleSwitchService; use PHPUnit\Framework\TestCase; @@ -31,7 +32,16 @@ public function testModuleActivationAndDeactivation( ->with($moduleId) ->willReturn(true); - $sut = $this->getSut(moduleSwitchInfrastructure: $moduleSwitchInfrastructureMock); + $moduleBlocklistServiceMock = $this->createMock(ModuleBlocklistServiceInterface::class); + $moduleBlocklistServiceMock + ->method('isModuleBlocked') + ->with($moduleId) + ->willReturn(false); + + $sut = $this->getSut( + moduleSwitchInfrastructure: $moduleSwitchInfrastructureMock, + moduleBlocklistService: $moduleBlocklistServiceMock + ); $actualResult = ($method == 'activateModule') ? $sut->activateModule(moduleId: $moduleId) : $sut->deactivateModule(moduleId: $moduleId); @@ -40,11 +50,14 @@ public function testModuleActivationAndDeactivation( } private function getSut( - ModuleSwitchInfrastructureInterface $moduleSwitchInfrastructure = null + ModuleSwitchInfrastructureInterface $moduleSwitchInfrastructure = null, + ModuleBlocklistServiceInterface $moduleBlocklistService = null ): ModuleSwitchService { return new ModuleSwitchService( moduleSwitchInfrastructure: $moduleSwitchInfrastructure - ?? $this->createStub(ModuleSwitchInfrastructureInterface::class) + ?? $this->createStub(ModuleSwitchInfrastructureInterface::class), + moduleBlocklistService: $moduleBlocklistService + ?? $this->createStub(ModuleBlocklistServiceInterface::class) ); }