Skip to content

Commit

Permalink
OXDEV-8952: Move ModuleDataType generation to Infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelOxid committed Dec 13, 2024
1 parent 7c22b8d commit 654dbf8
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 50 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions src/Module/Infrastructure/ModuleListInfrastructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ModuleConfiguration>
* @throws ModulesNotFoundException
* @return array<ModuleDataTypeInterface>
*/
public function getModuleConfigurations(): array;
public function getModuleList(): array;
}
10 changes: 2 additions & 8 deletions src/Module/Service/ModuleListService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
3 changes: 3 additions & 0 deletions src/Shared/Subscriber/BeforeModuleDeactivation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

final class BeforeModuleDeactivation implements EventSubscriberInterface
{
/**
* @param string[] $dependencies
*/
public function __construct(
private array $dependencies
) {
Expand Down
26 changes: 17 additions & 9 deletions tests/Integration/Infrastructure/ModuleListInfrastructureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,31 +26,37 @@ 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
->addModuleConfiguration($moduleConfiguration1)
->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)
);
}
}
34 changes: 22 additions & 12 deletions tests/Unit/Module/Infrastructure/ModuleListInfrastructureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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)
);
}
}
18 changes: 3 additions & 15 deletions tests/Unit/Module/Service/ModuleListServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 654dbf8

Please sign in to comment.