diff --git a/src/Setting/Service/ModuleSettingService.php b/src/Setting/Service/ModuleSettingService.php index 0765bc6..a507a81 100644 --- a/src/Setting/Service/ModuleSettingService.php +++ b/src/Setting/Service/ModuleSettingService.php @@ -22,7 +22,8 @@ final class ModuleSettingService implements ModuleSettingServiceInterface { public function __construct( - private ModuleSettingRepositoryInterface $moduleSettingRepository + private ModuleSettingRepositoryInterface $moduleSettingRepository, + private JsonServiceInterface $jsonService ) { } @@ -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) ); } diff --git a/tests/Unit/Service/ModuleSettingServiceTest.php b/tests/Unit/Service/ModuleSettingServiceTest.php index 6722bbb..5c6136f 100644 --- a/tests/Unit/Service/ModuleSettingServiceTest.php +++ b/tests/Unit/Service/ModuleSettingServiceTest.php @@ -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; @@ -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( @@ -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( @@ -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( @@ -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( @@ -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) ); } @@ -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'); @@ -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'); @@ -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; @@ -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'; @@ -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'); @@ -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"]'; @@ -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), + ); + } }