From 91f5515a6865e778fffa2218944e1077be04d419 Mon Sep 17 00:00:00 2001 From: marcelmanzel Date: Mon, 16 Dec 2024 08:48:17 +0100 Subject: [PATCH] OXDEV-8952: Move ThemeDataType generation to Infrastructur --- CHANGELOG.md | 1 + .../ModuleListInfrastructure.php | 6 +++--- .../ThemeListInfrastructure.php | 14 +++++++++++-- .../ThemeListInfrastructureInterface.php | 4 ++-- src/Theme/Service/ThemeListService.php | 12 +++-------- .../ThemeListInfrastructureTest.php | 6 ++++-- .../ThemeListInfrastructureTest.php | 20 +++++++++++++++---- .../Theme/Service/ThemeListServiceTest.php | 17 ++-------------- 8 files changed, 43 insertions(+), 37 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cc71b9..0da7bd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Move ModuleDataType generation to Infrastructure +- Move ThemeDataType 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. diff --git a/src/Module/Infrastructure/ModuleListInfrastructure.php b/src/Module/Infrastructure/ModuleListInfrastructure.php index f15384c..37c984a 100644 --- a/src/Module/Infrastructure/ModuleListInfrastructure.php +++ b/src/Module/Infrastructure/ModuleListInfrastructure.php @@ -30,10 +30,10 @@ public function getModuleList(): array throw new ModulesNotFoundException(); } - $modulesArray = []; + $moduleDatatypes = []; foreach ($moduleConfigurations as $moduleConfig) { - $modulesArray[] = $this->moduleDataTypeFactory->createFromModuleConfiguration($moduleConfig); + $moduleDatatypes[] = $this->moduleDataTypeFactory->createFromModuleConfiguration($moduleConfig); } - return $modulesArray; + return $moduleDatatypes; } } diff --git a/src/Theme/Infrastructure/ThemeListInfrastructure.php b/src/Theme/Infrastructure/ThemeListInfrastructure.php index 0ad5f73..a2c1e6e 100644 --- a/src/Theme/Infrastructure/ThemeListInfrastructure.php +++ b/src/Theme/Infrastructure/ThemeListInfrastructure.php @@ -9,15 +9,20 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Theme\Infrastructure; +use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataTypeFactoryInterface; use OxidEsales\GraphQL\ConfigurationAccess\Theme\Exception\ThemesNotFound; final class ThemeListInfrastructure implements ThemeListInfrastructureInterface { public function __construct( - private readonly CoreThemeFactoryInterface $coreThemeFactory + private readonly CoreThemeFactoryInterface $coreThemeFactory, + private readonly ThemeDataTypeFactoryInterface $themeDataTypeFactory ) { } + /** + * @inheritDoc + */ public function getThemes(): array { $coreThemeService = $this->coreThemeFactory->create(); @@ -27,6 +32,11 @@ public function getThemes(): array throw new ThemesNotFound(); } - return $themesList; + $themeDataTypes = []; + foreach ($themesList as $theme) { + $themeDataTypes[] = $this->themeDataTypeFactory->createFromCoreTheme(theme: $theme); + } + + return $themeDataTypes; } } diff --git a/src/Theme/Infrastructure/ThemeListInfrastructureInterface.php b/src/Theme/Infrastructure/ThemeListInfrastructureInterface.php index a7c85e5..918d3dc 100644 --- a/src/Theme/Infrastructure/ThemeListInfrastructureInterface.php +++ b/src/Theme/Infrastructure/ThemeListInfrastructureInterface.php @@ -7,13 +7,13 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Theme\Infrastructure; -use OxidEsales\Eshop\Core\Theme; +use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataTypeInterface; use OxidEsales\GraphQL\ConfigurationAccess\Theme\Exception\ThemesNotFound; interface ThemeListInfrastructureInterface { /** - * @return array + * @return ThemeDataTypeInterface[] * @throws ThemesNotFound */ public function getThemes(): array; diff --git a/src/Theme/Service/ThemeListService.php b/src/Theme/Service/ThemeListService.php index 29f8716..6f84cef 100644 --- a/src/Theme/Service/ThemeListService.php +++ b/src/Theme/Service/ThemeListService.php @@ -11,26 +11,20 @@ use OxidEsales\GraphQL\ConfigurationAccess\Shared\DataType\ComponentFiltersInterface; use OxidEsales\GraphQL\ConfigurationAccess\Shared\Service\ComponentFilterServiceInterface; -use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataTypeFactoryInterface; use OxidEsales\GraphQL\ConfigurationAccess\Theme\Infrastructure\ThemeListInfrastructureInterface; final class ThemeListService implements ThemeListServiceInterface { public function __construct( private readonly ThemeListInfrastructureInterface $themeListInfrastructure, - private readonly ComponentFilterServiceInterface $componentFilterService, - private readonly ThemeDataTypeFactoryInterface $themeDataTypeFactory + private readonly ComponentFilterServiceInterface $componentFilterService ) { } public function getThemeList(ComponentFiltersInterface $filters): array { - $themesArray = []; - $themesList = $this->themeListInfrastructure->getThemes(); - foreach ($themesList as $theme) { - $themesArray[] = $this->themeDataTypeFactory->createFromCoreTheme(theme: $theme); - } + $themeDataTypes = $this->themeListInfrastructure->getThemes(); - return $this->componentFilterService->filterComponents($themesArray, $filters); + return $this->componentFilterService->filterComponents($themeDataTypes, $filters); } } diff --git a/tests/Integration/Infrastructure/ThemeListInfrastructureTest.php b/tests/Integration/Infrastructure/ThemeListInfrastructureTest.php index c42f600..058aa7f 100644 --- a/tests/Integration/Infrastructure/ThemeListInfrastructureTest.php +++ b/tests/Integration/Infrastructure/ThemeListInfrastructureTest.php @@ -9,8 +9,9 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Tests\Integration\Infrastructure; -use OxidEsales\Eshop\Core\Theme; use OxidEsales\EshopCommunity\Tests\Integration\IntegrationTestCase; +use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataTypeFactoryInterface; +use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataTypeInterface; use OxidEsales\GraphQL\ConfigurationAccess\Theme\Infrastructure\CoreThemeFactoryInterface; use OxidEsales\GraphQL\ConfigurationAccess\Theme\Infrastructure\ThemeListInfrastructure; @@ -28,7 +29,7 @@ public function testGetThemesValidData(): void $this->assertIsArray($themesArray); foreach ($themesArray as $theme) { - $this->assertInstanceOf(Theme::class, $theme); + $this->assertInstanceOf(ThemeDataTypeInterface::class, $theme); } } @@ -36,6 +37,7 @@ public function getSut(): ThemeListInfrastructure { return new ThemeListInfrastructure( $this->get(CoreThemeFactoryInterface::class), + $this->get(ThemeDataTypeFactoryInterface::class) ); } } diff --git a/tests/Unit/Theme/Infrastructure/ThemeListInfrastructureTest.php b/tests/Unit/Theme/Infrastructure/ThemeListInfrastructureTest.php index ee28867..be764be 100644 --- a/tests/Unit/Theme/Infrastructure/ThemeListInfrastructureTest.php +++ b/tests/Unit/Theme/Infrastructure/ThemeListInfrastructureTest.php @@ -10,6 +10,8 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\Theme\Infrastructure; use OxidEsales\Eshop\Core\Theme; +use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataType; +use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataTypeFactoryInterface; use OxidEsales\GraphQL\ConfigurationAccess\Theme\Infrastructure\CoreThemeFactoryInterface; use OxidEsales\GraphQL\ConfigurationAccess\Theme\Exception\ThemesNotFound; use OxidEsales\GraphQL\ConfigurationAccess\Theme\Infrastructure\ThemeListInfrastructure; @@ -24,14 +26,22 @@ public function testGetThemes(): void { $theme1 = $this->createStub(Theme::class); $theme2 = $this->createStub(Theme::class); + $themeDataType1 = $this->createStub(ThemeDataType::class); + $themeDataType2 = $this->createStub(ThemeDataType::class); $coreThemeMock = $this->createMock(Theme::class); $coreThemeMock->method('getList')->willReturn([$theme1, $theme2]); $coreThemeFactoryMock = $this->getCoreThemeFactoryMock(returnValue: $coreThemeMock); - $sut = $this->getSut(coreThemeFactory: $coreThemeFactoryMock); + $themeDataTypeFactory = $this->createMock(ThemeDataTypeFactoryInterface::class); + $themeDataTypeFactory->method('createFromCoreTheme')->willReturnMap([ + [$theme1, $themeDataType1], + [$theme2, $themeDataType2] + ]); + + $sut = $this->getSut(coreThemeFactory: $coreThemeFactoryMock, themeDataTypeFactory: $themeDataTypeFactory); $actualThemesArray = $sut->getThemes(); - $this->assertSame([$theme1, $theme2], $actualThemesArray); + $this->assertSame([$themeDataType1, $themeDataType2], $actualThemesArray); } public function testGetThemesThrowsException(): void @@ -47,10 +57,12 @@ public function testGetThemesThrowsException(): void } private function getSut( - CoreThemeFactoryInterface $coreThemeFactory + CoreThemeFactoryInterface $coreThemeFactory = null, + ThemeDataTypeFactoryInterface $themeDataTypeFactory = null ): ThemeListInfrastructure { return new ThemeListInfrastructure( - coreThemeFactory: $coreThemeFactory + coreThemeFactory: $coreThemeFactory ?? $this->createStub(CoreThemeFactoryInterface::class), + themeDataTypeFactory: $themeDataTypeFactory ?? $this->createStub(ThemeDataTypeFactoryInterface::class) ); } diff --git a/tests/Unit/Theme/Service/ThemeListServiceTest.php b/tests/Unit/Theme/Service/ThemeListServiceTest.php index e729e47..f5b3329 100644 --- a/tests/Unit/Theme/Service/ThemeListServiceTest.php +++ b/tests/Unit/Theme/Service/ThemeListServiceTest.php @@ -25,37 +25,24 @@ class ThemeListServiceTest extends TestCase { public function testGetThemeListWithFilters(): void { - $theme1 = $this->createStub(Theme::class); - $theme2 = $this->createStub(Theme::class); - $themes = [$theme1, $theme2]; $themeDataType1 = $this->createStub(ThemeDataTypeInterface::class); $themeDataType2 = $this->createStub(ThemeDataTypeInterface::class); - $themeList = [$themeDataType1, $themeDataType2]; + $themes = [$themeDataType1, $themeDataType2]; $filteredThemeList = [$themeDataType1]; $themeListInfrastructureMock = $this->createMock(ThemeListInfrastructureInterface::class); $themeListInfrastructureMock->method('getThemes')->willReturn($themes); - $themeDataTypeFactoryMock = $this->createMock(ThemeDataTypeFactoryInterface::class); - $themeDataTypeFactoryMock - ->expects($this->exactly(2)) - ->method('createFromCoreTheme') - ->willReturnMap([ - [$theme1, $themeDataType1], - [$theme2, $themeDataType2] - ]); - $componentFiltersStub = $this->createStub(ComponentFiltersInterface::class); $componentFilterServiceMock = $this->createMock(ComponentFilterServiceInterface::class); $componentFilterServiceMock->method('filterComponents') - ->with($themeList, $componentFiltersStub) + ->with($themes, $componentFiltersStub) ->willReturn($filteredThemeList); $themeListService = new ThemeListService( themeListInfrastructure: $themeListInfrastructureMock, componentFilterService: $componentFilterServiceMock, - themeDataTypeFactory: $themeDataTypeFactoryMock ); $actualThemes = $themeListService->getThemeList($componentFiltersStub); $this->assertSame($filteredThemeList, $actualThemes);