diff --git a/src/Setting/Service/ModuleSettingService.php b/src/Setting/Service/ModuleSettingService.php index 4c07f18..4ceec20 100644 --- a/src/Setting/Service/ModuleSettingService.php +++ b/src/Setting/Service/ModuleSettingService.php @@ -99,13 +99,11 @@ public function changeStringSetting(ID $name, string $value, string $moduleId): public function changeCollectionSetting(ID $name, string $value, string $moduleId): StringSetting { - $arrayValue = json_decode($value, true); - - if (!is_array($arrayValue) || json_last_error() !== JSON_ERROR_NONE) { - throw new InvalidCollection($value); - } - - $this->moduleSettingRepository->saveCollectionSetting((string)$name, $arrayValue, $moduleId); + $this->moduleSettingRepository->saveCollectionSetting( + (string)$name, + $this->jsonService->jsonDecodeCollection($value), + $moduleId + ); return $this->getCollectionSetting($name, $moduleId); } diff --git a/tests/Codeception/Acceptance/ModuleSettingMutationsCest.php b/tests/Codeception/Acceptance/ModuleSettingMutationsCest.php index 04eae60..5b272db 100644 --- a/tests/Codeception/Acceptance/ModuleSettingMutationsCest.php +++ b/tests/Codeception/Acceptance/ModuleSettingMutationsCest.php @@ -160,6 +160,6 @@ public function testChangeCollectionSettingAuthorized(AcceptanceTester $I): void $setting = $result['data']['changeModuleSettingCollection']; $I->assertSame('arraySetting', $setting['name']); - $I->assertSame('[3, "interesting", "values"]', $setting['value']); + $I->assertSame('[3,"interesting","values"]', $setting['value']); } } diff --git a/tests/Unit/Service/ModuleSettingServiceTest.php b/tests/Unit/Service/ModuleSettingServiceTest.php index 63d25f4..5a83516 100644 --- a/tests/Unit/Service/ModuleSettingServiceTest.php +++ b/tests/Unit/Service/ModuleSettingServiceTest.php @@ -243,47 +243,19 @@ public function testChangeModuleSettingString(): void $this->assertSame($repositoryValue, $setting->getValue()); } - /** - * @dataProvider invalidCollectionDataProvider - */ - public function testChangeModuleSettingInvalidCollection($value): void - { - $repository = $this->createMock(ModuleSettingRepositoryInterface::class); - - $settingService = $this->getSut($repository); - - $nameID = new ID('collectionSetting'); - - $this->expectException(InvalidCollection::class); - $this->expectExceptionMessage(sprintf('%s is not a valid collection string.', $value)); - - $settingService->changeCollectionSetting($nameID, $value, 'awesomeModule'); - } - - public function invalidCollectionDataProvider(): array - { - return [ - ['[2, "values"'], - ['{2, "values"}'], - ['2, "values"}'], - ['[2, values]'], - ['"3, interesting, values"'], - ['"3, \'interesting\', \'values\'"'], - ]; - } - public function testChangeModuleSettingCollection(): void { $name = 'collectionSetting'; $moduleId = 'awesomeModule'; - $callValue = '[2, "values"]'; + $callValue = 'someCollectionValue'; $repositoryValue = ['realDatabaseValue']; + $decodedValue = ['decodedCollectionValue']; $repository = $this->createMock(ModuleSettingRepositoryInterface::class); $repository->expects($this->once()) ->method('saveCollectionSetting') - ->with($name, json_decode($callValue), $moduleId); + ->with($name, $decodedValue, $moduleId); $repository->method('getCollectionSetting') ->with($name, $moduleId) ->willReturn($repositoryValue); @@ -293,6 +265,9 @@ public function testChangeModuleSettingCollection(): void $encoder->method('jsonEncodeArray') ->with($repositoryValue) ->willReturn($encoderResponse); + $encoder->method('jsonDecodeCollection') + ->with($callValue) + ->willReturn($decodedValue); $sut = $this->getSut( repository: $repository,