From 62b6a4079d662e934ac64bcb9ae4db2ed835628c Mon Sep 17 00:00:00 2001 From: RahatHameed Date: Tue, 30 Jul 2024 11:16:53 +0200 Subject: [PATCH] Move ThemeDataType creation into service --- .../ThemeListInfrastructure.php | 11 +-- src/Theme/Service/ThemeListService.php | 13 +++- .../ThemeListInfrastructureTest.php | 71 ++++++------------- .../Theme/Service/ThemeListServiceTest.php | 13 +++- 4 files changed, 46 insertions(+), 62 deletions(-) diff --git a/src/Theme/Infrastructure/ThemeListInfrastructure.php b/src/Theme/Infrastructure/ThemeListInfrastructure.php index 7922a59..0ad5f73 100644 --- a/src/Theme/Infrastructure/ThemeListInfrastructure.php +++ b/src/Theme/Infrastructure/ThemeListInfrastructure.php @@ -9,14 +9,12 @@ 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 ThemeDataTypeFactoryInterface $themeDataTypeFactory + private readonly CoreThemeFactoryInterface $coreThemeFactory ) { } @@ -29,11 +27,6 @@ public function getThemes(): array throw new ThemesNotFound(); } - $themesArray = []; - foreach ($themesList as $theme) { - $themesArray[] = $this->themeDataTypeFactory->createFromCoreTheme(theme: $theme); - } - - return $themesArray; + return $themesList; } } diff --git a/src/Theme/Service/ThemeListService.php b/src/Theme/Service/ThemeListService.php index 1da3e31..6682ffa 100644 --- a/src/Theme/Service/ThemeListService.php +++ b/src/Theme/Service/ThemeListService.php @@ -9,15 +9,18 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Theme\Service; +use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataTypeFactoryInterface; use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeFiltersInterface; use OxidEsales\GraphQL\ConfigurationAccess\Theme\Infrastructure\ThemeListInfrastructureInterface; use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataType; +use OxidEsales\Eshop\Core\Theme; final class ThemeListService implements ThemeListServiceInterface { public function __construct( private readonly ThemeListInfrastructureInterface $themeListInfrastructure, - private readonly ThemeFilterServiceInterface $themeFilterService + private readonly ThemeFilterServiceInterface $themeFilterService, + private readonly ThemeDataTypeFactoryInterface $themeDataTypeFactory ) { } @@ -26,7 +29,13 @@ public function __construct( */ public function getThemeList(ThemeFiltersInterface $filters): array { - $themesArray = $this->themeListInfrastructure->getThemes(); + $themesArray = []; + $themesList = $this->themeListInfrastructure->getThemes(); + foreach ($themesList as $theme) { + /** @var Theme $theme */ + $themesArray[] = $this->themeDataTypeFactory->createFromCoreTheme(theme: $theme); + } + return $this->themeFilterService->filterThemes($themesArray, $filters); } } diff --git a/tests/Unit/Theme/Infrastructure/ThemeListInfrastructureTest.php b/tests/Unit/Theme/Infrastructure/ThemeListInfrastructureTest.php index 5ef3d8e..39caa9e 100644 --- a/tests/Unit/Theme/Infrastructure/ThemeListInfrastructureTest.php +++ b/tests/Unit/Theme/Infrastructure/ThemeListInfrastructureTest.php @@ -25,34 +25,31 @@ class ThemeListInfrastructureTest extends TestCase { public function testGetThemes(): void { - $theme1 = $this->createThemeMock(uniqid(), uniqid(), uniqid(), uniqid(), true); - $theme2 = $this->createThemeMock(uniqid(), uniqid(), uniqid(), uniqid(), false); - - $convertThemeCallback = function (Theme $theme) { - return new ThemeDataType( - $theme->getInfo('title'), - $theme->getInfo('id'), - $theme->getInfo('version'), - $theme->getInfo('description'), - $theme->getInfo('active') - ); - }; - $theme1DataType = $convertThemeCallback($theme1); - $theme2DataType = $convertThemeCallback($theme2); + $theme1 = [ + 'title' => uniqid(), + 'id' => uniqid(), + 'version' => uniqid(), + 'description' => uniqid(), + 'active' => true + ]; + + $theme2 = [ + 'title' => uniqid(), + 'id' => uniqid(), + 'version' => uniqid(), + 'description' => uniqid(), + 'active' => false + ]; $coreThemeMock = $this->createMock(Theme::class); $coreThemeMock->expects($this->once())->method('getList') ->willReturn([$theme1, $theme2]); - $coreThemeFactoryMock = $this->getCoreThemeFactoryMock($coreThemeMock); - $themeDataTypeFactoryMock = $this->createMock(ThemeDataTypeFactoryInterface::class); - $themeDataTypeFactoryMock->expects($this->exactly(2))->method('createFromCoreTheme') - ->with($this->logicalOr($theme1, $theme2)) - ->willReturnCallback($convertThemeCallback); + $coreThemeFactoryMock = $this->getCoreThemeFactoryMock(returnValue: $coreThemeMock); - $sut = $this->getSut(coreThemeFactory: $coreThemeFactoryMock, themeDataTypeFactory: $themeDataTypeFactoryMock); + $sut = $this->getSut(coreThemeFactory: $coreThemeFactoryMock); $actualThemesArray = $sut->getThemes(); - $this->assertEquals([$theme1DataType, $theme2DataType], $actualThemesArray); + $this->assertEquals([$theme1, $theme2], $actualThemesArray); } public function testGetThemesThrowsException(): void @@ -60,43 +57,19 @@ public function testGetThemesThrowsException(): void $coreThemeMock = $this->createMock(Theme::class); $coreThemeMock->expects($this->once())->method('getList') ->willReturn([]); - $coreThemeFactoryMock = $this->getCoreThemeFactoryMock($coreThemeMock); + $coreThemeFactoryMock = $this->getCoreThemeFactoryMock(returnValue: $coreThemeMock); - $themeDataTypeFactoryMock = $this->createMock(ThemeDataTypeFactoryInterface::class); - $sut = $this->getSut(coreThemeFactory: $coreThemeFactoryMock, themeDataTypeFactory: $themeDataTypeFactoryMock); + $sut = $this->getSut(coreThemeFactory: $coreThemeFactoryMock); $this->expectException(ThemesNotFound::class); $sut->getThemes(); } - private function createThemeMock( - string $title, - string $id, - string $version, - string $description, - bool $active - ): Theme|MockObject { - $themeMock = $this->createMock(Theme::class); - $themeMock->expects($this->exactly(10)) - ->method('getInfo') - ->willReturnMap([ - ['title', $title], - ['id', $id], - ['version', $version], - ['description', $description], - ['active', $active] - ]); - - return $themeMock; - } - private function getSut( - CoreThemeFactoryInterface $coreThemeFactory = null, - ThemeDataTypeFactoryInterface $themeDataTypeFactory = null + CoreThemeFactoryInterface $coreThemeFactory = null ): ThemeListInfrastructure { return new ThemeListInfrastructure( - coreThemeFactory: $coreThemeFactory ?? $this->createStub(CoreThemeFactoryInterface::class), - themeDataTypeFactory: $themeDataTypeFactory ?? $this->createStub(ThemeDataTypeFactoryInterface::class) + coreThemeFactory: $coreThemeFactory ?? $this->createStub(CoreThemeFactoryInterface::class) ); } diff --git a/tests/Unit/Theme/Service/ThemeListServiceTest.php b/tests/Unit/Theme/Service/ThemeListServiceTest.php index 271cb93..2894f18 100644 --- a/tests/Unit/Theme/Service/ThemeListServiceTest.php +++ b/tests/Unit/Theme/Service/ThemeListServiceTest.php @@ -12,11 +12,13 @@ use OxidEsales\GraphQL\Base\DataType\Filter\BoolFilter; use OxidEsales\GraphQL\Base\DataType\Filter\StringFilter; use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataType; +use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeDataTypeFactoryInterface; use OxidEsales\GraphQL\ConfigurationAccess\Theme\DataType\ThemeFilters; use OxidEsales\GraphQL\ConfigurationAccess\Theme\Infrastructure\ThemeListInfrastructureInterface; use OxidEsales\GraphQL\ConfigurationAccess\Theme\Service\ThemeFilterServiceInterface; use OxidEsales\GraphQL\ConfigurationAccess\Theme\Service\ThemeListService; use PHPUnit\Framework\TestCase; +use OxidEsales\Eshop\Core\Theme; /** * @covers \OxidEsales\GraphQL\ConfigurationAccess\Theme\Service\ThemeListService @@ -25,6 +27,7 @@ class ThemeListServiceTest extends TestCase { public function testGetThemeListWithFilters(): void { + $themes = [new Theme(), new Theme()]; $theme1 = new ThemeDataType(uniqid(), uniqid(), uniqid(), uniqid(), true); $theme2 = new ThemeDataType(uniqid(), uniqid(), uniqid(), uniqid(), false); $themeList = [$theme1, $theme2]; @@ -32,7 +35,12 @@ public function testGetThemeListWithFilters(): void $themeListInfrastructureMock = $this->createMock(ThemeListInfrastructureInterface::class); $themeListInfrastructureMock->expects($this->once())->method('getThemes') - ->willReturn($themeList); + ->willReturn($themes); + + $themeDataTypeFactoryMock = $this->createMock(ThemeDataTypeFactoryInterface::class); + $themeDataTypeFactoryMock + ->method('createFromCoreTheme') + ->willReturnOnConsecutiveCalls($themeList[0], $themeList[1]); $filtersList = new ThemeFilters( titleFilter: new StringFilter(contains: uniqid()), @@ -46,7 +54,8 @@ public function testGetThemeListWithFilters(): void $themeListService = new ThemeListService( themeListInfrastructure: $themeListInfrastructureMock, - themeFilterService: $themeFilterServiceMock + themeFilterService: $themeFilterServiceMock, + themeDataTypeFactory: $themeDataTypeFactoryMock ); $actualThemes = $themeListService->getThemeList($filtersList); $this->assertSame($filteredThemeList, $actualThemes);