Skip to content

Commit

Permalink
Move ThemeDataType creation into service
Browse files Browse the repository at this point in the history
  • Loading branch information
RahatHameed committed Jul 30, 2024
1 parent 5f0d9ac commit 62b6a40
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 62 deletions.
11 changes: 2 additions & 9 deletions src/Theme/Infrastructure/ThemeListInfrastructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
}

Expand All @@ -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;
}
}
13 changes: 11 additions & 2 deletions src/Theme/Service/ThemeListService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
}

Expand All @@ -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);
}
}
71 changes: 22 additions & 49 deletions tests/Unit/Theme/Infrastructure/ThemeListInfrastructureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,78 +25,51 @@ 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
{
$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)
);
}

Expand Down
13 changes: 11 additions & 2 deletions tests/Unit/Theme/Service/ThemeListServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,14 +27,20 @@ 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];
$filteredThemeList = [$theme1];

$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()),
Expand All @@ -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);
Expand Down

0 comments on commit 62b6a40

Please sign in to comment.