From 654dbf8cdcf73703415ab1c98c63a1408604a945 Mon Sep 17 00:00:00 2001 From: marcelmanzel Date: Fri, 13 Dec 2024 15:42:22 +0100 Subject: [PATCH] OXDEV-8952: Move ModuleDataType generation to Infrastructure --- CHANGELOG.md | 6 ++++ .../ModuleListInfrastructure.php | 12 +++++-- .../ModuleListInfrastructureInterface.php | 6 ++-- src/Module/Service/ModuleListService.php | 10 ++---- .../Subscriber/BeforeModuleDeactivation.php | 3 ++ .../ModuleListInfrastructureTest.php | 26 +++++++++----- .../ModuleListInfrastructureTest.php | 34 ++++++++++++------- .../Module/Service/ModuleListServiceTest.php | 18 ++-------- 8 files changed, 65 insertions(+), 50 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d13feff..1cc71b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [2.0.0] - unreleased + +### Changed +- Move ModuleDataType generation to Infrastructure + ## [1.2.0] - 2024-11-27 This is the stable release of v1.2.0. No changes have been made since v1.2.0-rc.1. @@ -35,6 +40,7 @@ This is stable release for v1.1.0. No changes have been made since v1.1.0-rc.1. - Initial release +[2.0.0]: https://github.com/OXID-eSales/graphql-configuration-access/compare/v1.2.0...b-7.2.x [1.2.0]: https://github.com/OXID-eSales/graphql-configuration-access/compare/v1.2.0-rc.1...v1.2.0 [1.2.0-rc.1]: https://github.com/OXID-eSales/graphql-configuration-access/compare/v1.1.0...v1.2.0-rc.1 [1.1.0]: https://github.com/OXID-eSales/graphql-configuration-access/compare/v1.1.0-rc.1...v1.1.0 diff --git a/src/Module/Infrastructure/ModuleListInfrastructure.php b/src/Module/Infrastructure/ModuleListInfrastructure.php index 4693784..f15384c 100644 --- a/src/Module/Infrastructure/ModuleListInfrastructure.php +++ b/src/Module/Infrastructure/ModuleListInfrastructure.php @@ -10,16 +10,18 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Module\Infrastructure; use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Bridge\ShopConfigurationDaoBridgeInterface; +use OxidEsales\GraphQL\ConfigurationAccess\Module\DataType\ModuleDataTypeFactoryInterface; use OxidEsales\GraphQL\ConfigurationAccess\Module\Exception\ModulesNotFoundException; class ModuleListInfrastructure implements ModuleListInfrastructureInterface { public function __construct( - private readonly ShopConfigurationDaoBridgeInterface $shopConfigurationDaoBridge + private readonly ShopConfigurationDaoBridgeInterface $shopConfigurationDaoBridge, + private readonly ModuleDataTypeFactoryInterface $moduleDataTypeFactory ) { } - public function getModuleConfigurations(): array + public function getModuleList(): array { $shopConfiguration = $this->shopConfigurationDaoBridge->get(); $moduleConfigurations = $shopConfiguration->getModuleConfigurations(); @@ -28,6 +30,10 @@ public function getModuleConfigurations(): array throw new ModulesNotFoundException(); } - return $moduleConfigurations; + $modulesArray = []; + foreach ($moduleConfigurations as $moduleConfig) { + $modulesArray[] = $this->moduleDataTypeFactory->createFromModuleConfiguration($moduleConfig); + } + return $modulesArray; } } diff --git a/src/Module/Infrastructure/ModuleListInfrastructureInterface.php b/src/Module/Infrastructure/ModuleListInfrastructureInterface.php index 8855519..7d35074 100644 --- a/src/Module/Infrastructure/ModuleListInfrastructureInterface.php +++ b/src/Module/Infrastructure/ModuleListInfrastructureInterface.php @@ -7,14 +7,14 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Module\Infrastructure; +use OxidEsales\GraphQL\ConfigurationAccess\Module\DataType\ModuleDataTypeInterface; use OxidEsales\GraphQL\ConfigurationAccess\Module\Exception\ModulesNotFoundException; -use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\DataObject\ModuleConfiguration; interface ModuleListInfrastructureInterface { /** - * @return array * @throws ModulesNotFoundException + * @return array */ - public function getModuleConfigurations(): array; + public function getModuleList(): array; } diff --git a/src/Module/Service/ModuleListService.php b/src/Module/Service/ModuleListService.php index 9fb5b46..e0ef919 100644 --- a/src/Module/Service/ModuleListService.php +++ b/src/Module/Service/ModuleListService.php @@ -9,7 +9,6 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Module\Service; -use OxidEsales\GraphQL\ConfigurationAccess\Module\DataType\ModuleDataTypeFactoryInterface; use OxidEsales\GraphQL\ConfigurationAccess\Module\Infrastructure\ModuleListInfrastructureInterface; use OxidEsales\GraphQL\ConfigurationAccess\Shared\DataType\ComponentFiltersInterface; use OxidEsales\GraphQL\ConfigurationAccess\Shared\Service\ComponentFilterServiceInterface; @@ -19,18 +18,13 @@ final class ModuleListService implements ModuleListServiceInterface public function __construct( private readonly ModuleListInfrastructureInterface $moduleListInfrastructure, private readonly ComponentFilterServiceInterface $componentFilterService, - private readonly ModuleDataTypeFactoryInterface $moduleDataTypeFactory ) { } public function getModuleList(ComponentFiltersInterface $filters): array { - $modulesArray = []; - $moduleConfigurations = $this->moduleListInfrastructure->getModuleConfigurations(); - foreach ($moduleConfigurations as $moduleConfig) { - $modulesArray[] = $this->moduleDataTypeFactory->createFromModuleConfiguration($moduleConfig); - } + $moduleConfigurations = $this->moduleListInfrastructure->getModuleList(); - return $this->componentFilterService->filterComponents($modulesArray, $filters); + return $this->componentFilterService->filterComponents($moduleConfigurations, $filters); } } diff --git a/src/Shared/Subscriber/BeforeModuleDeactivation.php b/src/Shared/Subscriber/BeforeModuleDeactivation.php index 2d5cf78..1ccfd1c 100644 --- a/src/Shared/Subscriber/BeforeModuleDeactivation.php +++ b/src/Shared/Subscriber/BeforeModuleDeactivation.php @@ -16,6 +16,9 @@ final class BeforeModuleDeactivation implements EventSubscriberInterface { + /** + * @param string[] $dependencies + */ public function __construct( private array $dependencies ) { diff --git a/tests/Integration/Infrastructure/ModuleListInfrastructureTest.php b/tests/Integration/Infrastructure/ModuleListInfrastructureTest.php index 423f01b..e6f8881 100644 --- a/tests/Integration/Infrastructure/ModuleListInfrastructureTest.php +++ b/tests/Integration/Infrastructure/ModuleListInfrastructureTest.php @@ -12,6 +12,8 @@ use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Bridge\ShopConfigurationDaoBridgeInterface; use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\DataObject\ModuleConfiguration; use OxidEsales\EshopCommunity\Tests\Integration\IntegrationTestCase; +use OxidEsales\GraphQL\ConfigurationAccess\Module\DataType\ModuleDataTypeFactoryInterface; +use OxidEsales\GraphQL\ConfigurationAccess\Module\DataType\ModuleDataTypeInterface; use OxidEsales\GraphQL\ConfigurationAccess\Module\Infrastructure\ModuleListInfrastructure; class ModuleListInfrastructureTest extends IntegrationTestCase @@ -24,10 +26,16 @@ public function testGetModuleList() $moduleConfiguration1 = new ModuleConfiguration(); $moduleConfiguration1->setId('firstModule'); + $moduleConfiguration1->setTitle(['en' => 'Module 1']); + $moduleConfiguration1->setDescription(['en' => 'Module 1 description']); + $moduleConfiguration1->setLang('en'); $moduleConfiguration1->setModuleSource('test'); $moduleConfiguration2 = new ModuleConfiguration(); $moduleConfiguration2->setId('secondModule'); + $moduleConfiguration2->setTitle(['en' => 'Module 2']); + $moduleConfiguration2->setDescription(['en' => 'Module 2 description']); + $moduleConfiguration2->setLang('en'); $moduleConfiguration2->setModuleSource('test1'); $shopConfiguration @@ -35,20 +43,20 @@ public function testGetModuleList() ->addModuleConfiguration($moduleConfiguration2); $shopConfigurationDaoBridge->save($shopConfiguration); - $sut = new ModuleListInfrastructure( - $shopConfigurationDaoBridge - ); - $modulesList = $sut->getModuleConfigurations(); - $this->assertEquals([ - $moduleConfiguration1->getId() => $moduleConfiguration1, - $moduleConfiguration2->getId() => $moduleConfiguration2 - ], $modulesList); + $sut = $this->getSut(); + $modulesList = $sut->getModuleList(); + $this->assertCount(2, $modulesList); + $this->assertInstanceOf(ModuleDataTypeInterface::class, $modulesList[0]); + $this->assertInstanceOf(ModuleDataTypeInterface::class, $modulesList[1]); + $this->assertSame($modulesList[0]->getId(), $moduleConfiguration1->getId()); + $this->assertSame($modulesList[1]->getId(), $moduleConfiguration2->getId()); } public function getSut(): ModuleListInfrastructure { return new ModuleListInfrastructure( - $this->get(ShopConfigurationDaoBridgeInterface::class) + $this->get(ShopConfigurationDaoBridgeInterface::class), + $this->get(ModuleDataTypeFactoryInterface::class) ); } } diff --git a/tests/Unit/Module/Infrastructure/ModuleListInfrastructureTest.php b/tests/Unit/Module/Infrastructure/ModuleListInfrastructureTest.php index c13d780..b2ab731 100644 --- a/tests/Unit/Module/Infrastructure/ModuleListInfrastructureTest.php +++ b/tests/Unit/Module/Infrastructure/ModuleListInfrastructureTest.php @@ -11,6 +11,8 @@ use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Bridge\ShopConfigurationDaoBridgeInterface; use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\DataObject\ShopConfiguration; +use OxidEsales\GraphQL\ConfigurationAccess\Module\DataType\ModuleDataTypeFactoryInterface; +use OxidEsales\GraphQL\ConfigurationAccess\Module\DataType\ModuleDataTypeInterface; use OxidEsales\GraphQL\ConfigurationAccess\Module\Infrastructure\ModuleListInfrastructure; use OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\UnitTestCase; use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\DataObject\ModuleConfiguration; @@ -23,32 +25,37 @@ class ModuleListInfrastructureTest extends UnitTestCase { public function testGetModuleList() { - $moduleConfigurationMock = $this->createMock(ModuleConfiguration::class); + $moduleConfigurationStub = $this->createStub(ModuleConfiguration::class); + $moduleDataType = $this->createStub(ModuleDataTypeInterface::class); $shopConfigurationMock = $this->createMock(ShopConfiguration::class); $shopConfigurationMock ->method('getModuleConfigurations') - ->willReturn([$moduleConfigurationMock]); + ->willReturn([$moduleConfigurationStub]); $shopConfigurationDaoBridgeMock = $this->createMock(ShopConfigurationDaoBridgeInterface::class); - $shopConfigurationDaoBridgeMock - ->method('get') + $shopConfigurationDaoBridgeMock->method('get') ->willReturn($shopConfigurationMock); + $moduleDataTypeFactoryMock = $this->createMock(ModuleDataTypeFactoryInterface::class); + $moduleDataTypeFactoryMock->method('createFromModuleConfiguration') + ->with($moduleConfigurationStub) + ->willReturn($moduleDataType); + $sut = $this->getSut( - shopConfigurationDaoBridgeMock: $shopConfigurationDaoBridgeMock + shopConfigurationDaoBridgeMock: $shopConfigurationDaoBridgeMock, + moduleDataTypeFactory: $moduleDataTypeFactoryMock ); - $result = $sut->getModuleConfigurations(); + $result = $sut->getModuleList(); - $this->assertSame([$moduleConfigurationMock], $result); + $this->assertSame([$moduleDataType], $result); } public function testGetModuleListThrowsException() { $shopConfigurationDaoBridgeMock = $this->createMock(ShopConfigurationDaoBridgeInterface::class); - $shopConfigurationDaoBridgeMock - ->method('get') + $shopConfigurationDaoBridgeMock->method('get') ->willReturn($this->createMock(ShopConfiguration::class)); $this->expectException(ModulesNotFoundException::class); @@ -57,14 +64,17 @@ public function testGetModuleListThrowsException() shopConfigurationDaoBridgeMock: $shopConfigurationDaoBridgeMock ); - $sut->getModuleConfigurations(); + $sut->getModuleList(); } public function getSut( - ?ShopConfigurationDaoBridgeInterface $shopConfigurationDaoBridgeMock = null + ?ShopConfigurationDaoBridgeInterface $shopConfigurationDaoBridgeMock = null, + ?ModuleDataTypeFactoryInterface $moduleDataTypeFactory = null ): ModuleListInfrastructure { return new ModuleListInfrastructure( - $shopConfigurationDaoBridgeMock ?? $this->createMock(ShopConfigurationDaoBridgeInterface::class) + shopConfigurationDaoBridge: $shopConfigurationDaoBridgeMock ?? + $this->createStub(ShopConfigurationDaoBridgeInterface::class), + moduleDataTypeFactory: $moduleDataTypeFactory ?? $this->createStub(ModuleDataTypeFactoryInterface::class) ); } } diff --git a/tests/Unit/Module/Service/ModuleListServiceTest.php b/tests/Unit/Module/Service/ModuleListServiceTest.php index 19e4865..52b5742 100644 --- a/tests/Unit/Module/Service/ModuleListServiceTest.php +++ b/tests/Unit/Module/Service/ModuleListServiceTest.php @@ -30,32 +30,20 @@ public function testGetModuleListWithFilters(): void $moduleStub2 = $this->createStub(ModuleDataTypeInterface::class); $filteredModules = [$moduleStub1]; - $moduleConfigStub1 = $this->createStub(ModuleConfiguration::class); - $moduleConfigStub2 = $this->createStub(ModuleConfiguration::class); - $modulesConfigurations = [$moduleConfigStub1, $moduleConfigStub2]; + $modulesConfigurations = [$moduleStub1, $moduleStub2]; $moduleListInfrastructureMock = $this->createMock(ModuleListInfrastructureInterface::class); - $moduleListInfrastructureMock->method('getModuleConfigurations') + $moduleListInfrastructureMock->method('getModuleList') ->willReturn($modulesConfigurations); $componentFilterServiceMock = $this->createMock(ComponentFilterServiceInterface::class); $componentFilterServiceMock->method('filterComponents') - ->with([$moduleStub1, $moduleStub2], $filtersStub) + ->with($modulesConfigurations, $filtersStub) ->willReturn($filteredModules); - $moduleDataTypeFactoryMock = $this->createMock(ModuleDataTypeFactoryInterface::class); - $moduleDataTypeFactoryMock - ->expects($this->exactly(2)) - ->method('createFromModuleConfiguration') - ->willReturnMap([ - [$moduleConfigStub1, $moduleStub1], - [$moduleConfigStub2, $moduleStub2] - ]); - $sut = new ModuleListService( moduleListInfrastructure: $moduleListInfrastructureMock, componentFilterService: $componentFilterServiceMock, - moduleDataTypeFactory: $moduleDataTypeFactoryMock ); $actualModules = $sut->getModuleList($filtersStub);