Skip to content

Commit

Permalink
OXDEV-8952: Move ThemeDataType generation to Infrastructur
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelOxid committed Dec 16, 2024
1 parent 6109e9a commit 91f5515
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 37 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions src/Module/Infrastructure/ModuleListInfrastructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
14 changes: 12 additions & 2 deletions src/Theme/Infrastructure/ThemeListInfrastructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}
}
4 changes: 2 additions & 2 deletions src/Theme/Infrastructure/ThemeListInfrastructureInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<Theme>
* @return ThemeDataTypeInterface[]
* @throws ThemesNotFound
*/
public function getThemes(): array;
Expand Down
12 changes: 3 additions & 9 deletions src/Theme/Service/ThemeListService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -28,14 +29,15 @@ public function testGetThemesValidData(): void
$this->assertIsArray($themesArray);

foreach ($themesArray as $theme) {
$this->assertInstanceOf(Theme::class, $theme);
$this->assertInstanceOf(ThemeDataTypeInterface::class, $theme);
}
}

public function getSut(): ThemeListInfrastructure
{
return new ThemeListInfrastructure(
$this->get(CoreThemeFactoryInterface::class),
$this->get(ThemeDataTypeFactoryInterface::class)
);
}
}
20 changes: 16 additions & 4 deletions tests/Unit/Theme/Infrastructure/ThemeListInfrastructureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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)
);
}

Expand Down
17 changes: 2 additions & 15 deletions tests/Unit/Theme/Service/ThemeListServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 91f5515

Please sign in to comment.