Skip to content

Commit

Permalink
OXDEV-7557 Use the jsonDecodeCollection in ModuleSettingService
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 618fb4c commit 7780923
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 39 deletions.
12 changes: 5 additions & 7 deletions src/Setting/Service/ModuleSettingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
37 changes: 6 additions & 31 deletions tests/Unit/Service/ModuleSettingServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand Down

0 comments on commit 7780923

Please sign in to comment.