Skip to content

Commit

Permalink
OXDEV-7557 Use json encoder in Module settings collection encoding
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Fedurtsya <[email protected]>
  • Loading branch information
Sieg committed Nov 15, 2023
1 parent 164151d commit a6021a2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
7 changes: 5 additions & 2 deletions src/Setting/Service/ModuleSettingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
final class ModuleSettingService implements ModuleSettingServiceInterface
{
public function __construct(
private ModuleSettingRepositoryInterface $moduleSettingRepository
private ModuleSettingRepositoryInterface $moduleSettingRepository,
private JsonServiceInterface $jsonService
) {
}

Expand Down Expand Up @@ -60,9 +61,11 @@ public function getStringSetting(ID $name, string $moduleId): StringSetting

public function getCollectionSetting(ID $name, string $moduleId): StringSetting
{
$collection = $this->moduleSettingRepository->getCollectionSetting((string)$name, $moduleId);

return new StringSetting(
$name,
json_encode($this->moduleSettingRepository->getCollectionSetting((string)$name, $moduleId))
$this->jsonService->jsonEncodeArray($collection)
);
}

Expand Down
46 changes: 33 additions & 13 deletions tests/Unit/Service/ModuleSettingServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Enum\FieldType;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Exception\InvalidCollection;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Infrastructure\ModuleSettingRepositoryInterface;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Service\JsonServiceInterface;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Service\ModuleSettingService;
use OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\UnitTestCase;
use TheCodingMachine\GraphQLite\Types\ID;
Expand All @@ -28,7 +29,7 @@ public function testGetModuleSettingInteger(): void
->with($name, $moduleId)
->willReturn($repositoryResponse);

$sut = new ModuleSettingService($repository);
$sut = $this->getSut($repository);

$nameID = new ID($name);
$this->assertEquals(
Expand All @@ -49,7 +50,7 @@ public function testGetModuleSettingFloat(): void
->with($name, $moduleId)
->willReturn($repositoryResponse);

$sut = new ModuleSettingService($repository);
$sut = $this->getSut($repository);

$nameID = new ID($name);
$this->assertEquals(
Expand All @@ -70,7 +71,7 @@ public function testGetModuleSettingBoolean(): void
->with($name, $moduleId)
->willReturn($repositoryResponse);

$sut = new ModuleSettingService($repository);
$sut = $this->getSut($repository);

$nameID = new ID($name);
$this->assertEquals(
Expand All @@ -91,7 +92,7 @@ public function testGetModuleSettingString(): void
->with($name, $moduleId)
->willReturn($repositoryResponse);

$sut = new ModuleSettingService($repository);
$sut = $this->getSut($repository);

$nameID = new ID($name);
$this->assertEquals(
Expand All @@ -112,11 +113,20 @@ public function testGetModuleSettingCollection(): void
->with($name, $moduleId)
->willReturn($repositoryResponse);

$sut = new ModuleSettingService($repository);
$encoderResponse = 'encoderResponse';
$encoder = $this->createMock(JsonServiceInterface::class);
$encoder->method('jsonEncodeArray')
->with($repositoryResponse)
->willReturn($encoderResponse);

$sut = $this->getSut(
repository: $repository,
jsonService: $encoder
);

$nameID = new ID($name);
$this->assertEquals(
new StringSetting($nameID, json_encode($repositoryResponse)),
new StringSetting($nameID, $encoderResponse),
$sut->getCollectionSetting($nameID, $moduleId)
);
}
Expand All @@ -125,7 +135,7 @@ public function testChangeModuleSettingInteger(): void
{
$repository = $this->createMock(ModuleSettingRepositoryInterface::class);

$settingService = new ModuleSettingService($repository);
$settingService = $this->getSut($repository);

$nameID = new ID('intSetting');
$integerSetting = $settingService->changeIntegerSetting($nameID, 123, 'awesomeModule');
Expand All @@ -138,7 +148,7 @@ public function testChangeModuleSettingFloat(): void
{
$repository = $this->createMock(ModuleSettingRepositoryInterface::class);

$settingService = new ModuleSettingService($repository);
$settingService = $this->getSut($repository);

$nameID = new ID('floatSetting');
$floatSetting = $settingService->changeFloatSetting($nameID, 1.23, 'awesomeModule');
Expand All @@ -151,7 +161,7 @@ public function testChangeModuleSettingBoolean(): void
{
$repository = $this->createMock(ModuleSettingRepositoryInterface::class);

$settingService = new ModuleSettingService($repository);
$settingService = $this->getSut($repository);

$nameID = new ID('boolSetting');
$value = false;
Expand All @@ -165,7 +175,7 @@ public function testChangeModuleSettingString(): void
{
$repository = $this->createMock(ModuleSettingRepositoryInterface::class);

$settingService = new ModuleSettingService($repository);
$settingService = $this->getSut($repository);

$nameID = new ID('stringSetting');
$value = 'default';
Expand All @@ -182,7 +192,7 @@ public function testChangeModuleSettingInvalidCollection($value): void
{
$repository = $this->createMock(ModuleSettingRepositoryInterface::class);

$settingService = new ModuleSettingService($repository);
$settingService = $this->getSut($repository);

$nameID = new ID('collectionSetting');

Expand All @@ -208,7 +218,7 @@ public function testChangeModuleSettingCollection(): void
{
$repository = $this->createMock(ModuleSettingRepositoryInterface::class);

$settingService = new ModuleSettingService($repository);
$settingService = $this->getSut($repository);

$nameID = new ID('collectionSetting');
$value = '[2, "values"]';
Expand All @@ -232,7 +242,17 @@ public function testListModuleSettings(): void
->with($moduleId)
->willReturn([$intSetting, $stringSetting, $arraySetting]);

$sut = new ModuleSettingService($repository);
$sut = $this->getSut($repository);
$this->assertEquals($this->getSettingTypeList(), $sut->getSettingsList($moduleId));
}

private function getSut(
?ModuleSettingRepositoryInterface $repository = null,
?JsonServiceInterface $jsonService = null,
): ModuleSettingService {
return new ModuleSettingService(
moduleSettingRepository: $repository ?? $this->createStub(ModuleSettingRepositoryInterface::class),
jsonService: $jsonService ?? $this->createStub(JsonServiceInterface::class),
);
}
}

0 comments on commit a6021a2

Please sign in to comment.